| 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 |
|
|---|
| 9 | type
|
|---|
| 10 | TPingList = class(TThread)
|
|---|
| 11 | private
|
|---|
| 12 | { Private declarations }
|
|---|
| 13 | protected
|
|---|
| 14 | procedure Execute; override;
|
|---|
| 15 | end;
|
|---|
| 16 |
|
|---|
| 17 | TNetworkTest = class(TForm)
|
|---|
| 18 | Button1: TButton;
|
|---|
| 19 | ListView1: TListView;
|
|---|
| 20 | Button2: TButton;
|
|---|
| 21 | SpinEdit1: TSpinEdit;
|
|---|
| 22 | Label1: TLabel;
|
|---|
| 23 | ComboBox1: TComboBox;
|
|---|
| 24 | procedure Button1Click(Sender: TObject);
|
|---|
| 25 | procedure FormCreate(Sender: TObject);
|
|---|
| 26 | procedure FormShow(Sender: TObject);
|
|---|
| 27 | procedure Button2Click(Sender: TObject);
|
|---|
| 28 | procedure CheckBox1Click(Sender: TObject);
|
|---|
| 29 | procedure SpinEdit1Change(Sender: TObject);
|
|---|
| 30 | procedure ComboBox1Change(Sender: TObject);
|
|---|
| 31 | private
|
|---|
| 32 | { Private declarations }
|
|---|
| 33 | public
|
|---|
| 34 | PingList: TPingList;
|
|---|
| 35 | Devices: array of record
|
|---|
| 36 | ID: Integer;
|
|---|
| 37 | IP: string;
|
|---|
| 38 | Name: string;
|
|---|
| 39 | Place: string;
|
|---|
| 40 | Parent: Integer;
|
|---|
| 41 | Sort: Integer;
|
|---|
| 42 | end;
|
|---|
| 43 | LocalPCID: Integer;
|
|---|
| 44 | procedure RefreshList;
|
|---|
| 45 | function IndexByID(WantedID: Integer): Integer;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 | var
|
|---|
| 49 | NetworkTest: TNetworkTest;
|
|---|
| 50 |
|
|---|
| 51 | implementation
|
|---|
| 52 |
|
|---|
| 53 | uses UMainWindow, ULocalization, USunriseChatCore;
|
|---|
| 54 |
|
|---|
| 55 | {$R *.dfm}
|
|---|
| 56 |
|
|---|
| 57 | procedure TNetworkTest.Button1Click(Sender: TObject);
|
|---|
| 58 | begin
|
|---|
| 59 | Close;
|
|---|
| 60 | end;
|
|---|
| 61 |
|
|---|
| 62 | procedure TNetworkTest.FormCreate(Sender: TObject);
|
|---|
| 63 |
|
|---|
| 64 | var
|
|---|
| 65 | Soubor: TextFile;
|
|---|
| 66 | Row: string;
|
|---|
| 67 |
|
|---|
| 68 | function Parse: string;
|
|---|
| 69 | begin
|
|---|
| 70 | Result:= Copy(Row,1,Pos(':',Row)-1);
|
|---|
| 71 | Delete(Row,1,Length(Result)+1);
|
|---|
| 72 | end;
|
|---|
| 73 |
|
|---|
| 74 | begin
|
|---|
| 75 | if MainWindow.OnTop then FormStyle:= fsStayOnTop else FormStyle:= fsNormal;
|
|---|
| 76 | try
|
|---|
| 77 | PingList:= TPingList.Create(True);
|
|---|
| 78 | ChDir(ExtractFileDir(Application.ExeName));
|
|---|
| 79 | AssignFile(Soubor,'Network.cfg');
|
|---|
| 80 | Reset(Soubor);
|
|---|
| 81 | SetLength(Devices,1);
|
|---|
| 82 | while not Eof(Soubor) do begin
|
|---|
| 83 | ReadLn(Soubor,Row);
|
|---|
| 84 | SetLength(Devices,Length(Devices)+1);
|
|---|
| 85 | with SunriseChatCore, Devices[High(Devices)] do begin
|
|---|
| 86 | TryStrToInt(Parse,ID);
|
|---|
| 87 | TryStrToInt(Parse,Parent);
|
|---|
| 88 | Name:= Parse;
|
|---|
| 89 | IP:= Parse;
|
|---|
| 90 | if IP = NetworkInterfaces[NetworkInterfaceIndex].IPAddress then
|
|---|
| 91 | LocalPCID:= ID;
|
|---|
| 92 | Place:= Parse;
|
|---|
| 93 | TryStrToInt(Row,Sort);
|
|---|
| 94 | end;
|
|---|
| 95 | end;
|
|---|
| 96 | finally
|
|---|
| 97 | CloseFile(Soubor);
|
|---|
| 98 | SpinEdit1.Value:= MainWindow.PingTimeout;
|
|---|
| 99 | end;
|
|---|
| 100 | end;
|
|---|
| 101 |
|
|---|
| 102 | procedure TNetworkTest.FormShow(Sender: TObject);
|
|---|
| 103 | begin
|
|---|
| 104 | ComboBox1.Visible:= MainWindow.ServiceFunction;
|
|---|
| 105 | RefreshList;
|
|---|
| 106 | PingList.Resume;
|
|---|
| 107 | with Localization do begin
|
|---|
| 108 | Button2.Caption:= Item('PerformTest');
|
|---|
| 109 | Button1.Caption:= Item('Close');
|
|---|
| 110 | //CheckBox1.Caption:= Item('ShowAllHosts');
|
|---|
| 111 | NetworkTest.Caption:= Item('NetworkTest');
|
|---|
| 112 | ListView1.Columns[0].Caption:= Item('Host');
|
|---|
| 113 | ListView1.Columns[1].Caption:= Item('IPAddress');
|
|---|
| 114 | ListView1.Columns[2].Caption:= Item('Location');
|
|---|
| 115 | ListView1.Columns[3].Caption:= Item('Status');
|
|---|
| 116 | end;
|
|---|
| 117 | end;
|
|---|
| 118 |
|
|---|
| 119 | { TPingList }
|
|---|
| 120 |
|
|---|
| 121 | procedure TPingList.Execute;
|
|---|
| 122 | var
|
|---|
| 123 | I: Integer;
|
|---|
| 124 | begin
|
|---|
| 125 | with NetworkTest do begin
|
|---|
| 126 | ListView1.SetFocus;
|
|---|
| 127 | // ListView1.Items.BeginUpdate;
|
|---|
| 128 | for I:= 0 to ListView1.Items.Count-1 do with ListView1.Items[I] do begin
|
|---|
| 129 | Selected:= True;
|
|---|
| 130 | Focused:= True;
|
|---|
| 131 | if Ping(SubItems[0],MainWindow.PingTimeout) then SubItems[2]:= 'Ok' else SubItems[2]:= 'Nedostupný';
|
|---|
| 132 | Focused:= False;
|
|---|
| 133 | Selected:= False;
|
|---|
| 134 | if Terminated then Break;
|
|---|
| 135 | end;
|
|---|
| 136 | // ListView1.Items.EndUpdate;
|
|---|
| 137 | Button2.Enabled:= True;
|
|---|
| 138 | Button2.Caption:= Localization.Item('PerformTest');
|
|---|
| 139 | ComboBox1.Enabled:= True;
|
|---|
| 140 | end;
|
|---|
| 141 | end;
|
|---|
| 142 |
|
|---|
| 143 | procedure TNetworkTest.Button2Click(Sender: TObject);
|
|---|
| 144 | begin
|
|---|
| 145 | with Localization do
|
|---|
| 146 | if Button2.Caption = Item('PerformTest') then begin
|
|---|
| 147 | Button2.Caption:= Item('Break');
|
|---|
| 148 | ComboBox1.Enabled:= False;
|
|---|
| 149 | ListView1.SetFocus;
|
|---|
| 150 | PingList.Free;
|
|---|
| 151 | PingList:= TPingList.Create(True);
|
|---|
| 152 | PingList.Resume;
|
|---|
| 153 | end else begin
|
|---|
| 154 | PingList.Terminate;
|
|---|
| 155 | Button2.Caption:= Item('PerformTest');
|
|---|
| 156 | Button2.Enabled:= False;
|
|---|
| 157 | end;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | procedure TNetworkTest.RefreshList;
|
|---|
| 161 | var
|
|---|
| 162 | I: Integer;
|
|---|
| 163 | Item: TListItem;
|
|---|
| 164 | begin
|
|---|
| 165 | ListView1.Items.BeginUpdate;
|
|---|
| 166 | ListView1.Items.Clear;
|
|---|
| 167 | case ComboBox1.ItemIndex of
|
|---|
| 168 | 0: begin
|
|---|
| 169 | // Show only devices between this computer and internet
|
|---|
| 170 | I:= IndexByID(LocalPCID);
|
|---|
| 171 | while I<>0 do with Devices[I] do begin
|
|---|
| 172 | Item:= ListView1.Items.Add;
|
|---|
| 173 | Item.Caption:= Name;
|
|---|
| 174 | with Item.SubItems do begin
|
|---|
| 175 | Add(IP);
|
|---|
| 176 | Add(Place);
|
|---|
| 177 | Add('');
|
|---|
| 178 | end;
|
|---|
| 179 | I:= IndexByID(Devices[I].Parent);
|
|---|
| 180 | end;
|
|---|
| 181 | end;
|
|---|
| 182 | 1: begin
|
|---|
| 183 | // Show hosts
|
|---|
| 184 | for I:= 1 to High(Devices) do with Devices[I] do
|
|---|
| 185 | if Sort = 0 then
|
|---|
| 186 | begin
|
|---|
| 187 | Item:= ListView1.Items.Add;
|
|---|
| 188 | Item.Caption:= Name;
|
|---|
| 189 | with Item.SubItems do begin
|
|---|
| 190 | Add(IP);
|
|---|
| 191 | Add(Place);
|
|---|
| 192 | Add('');
|
|---|
| 193 | end;
|
|---|
| 194 | end;
|
|---|
| 195 | end;
|
|---|
| 196 | 2: begin
|
|---|
| 197 | // Show only devices
|
|---|
| 198 | for I:= 1 to High(Devices) do with Devices[I] do
|
|---|
| 199 | if Sort = 1 then
|
|---|
| 200 | begin
|
|---|
| 201 | Item:= ListView1.Items.Add;
|
|---|
| 202 | Item.Caption:= Name;
|
|---|
| 203 | with Item.SubItems do begin
|
|---|
| 204 | Add(IP);
|
|---|
| 205 | Add(Place);
|
|---|
| 206 | Add('');
|
|---|
| 207 | end;
|
|---|
| 208 | end;
|
|---|
| 209 | end;
|
|---|
| 210 | 3: begin
|
|---|
| 211 | // Show all items
|
|---|
| 212 | for I:= 1 to High(Devices) do with Devices[I] do 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 | end;
|
|---|
| 223 | ListView1.Items.EndUpdate;
|
|---|
| 224 | if ListView1.Items.Count > 4 then Height:= ListView1.Items.Count*14 + 97
|
|---|
| 225 | else Height:= 4*14 + 97;
|
|---|
| 226 | if Height > (Screen.Height-50) then Height:= (Screen.Height-50);
|
|---|
| 227 | end;
|
|---|
| 228 |
|
|---|
| 229 | procedure TNetworkTest.CheckBox1Click(Sender: TObject);
|
|---|
| 230 | begin
|
|---|
| 231 | RefreshList;
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | procedure TNetworkTest.SpinEdit1Change(Sender: TObject);
|
|---|
| 235 | begin
|
|---|
| 236 | MainWindow.PingTimeout:= SpinEdit1.Value;
|
|---|
| 237 | end;
|
|---|
| 238 |
|
|---|
| 239 | procedure TNetworkTest.ComboBox1Change(Sender: TObject);
|
|---|
| 240 | begin
|
|---|
| 241 | RefreshList;
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | function TNetworkTest.IndexByID(WantedID: Integer): Integer;
|
|---|
| 245 | var
|
|---|
| 246 | I: Integer;
|
|---|
| 247 | begin
|
|---|
| 248 | for I:= 0 to High(Devices) do if Devices[I].ID = WantedID then Break;
|
|---|
| 249 | Result:= I;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | end.
|
|---|