| 1 | unit CommHub;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Contnrs, CommPin, SpecializedList;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TCommHub = class;
|
|---|
| 10 |
|
|---|
| 11 | { TPinList }
|
|---|
| 12 |
|
|---|
| 13 | TPinList = class(TObjectList)
|
|---|
| 14 | Hub: TCommHub;
|
|---|
| 15 | function Add(AObject: TObject): Integer;
|
|---|
| 16 | function AddNew: TCommPin;
|
|---|
| 17 | function Extract(Item: TObject): TObject;
|
|---|
| 18 | procedure Insert(Index: Integer; AObject: TObject);
|
|---|
| 19 | end;
|
|---|
| 20 |
|
|---|
| 21 | { TCommHub }
|
|---|
| 22 |
|
|---|
| 23 | TCommHub = class(TCommNode)
|
|---|
| 24 | private
|
|---|
| 25 | FPins: TPinList;
|
|---|
| 26 | procedure Receive(Sender: TCommPin; Stream: TListByte);
|
|---|
| 27 | procedure SetStatus(Sender: TCommPin; Status: Integer);
|
|---|
| 28 | public
|
|---|
| 29 | constructor Create(AOwner: TComponent); override;
|
|---|
| 30 | destructor Destroy; override;
|
|---|
| 31 | property Pins: TPinList read FPins write FPins;
|
|---|
| 32 | end;
|
|---|
| 33 |
|
|---|
| 34 | implementation
|
|---|
| 35 |
|
|---|
| 36 | { TPinList }
|
|---|
| 37 |
|
|---|
| 38 | function TPinList.Add(AObject: TObject): Integer;
|
|---|
| 39 | begin
|
|---|
| 40 | Result := inherited Add(AObject);
|
|---|
| 41 | TCommPin(AObject).OnReceive := Hub.Receive;
|
|---|
| 42 | TCommPin(AObject).OnSetSatus := Hub.SetStatus;
|
|---|
| 43 | end;
|
|---|
| 44 |
|
|---|
| 45 | function TPinList.AddNew: TCommPin;
|
|---|
| 46 | begin
|
|---|
| 47 | Result := TCommPin(Items[Add(TCommPin.Create)]);
|
|---|
| 48 | Result.Node := Hub;
|
|---|
| 49 | end;
|
|---|
| 50 |
|
|---|
| 51 | function TPinList.Extract(Item: TObject): TObject;
|
|---|
| 52 | begin
|
|---|
| 53 | TCommPin(Item).OnReceive := nil;
|
|---|
| 54 | TCommPin(Item).OnSetSatus := nil;
|
|---|
| 55 | Result := inherited Extract(Item);
|
|---|
| 56 | end;
|
|---|
| 57 |
|
|---|
| 58 | procedure TPinList.Insert(Index: Integer; AObject: TObject);
|
|---|
| 59 | begin
|
|---|
| 60 | inherited Insert(Index, AObject);
|
|---|
| 61 | TCommPin(AObject).OnReceive := Hub.Receive;
|
|---|
| 62 | TCommPin(AObject).OnSetSatus := Hub.SetStatus;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | { TCommHub }
|
|---|
| 66 |
|
|---|
| 67 | procedure TCommHub.Receive(Sender: TCommPin; Stream: TListByte);
|
|---|
| 68 | var
|
|---|
| 69 | I: Integer;
|
|---|
| 70 | begin
|
|---|
| 71 | if FActive then begin
|
|---|
| 72 | // Broadcast received packet to all other pins
|
|---|
| 73 | for I := 0 to FPins.Count - 1 do
|
|---|
| 74 | if Sender <> FPins[I] then
|
|---|
| 75 | TCommPin(FPins[I]).Send(Stream);
|
|---|
| 76 | end;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | procedure TCommHub.SetStatus(Sender: TCommPin; Status: Integer);
|
|---|
| 80 | var
|
|---|
| 81 | I: Integer;
|
|---|
| 82 | begin
|
|---|
| 83 | if FActive then begin
|
|---|
| 84 | // Broadcast received packet to all other pins
|
|---|
| 85 | for I := 0 to FPins.Count - 1 do
|
|---|
| 86 | if Sender <> FPins[I] then
|
|---|
| 87 | TCommPin(FPins[I]).Status := Status;
|
|---|
| 88 | end;
|
|---|
| 89 | end;
|
|---|
| 90 |
|
|---|
| 91 | constructor TCommHub.Create(AOwner: TComponent);
|
|---|
| 92 | begin
|
|---|
| 93 | inherited;
|
|---|
| 94 | FPins := TPinList.Create;
|
|---|
| 95 | FPins.Hub := Self;
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | destructor TCommHub.Destroy;
|
|---|
| 99 | begin
|
|---|
| 100 | Active := False;
|
|---|
| 101 | FreeAndNil(FPins);
|
|---|
| 102 | inherited;
|
|---|
| 103 | end;
|
|---|
| 104 |
|
|---|
| 105 | end.
|
|---|
| 106 |
|
|---|