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