Changeset 3 for trunk


Ignore:
Timestamp:
Sep 16, 2016, 10:49:27 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Network address in each network interface should be full address not just single integer. Yet need to specify network subnets for routing.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.lfm

    r2 r3  
    11object FormMain: TFormMain
    2   Left = 458
     2  Left = 262
    33  Height = 964
    4   Top = 362
     4  Top = 185
    55  Width = 1759
    66  Caption = 'ISP game'
     
    1010  OnDestroy = FormDestroy
    1111  OnShow = FormShow
     12  Position = poScreenCenter
    1213  LCLVersion = '1.7'
     14  WindowState = wsMaximized
    1315  object PaintBox1: TPaintBox
    1416    Left = 0
  • trunk/Forms/UFormMain.pas

    r2 r3  
    5959  with Network do begin
    6060    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;
    6565    NodeSwitch := AddSwitch('switch', Point(200, 200), 4);
    6666    NodeSwitch2 := AddSwitch('switch2', Point(700, 200), 4);
    6767    NodeGeorge := AddHost('george', Point(100, 100), 1, Log);
    68     TNetworkInterface(NodeGeorge.Interfaces[0]).Address := 2;
     68    NodeGeorge.Interfaces[0].Address := NetworkAddress([2]);;
    6969    NodeFranta := AddHost('franta', Point(200, 100), 1, Log);
    70     TNetworkInterface(NodeFranta.Interfaces[0]).Address := 3;
     70    NodeFranta.Interfaces[0].Address := NetworkAddress([3]);;
    7171    NodeHonza := AddHost('honza', Point(300, 100), 1, Log);
    72     TNetworkInterface(NodeHonza.Interfaces[0]).Address := 4;
     72    NodeHonza.Interfaces[0].Address := NetworkAddress([4]);;
    7373    NodeJakub := AddHost('jakub', Point(600, 100), 1, Log);
    74     TNetworkInterface(NodeJakub.Interfaces[0]).Address := 2;
     74    NodeJakub.Interfaces[0].Address := NetworkAddress([2]);;
    7575    NodeMonika := AddHost('monika', Point(700, 100), 1, Log);
    76     TNetworkInterface(NodeMonika.Interfaces[0]).Address := 3;
     76    NodeMonika.Interfaces[0].Address := NetworkAddress([3]);;
    7777
    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]);
    8181
    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]);
    8484
    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]);
    8787
    8888    NodeGeorge.Ping(NetworkAddress([1, 4]));
  • trunk/UNetwork.pas

    r2 r3  
    1919    class operator Equal(A, B: TNetworkAddress): Boolean;
    2020    function GetString: string;
     21    procedure Clear;
    2122  end;
    2223
     
    4445  public
    4546    Node: TNetworkNode;
    46     Address: Integer;
     47    Address: TNetworkAddress;
    4748    Network: Integer;
    4849    procedure Connect(Remote: TNetworkInterface);
     
    5051    procedure SendPacket(Packet: TNetworkPacket);
    5152    constructor Create;
     53    destructor Destroy; override;
    5254    property Remote: TNetworkInterface read FRemote
    5355      write SetRemote;
     
    112114function NetworkAddress(AParts: array of Integer): TNetworkAddress;
    113115
     116
    114117implementation
    115118
     
    121124  for I := 0 to High(Result.Parts) do
    122125    Result.Parts[I] := AParts[I];
    123   Result.Context := 0;
    124126end;
    125127
     
    142144  Result := '';
    143145  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 + '.';
    146147    Result := Result + IntToStr(Parts[I]);
    147148  end;
     149end;
     150
     151procedure TNetworkAddress.Clear;
     152begin
     153  SetLength(Parts, 0);
    148154end;
    149155
     
    155161  I: Integer;
    156162begin
    157   if Packet.Destination = NetworkAddress([Sender.Address]) then
     163  if Packet.Destination = Sender.Address then
    158164    DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString)
    159165  else begin
    160166    if Length(Packet.Destination.Parts) > Packet.Destination.Context then
    161167    for I := 0 to Interfaces.Count - 1 do
    162     with TNetworkInterface(Interfaces[I]) do begin
     168    with Interfaces[I] do begin
    163169      if Network = Packet.Destination.Parts[Packet.Destination.Context + 1] then begin
    164170        //Inc
     
    177183begin
    178184  for I := 0 to Interfaces.Count - 1 do
    179     if TNetworkInterface(Interfaces[I]) <> Sender then
    180       TNetworkInterface(Interfaces[I]).SendPacket(Packet);
     185    if Interfaces[I] <> Sender then
     186      Interfaces[I].SendPacket(Packet);
    181187end;
    182188
     
    263269  R = 30;
    264270begin
    265   Canvas.Pen.Color := clBlack;
     271  Canvas.Pen.Color := clRed;
    266272  Canvas.Pen.Style := psSolid;
    267273
     
    277283    for J := 0 to Interfaces.Count - 1 do
    278284    if Assigned(Interfaces[J]) then begin
    279       if Assigned(TNetworkInterface(Interfaces[J]).Remote) then begin
    280         RemoteInterface := TNetworkInterface(Interfaces[J]).Remote;
     285      if Assigned(Interfaces[J].Remote) then begin
     286        RemoteInterface := Interfaces[J].Remote;
    281287        RemoteNode := RemoteInterface.Node;
    282288        RemoteInterfaceIndex := RemoteNode.Interfaces.IndexOf(RemoteInterface);
    283289        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);
    285292      end;
    286293    end;
     
    290297    for J := 0 to Interfaces.Count - 1 do
    291298    if Assigned(Interfaces[J]) then begin
    292       if TNetworkInterface(Interfaces[J]).Address > 0 then
    293       Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(IntToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,
    294         Position.Y - Canvas.TextHeight(IntToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,
    295         IntToStr(TNetworkInterface(Interfaces[J]).Address));
    296       if TNetworkInterface(Interfaces[J]).Network > 0 then
    297       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));
    300307    end;
    301308  end;
     
    312319  Packet: TNetworkPacket);
    313320begin
    314   if Packet.Destination = NetworkAddress([Sender.Address]) then
     321  if Packet.Destination = Sender.Address then
    315322    DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString);
    316323end;
     
    328335  UseInterface := TNetworkInterface(Interfaces[0]);
    329336  NewPacket := TNetworkPacket.Create;
    330   NewPacket.Source := NetworkAddress(UseInterface.Address);
     337  NewPacket.Source := UseInterface.Address;
    331338  NewPacket.Destination := Address;
    332339  NewPacket.Data := 'ping';
     
    340347  Result := TNetworkInterface.Create;
    341348  Result.Node := Self;
    342   Result.Address := 0;
     349  Result.Address.Clear;
    343350  Result.OnReceivePacket := ReceivePacket;
    344351  Interfaces.Add(Result);
     
    401408begin
    402409  FRemote := nil;
    403   Address := 0;
     410  Address.Clear;
     411end;
     412
     413destructor TNetworkInterface.Destroy;
     414begin
     415  inherited Destroy;
    404416end;
    405417
Note: See TracChangeset for help on using the changeset viewer.