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