This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupingthread.pas
98 lines (88 loc) · 2.06 KB
/
upingthread.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
unit UPingThread;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
{$IFDEF WINDOWS}
pingsend;
{$ENDIF}
{$IFDEF LINUX}
fpjson, jsonparser, process, Dialogs;
{$ENDIF}
type
TShowStatusEvent = procedure(status: boolean; return: string) of Object;
TPingThread = class(TThread)
private
fResult: boolean;
fStatusText: string;
FOnShowStatus: TShowStatusEvent;
host: string;
procedure showStatus;
protected
procedure execute; override;
public
constructor create(hostC: string);
property OnShowStatus: TShowStatusEvent read FOnShowStatus write FOnShowStatus;
end;
implementation
constructor TPingThread.create(hostC: string);
begin
host:=hostC;
FreeOnTerminate:=true;
inherited create(true);
end;
procedure TPingThread.showStatus;
begin
if Assigned(FOnShowStatus) then
begin
FOnShowStatus(fResult, fStatusText);
end;
end;
procedure TPingThread.execute;
var
{$IFDEF WINDOWS}
ping: TPingSend;
{$ENDIF}
{$IFDEF LINUX}
ping: TProcess;
response: TStringList;
jData: TJSONData;
{$ENDIF}
begin
{$IFDEF WINDOWS}
ping:=TPingSend.create();
if (ping.ping(host)) then begin
fResult:=true;
fStatusText:=ping.replyFrom;
end
else begin
fResult:=false;
fStatusText:=ping.replyErrorDesc;
end;
{$ENDIF}
{$IFDEF LINUX}
ping:=TProcess.create(nil);
ping.executable:='sh';
ping.parameters.add('-c');
ping.parameters.add('sudo PhilleConnectOnlineChecker '+host);
ping.showWindow:=swoHIDE;
ping.options:=ping.options + [poWaitOnExit, poUsePipes];
ping.execute;
response:=TStringList.create;
response.LoadFromStream(ping.output);
ping.free;
jData:=GetJSON(response[0]);
fStatusText:=jData.AsJSON;
response.free;
if (jData.FindPath('[0]').AsString = 'true') then begin
fResult:=true;
fStatusText:=jData.FindPath('[1]').AsString;
end
else begin
fResult:=false;
fStatusText:=jData.FindPath('[1]').AsString;
end;
{$ENDIF}
Synchronize(@ShowStatus);
end;
end.