1 | unit UNetworkTest;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, UPing, ComCtrls, StdCtrls, cUtils, cWinSock, Spin,
|
---|
8 | IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
|
---|
9 |
|
---|
10 | const
|
---|
11 | NetworkListFile = 'NetworkList.txt';
|
---|
12 | NetworkListURL = 'http://centrala.zdechov.net/sunrisechat/network.php';
|
---|
13 |
|
---|
14 | type
|
---|
15 | TPingList = class(TThread)
|
---|
16 | private
|
---|
17 | { Private declarations }
|
---|
18 | protected
|
---|
19 | procedure Execute; override;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | TNetworkTest = class(TForm)
|
---|
23 | Button1: TButton;
|
---|
24 | ListView1: TListView;
|
---|
25 | Button2: TButton;
|
---|
26 | SpinEdit1: TSpinEdit;
|
---|
27 | Label1: TLabel;
|
---|
28 | ComboBox1: TComboBox;
|
---|
29 | IdHTTP1: TIdHTTP;
|
---|
30 | procedure Button1Click(Sender: TObject);
|
---|
31 | procedure FormCreate(Sender: TObject);
|
---|
32 | procedure FormShow(Sender: TObject);
|
---|
33 | procedure Button2Click(Sender: TObject);
|
---|
34 | procedure CheckBox1Click(Sender: TObject);
|
---|
35 | procedure SpinEdit1Change(Sender: TObject);
|
---|
36 | procedure ComboBox1Change(Sender: TObject);
|
---|
37 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
38 | private
|
---|
39 | { Private declarations }
|
---|
40 | public
|
---|
41 | PingList: TPingList;
|
---|
42 | Devices: array of record
|
---|
43 | ID: Integer;
|
---|
44 | IP: string;
|
---|
45 | Name: string;
|
---|
46 | Place: string;
|
---|
47 | Parent: Integer;
|
---|
48 | Sort: Integer;
|
---|
49 | end;
|
---|
50 | LocalPCID: Integer;
|
---|
51 | procedure RefreshList;
|
---|
52 | function IndexByID(WantedID: Integer): Integer;
|
---|
53 | function IndexByName(Name: string): Integer;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | var
|
---|
57 | NetworkTest: TNetworkTest;
|
---|
58 |
|
---|
59 | implementation
|
---|
60 |
|
---|
61 | uses Math, UMainForm;
|
---|
62 |
|
---|
63 | {$R *.dfm}
|
---|
64 |
|
---|
65 | procedure TNetworkTest.Button1Click(Sender: TObject);
|
---|
66 | begin
|
---|
67 | Close;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | procedure TNetworkTest.FormCreate(Sender: TObject);
|
---|
71 | var
|
---|
72 | Soubor: TextFile;
|
---|
73 | Row: string;
|
---|
74 | Hostname: string;
|
---|
75 | LocalIp: string;
|
---|
76 | WSAErr: string;
|
---|
77 |
|
---|
78 | function Parse: string;
|
---|
79 | begin
|
---|
80 | Result:= Copy(Row, 1, Pos(':', Row) - 1);
|
---|
81 | Delete(Row, 1, Length(Result) + 1);
|
---|
82 | end;
|
---|
83 |
|
---|
84 | function NetworkListFilename: string;
|
---|
85 | begin
|
---|
86 | Result := CommonAppDataPath + '\' + Creator + '\' + ApplicationName;
|
---|
87 | if not DirectoryExists(Result) then
|
---|
88 | ForceDirectories(Result);
|
---|
89 |
|
---|
90 | Result := Result + '\' + NetworkListFile;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | begin
|
---|
94 | PingList := TPingList.Create(True);
|
---|
95 | ChDir(ExtractFileDir(Application.ExeName));
|
---|
96 |
|
---|
97 | HostName := 'default';
|
---|
98 | GetIPFromHost(Hostname, LocalIp, WSAErr);
|
---|
99 | if LocalIp <> '127.0.0.1' then
|
---|
100 | // Update list from internet
|
---|
101 | if (Trunc(Now) <> Trunc(MainForm.NetworkTestLastUpdate)) or
|
---|
102 | (not FileExists(NetworkListFilename)) then try
|
---|
103 | Row := IdHTTP1.Get(NetworkListURL);
|
---|
104 | AssignFile(Soubor, NetworkListFilename);
|
---|
105 | Rewrite(Soubor);
|
---|
106 | Write(Soubor, Row);
|
---|
107 | CloseFile(Soubor);
|
---|
108 | finally
|
---|
109 | end;
|
---|
110 |
|
---|
111 | // Load from file
|
---|
112 | try
|
---|
113 | AssignFile(Soubor, NetworkListFilename);
|
---|
114 | Reset(Soubor);
|
---|
115 | SetLength(Devices, 1);
|
---|
116 | while not Eof(Soubor) do begin
|
---|
117 | ReadLn(Soubor, Row);
|
---|
118 | SetLength(Devices, Length(Devices) + 1);
|
---|
119 | with Devices[High(Devices)] do begin
|
---|
120 | TryStrToInt(Parse,ID);
|
---|
121 | TryStrToInt(Parse, Parent);
|
---|
122 | Name := Parse;
|
---|
123 | IP := Parse;
|
---|
124 | //ShowMessage(IntToStr(networkInterfaceIndex));
|
---|
125 | //+' '+IntToStr(Length(NetworkInterfaces)));
|
---|
126 | //if IP = ActiveNetworkInterface.IPAddress then
|
---|
127 | // LocalPCID := ID;
|
---|
128 | Place := Parse;
|
---|
129 | TryStrToInt(Row, Sort);
|
---|
130 | end;
|
---|
131 | end;
|
---|
132 | finally
|
---|
133 | CloseFile(Soubor);
|
---|
134 | SpinEdit1.Value := MainForm.PingTimeout;
|
---|
135 | end;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TNetworkTest.FormShow(Sender: TObject);
|
---|
139 | begin
|
---|
140 | SpinEdit1.Value := MainForm.PingTimeout;
|
---|
141 | RefreshList;
|
---|
142 | PingList.Resume;
|
---|
143 | end;
|
---|
144 |
|
---|
145 | { TPingList }
|
---|
146 |
|
---|
147 | procedure TPingList.Execute;
|
---|
148 | var
|
---|
149 | I: Integer;
|
---|
150 | begin
|
---|
151 | with NetworkTest do begin
|
---|
152 | ListView1.SetFocus;
|
---|
153 | // ListView1.Items.BeginUpdate;
|
---|
154 | for I := 0 to ListView1.Items.Count - 1 do with ListView1.Items[I] do begin
|
---|
155 | Selected := True;
|
---|
156 | Focused := True;
|
---|
157 | if Ping(SubItems[0], MainForm.PingTimeout) then SubItems[2] := 'Ok' else SubItems[2] := 'Nedostupný';
|
---|
158 | Focused := False;
|
---|
159 | Selected := False;
|
---|
160 | if Terminated then Break;
|
---|
161 | end;
|
---|
162 | // ListView1.Items.EndUpdate;
|
---|
163 | Button2.Enabled := True;
|
---|
164 | ComboBox1.Enabled := True;
|
---|
165 | Button2.Caption := 'Otestovat';
|
---|
166 | end;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TNetworkTest.Button2Click(Sender: TObject);
|
---|
170 | begin
|
---|
171 | if Button2.Caption = 'Otestovat' then
|
---|
172 | begin
|
---|
173 | Button2.Caption := 'Pøeruit';
|
---|
174 | ComboBox1.Enabled := False;
|
---|
175 | ListView1.SetFocus;
|
---|
176 | PingList.Free;
|
---|
177 | PingList := TPingList.Create(True);
|
---|
178 | PingList.Resume;
|
---|
179 | end else begin
|
---|
180 | PingList.Terminate;
|
---|
181 | Button2.Caption := 'Otestovat';
|
---|
182 | Button2.Enabled := False;
|
---|
183 | end;
|
---|
184 | end;
|
---|
185 |
|
---|
186 | procedure TNetworkTest.RefreshList;
|
---|
187 | var
|
---|
188 | I: Integer;
|
---|
189 | Item: TListItem;
|
---|
190 | begin
|
---|
191 | ListView1.Items.BeginUpdate;
|
---|
192 | ListView1.Items.Clear;
|
---|
193 | case ComboBox1.ItemIndex of
|
---|
194 | 0: begin
|
---|
195 | // Show only devices between this computer and internet
|
---|
196 | I := IndexByName(GetComputerNetName);
|
---|
197 | while (I <> 0) and (I < High(Devices)) do with Devices[I] do begin
|
---|
198 | Item := ListView1.Items.Add;
|
---|
199 | Item.Caption := Name;
|
---|
200 | with Item.SubItems do begin
|
---|
201 | Add(IP);
|
---|
202 | Add(Place);
|
---|
203 | Add('');
|
---|
204 | end;
|
---|
205 | I := IndexByID(Devices[I].Parent);
|
---|
206 | end;
|
---|
207 | end;
|
---|
208 | 1: begin
|
---|
209 | // Show hosts
|
---|
210 | for I := 1 to High(Devices) do with Devices[I] do
|
---|
211 | if Sort = 0 then
|
---|
212 | begin
|
---|
213 | Item := ListView1.Items.Add;
|
---|
214 | Item.Caption := Name;
|
---|
215 | with Item.SubItems do begin
|
---|
216 | Add(IP);
|
---|
217 | Add(Place);
|
---|
218 | Add('');
|
---|
219 | end;
|
---|
220 | end;
|
---|
221 | end;
|
---|
222 | 2: begin
|
---|
223 | // Show only devices
|
---|
224 | for I := 1 to High(Devices) do with Devices[I] do
|
---|
225 | if Sort = 1 then
|
---|
226 | begin
|
---|
227 | Item := ListView1.Items.Add;
|
---|
228 | Item.Caption := Name;
|
---|
229 | with Item.SubItems do begin
|
---|
230 | Add(IP);
|
---|
231 | Add(Place);
|
---|
232 | Add('');
|
---|
233 | end;
|
---|
234 | end;
|
---|
235 | end;
|
---|
236 | 3: begin
|
---|
237 | // Show all items
|
---|
238 | for I := 1 to High(Devices) do with Devices[I] do begin
|
---|
239 | Item := ListView1.Items.Add;
|
---|
240 | Item.Caption := Name;
|
---|
241 | with Item.SubItems do begin
|
---|
242 | Add(IP);
|
---|
243 | Add(Place);
|
---|
244 | Add('');
|
---|
245 | end;
|
---|
246 | end;
|
---|
247 | end;
|
---|
248 | end;
|
---|
249 | ListView1.Items.EndUpdate;
|
---|
250 | if ListView1.Items.Count > 4 then Height := ListView1.Items.Count * 15 + 97
|
---|
251 | else Height := 4 * 15 + 97;
|
---|
252 | if Height > (Screen.Height - 50) then Height := Screen.Height - 50;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TNetworkTest.CheckBox1Click(Sender: TObject);
|
---|
256 | begin
|
---|
257 | RefreshList;
|
---|
258 | end;
|
---|
259 |
|
---|
260 | procedure TNetworkTest.SpinEdit1Change(Sender: TObject);
|
---|
261 | begin
|
---|
262 | MainForm.PingTimeout := SpinEdit1.Value;
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TNetworkTest.ComboBox1Change(Sender: TObject);
|
---|
266 | begin
|
---|
267 | RefreshList;
|
---|
268 | end;
|
---|
269 |
|
---|
270 | function TNetworkTest.IndexByID(WantedID: Integer): Integer;
|
---|
271 | var
|
---|
272 | I: Integer;
|
---|
273 | begin
|
---|
274 | for I := 0 to High(Devices) do if Devices[I].ID = WantedID then Break;
|
---|
275 | Result := I;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | function TNetworkTest.IndexByName(Name: string): Integer;
|
---|
279 | begin
|
---|
280 | Name := UpperCase(Name);
|
---|
281 | for Result := 0 to High(Devices) do
|
---|
282 | if Devices[Result].Name = Name then Break;
|
---|
283 | end;
|
---|
284 |
|
---|
285 | procedure TNetworkTest.FormClose(Sender: TObject;
|
---|
286 | var Action: TCloseAction);
|
---|
287 | begin
|
---|
288 | MainForm.PingTimeout := SpinEdit1.Value;
|
---|
289 | end;
|
---|
290 |
|
---|
291 | end.
|
---|