Changeset 3
- Timestamp:
- Sep 16, 2016, 10:49:27 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.lfm
r2 r3 1 1 object FormMain: TFormMain 2 Left = 4582 Left = 262 3 3 Height = 964 4 Top = 3624 Top = 185 5 5 Width = 1759 6 6 Caption = 'ISP game' … … 10 10 OnDestroy = FormDestroy 11 11 OnShow = FormShow 12 Position = poScreenCenter 12 13 LCLVersion = '1.7' 14 WindowState = wsMaximized 13 15 object PaintBox1: TPaintBox 14 16 Left = 0 -
trunk/Forms/UFormMain.pas
r2 r3 59 59 with Network do begin 60 60 NodeRouter := AddRouter('router', Point(400, 300), 2, Log); 61 TNetworkInterface(NodeRouter.Interfaces[0]).Address := 1;62 TNetworkInterface(NodeRouter.Interfaces[0]).Network := 3;63 TNetworkInterface(NodeRouter.Interfaces[1]).Address := 8;64 TNetworkInterface(NodeRouter.Interfaces[1]).Network := 4;61 NodeRouter.Interfaces[0].Address := NetworkAddress([1]); 62 NodeRouter.Interfaces[0].Network := 3; 63 NodeRouter.Interfaces[1].Address := NetworkAddress([8]);; 64 NodeRouter.Interfaces[1].Network := 4; 65 65 NodeSwitch := AddSwitch('switch', Point(200, 200), 4); 66 66 NodeSwitch2 := AddSwitch('switch2', Point(700, 200), 4); 67 67 NodeGeorge := AddHost('george', Point(100, 100), 1, Log); 68 TNetworkInterface(NodeGeorge.Interfaces[0]).Address := 2;68 NodeGeorge.Interfaces[0].Address := NetworkAddress([2]);; 69 69 NodeFranta := AddHost('franta', Point(200, 100), 1, Log); 70 TNetworkInterface(NodeFranta.Interfaces[0]).Address := 3;70 NodeFranta.Interfaces[0].Address := NetworkAddress([3]);; 71 71 NodeHonza := AddHost('honza', Point(300, 100), 1, Log); 72 TNetworkInterface(NodeHonza.Interfaces[0]).Address := 4;72 NodeHonza.Interfaces[0].Address := NetworkAddress([4]);; 73 73 NodeJakub := AddHost('jakub', Point(600, 100), 1, Log); 74 TNetworkInterface(NodeJakub.Interfaces[0]).Address := 2;74 NodeJakub.Interfaces[0].Address := NetworkAddress([2]);; 75 75 NodeMonika := AddHost('monika', Point(700, 100), 1, Log); 76 TNetworkInterface(NodeMonika.Interfaces[0]).Address := 3;76 NodeMonika.Interfaces[0].Address := NetworkAddress([3]);; 77 77 78 TNetworkInterface(NodeFranta.Interfaces[0]).Connect(TNetworkInterface(NodeSwitch.Interfaces[1]));79 TNetworkInterface(NodeGeorge.Interfaces[0]).Connect(TNetworkInterface(NodeSwitch.Interfaces[0]));80 TNetworkInterface(NodeHonza.Interfaces[0]).Connect(TNetworkInterface(NodeSwitch.Interfaces[2]));78 NodeFranta.Interfaces[0].Connect(NodeSwitch.Interfaces[1]); 79 NodeGeorge.Interfaces[0].Connect(NodeSwitch.Interfaces[0]); 80 NodeHonza.Interfaces[0].Connect(NodeSwitch.Interfaces[2]); 81 81 82 TNetworkInterface(NodeSwitch.Interfaces[3]).Connect(TNetworkInterface(NodeRouter.Interfaces[0]));83 TNetworkInterface(NodeSwitch2.Interfaces[0]).Connect(TNetworkInterface(NodeRouter.Interfaces[1]));82 NodeSwitch.Interfaces[3].Connect(NodeRouter.Interfaces[0]); 83 NodeSwitch2.Interfaces[0].Connect(NodeRouter.Interfaces[1]); 84 84 85 TNetworkInterface(NodeJakub.Interfaces[0]).Connect(TNetworkInterface(NodeSwitch2.Interfaces[1]));86 TNetworkInterface(NodeMonika.Interfaces[0]).Connect(TNetworkInterface(NodeSwitch2.Interfaces[2]));85 NodeJakub.Interfaces[0].Connect(NodeSwitch2.Interfaces[1]); 86 NodeMonika.Interfaces[0].Connect(NodeSwitch2.Interfaces[2]); 87 87 88 88 NodeGeorge.Ping(NetworkAddress([1, 4])); -
trunk/UNetwork.pas
r2 r3 19 19 class operator Equal(A, B: TNetworkAddress): Boolean; 20 20 function GetString: string; 21 procedure Clear; 21 22 end; 22 23 … … 44 45 public 45 46 Node: TNetworkNode; 46 Address: Integer;47 Address: TNetworkAddress; 47 48 Network: Integer; 48 49 procedure Connect(Remote: TNetworkInterface); … … 50 51 procedure SendPacket(Packet: TNetworkPacket); 51 52 constructor Create; 53 destructor Destroy; override; 52 54 property Remote: TNetworkInterface read FRemote 53 55 write SetRemote; … … 112 114 function NetworkAddress(AParts: array of Integer): TNetworkAddress; 113 115 116 114 117 implementation 115 118 … … 121 124 for I := 0 to High(Result.Parts) do 122 125 Result.Parts[I] := AParts[I]; 123 Result.Context := 0;124 126 end; 125 127 … … 142 144 Result := ''; 143 145 for I := 0 to Length(Parts) - 1 do begin 144 if I = Context then Result := Result + ';' 145 else if I > 0 then Result := Result + '.'; 146 if I > 0 then Result := Result + '.'; 146 147 Result := Result + IntToStr(Parts[I]); 147 148 end; 149 end; 150 151 procedure TNetworkAddress.Clear; 152 begin 153 SetLength(Parts, 0); 148 154 end; 149 155 … … 155 161 I: Integer; 156 162 begin 157 if Packet.Destination = NetworkAddress([Sender.Address])then163 if Packet.Destination = Sender.Address then 158 164 DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString) 159 165 else begin 160 166 if Length(Packet.Destination.Parts) > Packet.Destination.Context then 161 167 for I := 0 to Interfaces.Count - 1 do 162 with TNetworkInterface(Interfaces[I])do begin168 with Interfaces[I] do begin 163 169 if Network = Packet.Destination.Parts[Packet.Destination.Context + 1] then begin 164 170 //Inc … … 177 183 begin 178 184 for I := 0 to Interfaces.Count - 1 do 179 if TNetworkInterface(Interfaces[I])<> Sender then180 TNetworkInterface(Interfaces[I]).SendPacket(Packet);185 if Interfaces[I] <> Sender then 186 Interfaces[I].SendPacket(Packet); 181 187 end; 182 188 … … 263 269 R = 30; 264 270 begin 265 Canvas.Pen.Color := cl Black;271 Canvas.Pen.Color := clRed; 266 272 Canvas.Pen.Style := psSolid; 267 273 … … 277 283 for J := 0 to Interfaces.Count - 1 do 278 284 if Assigned(Interfaces[J]) then begin 279 if Assigned( TNetworkInterface(Interfaces[J]).Remote) then begin280 RemoteInterface := TNetworkInterface(Interfaces[J]).Remote;285 if Assigned(Interfaces[J].Remote) then begin 286 RemoteInterface := Interfaces[J].Remote; 281 287 RemoteNode := RemoteInterface.Node; 282 288 RemoteInterfaceIndex := RemoteNode.Interfaces.IndexOf(RemoteInterface); 283 289 Canvas.Line(Position.X + J * R - R * Interfaces.Count div 2 + R div 2, Position.Y, 284 RemoteInterface.Node.Position.X + RemoteInterfaceIndex * R - R * RemoteNode.Interfaces.Count div 2 + R div 2, RemoteNode.Position.Y); 290 RemoteInterface.Node.Position.X + RemoteInterfaceIndex * R - R * 291 RemoteNode.Interfaces.Count div 2 + R div 2, RemoteNode.Position.Y); 285 292 end; 286 293 end; … … 290 297 for J := 0 to Interfaces.Count - 1 do 291 298 if Assigned(Interfaces[J]) then begin 292 if TNetworkInterface(Interfaces[J]).Address > 0 then293 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(Int ToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,294 Position.Y - Canvas.TextHeight(Int ToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,295 Int ToStr(TNetworkInterface(Interfaces[J]).Address));296 if TNetworkInterface(Interfaces[J]).Network > 0 then297 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(IntToStr( TNetworkInterface(Interfaces[J]).Network)) div 2,298 Position.Y - R - Canvas.TextHeight(IntToStr( TNetworkInterface(Interfaces[J]).Network)) div 2,299 IntToStr( TNetworkInterface(Interfaces[J]).Network));299 //if Interfaces[J].Address > 0 then 300 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(Interfaces[J].Address.GetString) div 2, 301 Position.Y - Canvas.TextHeight(Interfaces[J].Address.GetString) div 2, 302 Interfaces[J].Address.GetString); 303 //if Interfaces[J].Network > 0 then 304 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(IntToStr(Interfaces[J].Network)) div 2, 305 Position.Y - R - Canvas.TextHeight(IntToStr(Interfaces[J].Network)) div 2, 306 IntToStr(Interfaces[J].Network)); 300 307 end; 301 308 end; … … 312 319 Packet: TNetworkPacket); 313 320 begin 314 if Packet.Destination = NetworkAddress([Sender.Address])then321 if Packet.Destination = Sender.Address then 315 322 DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString); 316 323 end; … … 328 335 UseInterface := TNetworkInterface(Interfaces[0]); 329 336 NewPacket := TNetworkPacket.Create; 330 NewPacket.Source := NetworkAddress(UseInterface.Address);337 NewPacket.Source := UseInterface.Address; 331 338 NewPacket.Destination := Address; 332 339 NewPacket.Data := 'ping'; … … 340 347 Result := TNetworkInterface.Create; 341 348 Result.Node := Self; 342 Result.Address := 0;349 Result.Address.Clear; 343 350 Result.OnReceivePacket := ReceivePacket; 344 351 Interfaces.Add(Result); … … 401 408 begin 402 409 FRemote := nil; 403 Address := 0; 410 Address.Clear; 411 end; 412 413 destructor TNetworkInterface.Destroy; 414 begin 415 inherited Destroy; 404 416 end; 405 417
Note:
See TracChangeset
for help on using the changeset viewer.