source: PinConnection/UCommHub.pas

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