-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnline.cs
More file actions
142 lines (125 loc) · 4.07 KB
/
Online.cs
File metadata and controls
142 lines (125 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
using System.Drawing;
using System.Net;
namespace AuditSec
{
class Online
{
static Icon[] ICONS = {
Icon.FromHandle(global::AuditSec.Properties.Resources.offline2.GetHicon()),
Icon.FromHandle(global::AuditSec.Properties.Resources.offline.GetHicon()),
Icon.FromHandle(global::AuditSec.Properties.Resources.online.GetHicon()),
Icon.FromHandle(global::AuditSec.Properties.Resources.online2.GetHicon()),
};
static Icon getIcon(Status status)
{
return ICONS[(int)status.GetValue()];
}
public enum STATUS
{
UNKNOWN = 0,
OFFLINE = 1,
ONLINE = 2,
ONLINE2 = 3
};
class Status
{
STATUS Value;
DateTime Time;
bool NA;
public Status(STATUS S, bool NA)
{
Value = S;
Time = DateTime.Now;
this.NA = NA;
}
public bool isOutdated()
{
return NA || DateTime.Now.Subtract(Time).TotalMinutes > 5;
}
public STATUS GetValue()
{
if (!isOutdated()) return Value;
else
{
if (Value == STATUS.UNKNOWN) return STATUS.UNKNOWN;
if (Value == STATUS.OFFLINE) return STATUS.UNKNOWN;
if (Value == STATUS.ONLINE) return STATUS.ONLINE2;
if (Value == STATUS.ONLINE2) return STATUS.OFFLINE;
else return STATUS.UNKNOWN;
}
}
}
public Icon getIcon(string machine)
{
return ICONS[(int)GetStatus(machine)];
}
Hashtable Machines = new Hashtable();
public bool STOP = false;
Func<string, bool> Refresh;
Func<bool> isActive;
public Online(Func<string, bool> Refresh, Func<bool> isActive)
{
this.Refresh = Refresh;
this.isActive = isActive;
}
public void Start()
{
while (!STOP)
{
Thread.Sleep(1000);
if (isActive()) CheckStatus();
}
}
public void Clear()
{
Machines.Clear();
}
public bool Add(string machine)
{
lock(Machines) {
if (!Machines.Contains(machine))
{
Machines.Add(machine, new Status(STATUS.UNKNOWN, true));
return true;
}
else return false;
}
}
public STATUS GetStatus(string machine)
{
return Machines.Contains(machine) ? ((Status)Machines[machine]).GetValue() : STATUS.UNKNOWN;
/*
lock (Machines)
{
if (!Machines.Contains(machine)) Add(machine);
return ((Status)Machines[machine]).GetValue();
}
*/
}
void CheckStatus()
{
new ArrayList(Machines.Keys).ToArray().Cast<string>().AsParallel().ForAll(machine =>
{
if (isActive()
&& Machines.Contains(machine)
&& ((Status)Machines[machine]).isOutdated())
lock (Machines)
{
Machines.Remove(machine);
Console.WriteLine("Online Checking... " + machine + "...");
bool reach = MachineInfo.isReachableAndValidNetbios(machine);
Status status = new Status(reach ? STATUS.ONLINE : STATUS.OFFLINE, false);
Machines.Add(machine, status);
Console.WriteLine("Online Checking... " + machine + " " + status.GetValue());
if (isActive()) Refresh(machine);
}
});
}
}
}