source: trunk/UNetworkTest.pas

Last change on this file was 5, checked in by george, 15 years ago
  • Odstraněno: Funkce a ovládací prvky související s vytáčením VPN.
File size: 7.1 KB
Line 
1unit UNetworkTest;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, UPing, ComCtrls, StdCtrls, cUtils, cWinSock, Spin,
8 IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
9
10const
11 NetworkListFile = 'NetworkList.txt';
12 NetworkListURL = 'http://centrala.zdechov.net/sunrisechat/network.php';
13
14type
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
56var
57 NetworkTest: TNetworkTest;
58
59implementation
60
61uses Math, UMainForm;
62
63{$R *.dfm}
64
65procedure TNetworkTest.Button1Click(Sender: TObject);
66begin
67 Close;
68end;
69
70procedure TNetworkTest.FormCreate(Sender: TObject);
71var
72 Soubor: TextFile;
73 Row: string;
74 Hostname: string;
75 LocalIp: string;
76 WSAErr: string;
77
78function Parse: string;
79begin
80 Result:= Copy(Row, 1, Pos(':', Row) - 1);
81 Delete(Row, 1, Length(Result) + 1);
82end;
83
84function NetworkListFilename: string;
85begin
86 Result := CommonAppDataPath + '\' + Creator + '\' + ApplicationName;
87 if not DirectoryExists(Result) then
88 ForceDirectories(Result);
89
90 Result := Result + '\' + NetworkListFile;
91end;
92
93begin
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;
136end;
137
138procedure TNetworkTest.FormShow(Sender: TObject);
139begin
140 SpinEdit1.Value := MainForm.PingTimeout;
141 RefreshList;
142 PingList.Resume;
143end;
144
145{ TPingList }
146
147procedure TPingList.Execute;
148var
149 I: Integer;
150begin
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;
167end;
168
169procedure TNetworkTest.Button2Click(Sender: TObject);
170 begin
171 if Button2.Caption = 'Otestovat' then
172 begin
173 Button2.Caption := 'Pøerušit';
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;
184end;
185
186procedure TNetworkTest.RefreshList;
187var
188 I: Integer;
189 Item: TListItem;
190begin
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;
253end;
254
255procedure TNetworkTest.CheckBox1Click(Sender: TObject);
256begin
257 RefreshList;
258end;
259
260procedure TNetworkTest.SpinEdit1Change(Sender: TObject);
261begin
262 MainForm.PingTimeout := SpinEdit1.Value;
263end;
264
265procedure TNetworkTest.ComboBox1Change(Sender: TObject);
266begin
267 RefreshList;
268end;
269
270function TNetworkTest.IndexByID(WantedID: Integer): Integer;
271var
272 I: Integer;
273begin
274 for I := 0 to High(Devices) do if Devices[I].ID = WantedID then Break;
275 Result := I;
276end;
277
278function TNetworkTest.IndexByName(Name: string): Integer;
279begin
280 Name := UpperCase(Name);
281 for Result := 0 to High(Devices) do
282 if Devices[Result].Name = Name then Break;
283end;
284
285procedure TNetworkTest.FormClose(Sender: TObject;
286 var Action: TCloseAction);
287begin
288 MainForm.PingTimeout := SpinEdit1.Value;
289end;
290
291end.
Note: See TracBrowser for help on using the repository browser.