source: PinConnection/CommHub.pas

Last change on this file was 575, checked in by chronos, 5 months ago
  • Modified: Remove U prefix from unit names of PinConnection package.
File size: 2.3 KB
Line 
1unit CommHub;
2
3interface
4
5uses
6 Classes, SysUtils, Contnrs, CommPin, SpecializedList;
7
8type
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
34implementation
35
36{ TPinList }
37
38function TPinList.Add(AObject: TObject): Integer;
39begin
40 Result := inherited Add(AObject);
41 TCommPin(AObject).OnReceive := Hub.Receive;
42 TCommPin(AObject).OnSetSatus := Hub.SetStatus;
43end;
44
45function TPinList.AddNew: TCommPin;
46begin
47 Result := TCommPin(Items[Add(TCommPin.Create)]);
48 Result.Node := Hub;
49end;
50
51function TPinList.Extract(Item: TObject): TObject;
52begin
53 TCommPin(Item).OnReceive := nil;
54 TCommPin(Item).OnSetSatus := nil;
55 Result := inherited Extract(Item);
56end;
57
58procedure TPinList.Insert(Index: Integer; AObject: TObject);
59begin
60 inherited Insert(Index, AObject);
61 TCommPin(AObject).OnReceive := Hub.Receive;
62 TCommPin(AObject).OnSetSatus := Hub.SetStatus;
63end;
64
65{ TCommHub }
66
67procedure TCommHub.Receive(Sender: TCommPin; Stream: TListByte);
68var
69 I: Integer;
70begin
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;
77end;
78
79procedure TCommHub.SetStatus(Sender: TCommPin; Status: Integer);
80var
81 I: Integer;
82begin
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;
89end;
90
91constructor TCommHub.Create(AOwner: TComponent);
92begin
93 inherited;
94 FPins := TPinList.Create;
95 FPins.Hub := Self;
96end;
97
98destructor TCommHub.Destroy;
99begin
100 Active := False;
101 FreeAndNil(FPins);
102 inherited;
103end;
104
105end.
106
Note: See TracBrowser for help on using the repository browser.