-
Notifications
You must be signed in to change notification settings - Fork 1
/
DX.Utils.Connectivity.pas
181 lines (161 loc) · 4.83 KB
/
DX.Utils.Connectivity.pas
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
unit DX.Utils.Connectivity;
interface
uses
System.Classes, System.SysUtils,
System.Generics.Collections;
type
TConnectivityState = (Unkown, Offline, Online);
TDXServer = class(TObject)
private
FPort: Integer;
FConnectionTimeout: Integer;
FHostname: string;
protected
procedure SetHostname(const Value: string); virtual;
public
constructor Create(const AHostname: string; APort: Integer; AConnectionTimeout: Integer = 1000);
published
property Port: Integer read FPort write FPort;
property ConnectionTimeout: Integer read FConnectionTimeout write FConnectionTimeout;
property Hostname: string read FHostname write SetHostname;
end;
TDXConnectivity = class(TComponent)
private
FActive: Boolean;
FOnConnectivityChanged: TNotifyEvent;
FServers: TObjectList<TDXServer>;
FState: TConnectivityState;
function GetDeviceIsOnline: Boolean;
procedure SetDeviceIsOnline(const Value: Boolean);
procedure SetState(const Value: TConnectivityState);
protected
procedure DoConnectivityChanged; virtual;
procedure SetActive(const Value: Boolean); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Active: Boolean read FActive write SetActive;
property DeviceIsOnline: Boolean read GetDeviceIsOnline write SetDeviceIsOnline;
property Servers: TObjectList<TDXServer> read FServers write FServers;
property State: TConnectivityState read FState write SetState;
property OnConnectivityChanged: TNotifyEvent read FOnConnectivityChanged write FOnConnectivityChanged;
end;
implementation
uses
System.Threading, IdHTTP, IdTCPClient, DX.Utils.Logger;
{ TDXConnectivity }
constructor TDXConnectivity.Create(AOwner: TComponent);
var
LServer: TDXServer;
begin
inherited;
FActive := false;
FServers := TObjectList<TDXServer>.Create;
LServer := TDXServer.Create('www.google.com', 443, 1000);
FServers.Add(LServer);
FState := TConnectivityState.Unkown;
Log('Device state unknown!');
end;
destructor TDXConnectivity.Destroy;
begin
Active := false;
FreeAndNil(FServers);
inherited;
end;
procedure TDXConnectivity.DoConnectivityChanged;
begin
if DeviceIsOnline then
Log('Device is online!')
else
Log('Device is offline!');
if Assigned(FOnConnectivityChanged) then
// Synchronize - don't queue. That avoids "hyper activity" if connection is very flaky
TThread.Synchronize(nil,
procedure
begin
FOnConnectivityChanged(Self)
end);
end;
function TDXConnectivity.GetDeviceIsOnline: Boolean;
begin
Result := State = TConnectivityState.Online;
end;
procedure TDXConnectivity.SetActive(const Value: Boolean);
begin
if Value <> FActive then
begin
FActive := Value;
if FActive then
begin
TTask.Run(
procedure
var
LConnection: TIdTCPClient;
LSuccess: Boolean;
i: Integer;
LServer: TDXServer;
begin
LConnection := TIdTCPClient.Create(nil);
try
while Active and (Servers.Count > 0) do
begin
LSuccess := true;
for LServer in Servers do
begin
if LServer.Hostname = '' then
continue;
LConnection.Host := LServer.Hostname;
LConnection.Port := LServer.Port;
LConnection.ConnectTimeout := LServer.ConnectionTimeout;
try
LConnection.Connect;
LConnection.Disconnect;
except
LSuccess := false;
end;
end;
DeviceIsOnline := LSuccess;
// check once a second, terminate early
for i := 1 to 10 do
begin
sleep(100);
if not Active then
break;
end;
end;
finally
FreeAndNil(LConnection);
end;
end);
end
end
end;
procedure TDXConnectivity.SetDeviceIsOnline(const Value: Boolean);
begin
if Value then
State := TConnectivityState.Online
else
State := TConnectivityState.Offline;
end;
procedure TDXConnectivity.SetState(const Value: TConnectivityState);
begin
if FState <> Value then
begin
FState := Value;
DoConnectivityChanged;
end;
end;
{ TDXServer }
constructor TDXServer.Create(const AHostname: string; APort: Integer; AConnectionTimeout: Integer = 1000);
begin
inherited Create;
FHostname := AHostname;
FPort := APort;
FConnectionTimeout := AConnectionTimeout;
end;
procedure TDXServer.SetHostname(const Value: string);
begin
FHostname := Value.Trim;
end;
end.