Repository navigation
Expand file tree
/
Copy pathComponent.cs
More file actions
331 lines (277 loc) · 9.24 KB
/
Copy pathComponent.cs
File metadata and controls
331 lines (277 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
// Big TODO's:
//
// Auto wire unconnected Errors component port to default ConsoleWriter or settable property
// Allowing components to execute during Fixed/Late Update
// ? Change all exceptions to write to Errors port
// ? Figure out IP ownership and tracking/disposing
// ? Array Outputs
// ? Auto wire unconnected output ports to Drop component
namespace NTransit {
[InputPort("AUTO")]
[OutputPort("AUTO")]
[OutputPort("Errors")]
public abstract class Component {
protected class PendingPacket {
public string Port;
public InformationPacket Ip;
public PendingPacket(string port, InformationPacket ip) {
Port = port;
Ip = ip;
}
}
public enum ProcessStatus {
Unstarted,
Active,
Blocked,
Terminated,
}
public string Name { get; protected set; }
public ProcessStatus Status { get; protected set; }
public bool AutoStart {
get {
var hasAutoStartAttribute = false;
foreach (var attribute in GetType().GetCustomAttributes(true)) {
if (attribute is AutoStartAttribute) {
hasAutoStartAttribute = true;
}
}
var allInputPortsHaveInitialData = true;
foreach (var kvp in InPorts) {
if (kvp.Key == "AUTO") {
continue;
}
if (!kvp.Value.HasInitialData) {
allInputPortsHaveInitialData = false;
}
}
return hasAutoStartAttribute || allInputPortsHaveInitialData;
}
}
public bool HasInputPacketWaiting {
get {
foreach (var kvp in InPorts) {
if (kvp.Value.HasPacketWaiting) {
return true;
}
}
return false;
}
}
public bool HasOutputPacketWaiting { get { return pendingPackets.Count > 0; } }
protected bool DoNotTerminateWhenInputPortsAreClosed { get; set; }
protected Dictionary<string, IInputPort> InPorts { get; private set; }
protected Dictionary<string, IOutputPort> OutPorts { get; private set; }
protected Queue<PendingPacket> pendingPackets;
protected Dictionary<string, Stack<string>> sequenceIds;
protected Component(string name) {
Name = name;
pendingPackets = new Queue<PendingPacket>();
sequenceIds = new Dictionary<string, Stack<string>>();
Status = ProcessStatus.Unstarted;
InPorts = new Dictionary<string, IInputPort>();
OutPorts = new Dictionary<string, IOutputPort>();
CreatePorts();
}
public abstract void Setup();
public void SetInputPort(string name, IInputPort port) {
port.Name = name;
InPorts[name] = port;
InPorts[name].SequenceStart = data => data.Accept();
InPorts[name].SequenceEnd = data => data.Accept();
}
public void SetOutputPort(string name, IOutputPort port) {
port.Name = name;
OutPorts[name] = port;
}
public void ConnectTo(string outPortName, Component process, string inPortName) {
ValidateOutputPortName(outPortName);
process.AddConnectBetween(OutPorts[outPortName], inPortName);
}
public void ConnectTo(string outPortName, Component process, string inPortName, int capacity) {
ValidateOutputPortName(outPortName);
process.AddConnectBetween(OutPorts[outPortName], inPortName, capacity);
}
public void SetInitialData(string portName, object value) {
SetInitialData(portName, new InformationPacket(value));
}
public void SetInitialData(string portName, InformationPacket ip) {
ValidateInputPortName(portName);
InPorts[portName].SetInitialData(ip);
}
public void Startup() {
Status = ProcessStatus.Active;
Start();
}
public void Shutdown() {
End();
if (OutPorts["AUTO"].Connected) {
Send("AUTO", new InformationPacket(null, InformationPacket.PacketType.Auto));
}
foreach (var port in InPorts.Values) {
port.Close();
}
foreach (var port in OutPorts.Values) {
port.Close();
}
}
public bool Tick() {
PendingPacket firstPacket = null;
while (pendingPackets.Count > 0 && firstPacket != pendingPackets.Peek()) {
var pendingPacket = pendingPackets.Dequeue();
if (!OutPorts[pendingPacket.Port].ConnectedPortClosed && !OutPorts[pendingPacket.Port].TrySend(pendingPacket.Ip, true)) {
if (firstPacket == null) {
firstPacket = pendingPacket;
}
pendingPackets.Enqueue(pendingPacket);
}
}
if ((!DoNotTerminateWhenInputPortsAreClosed && AllUpstreamPortsAreClosed()) || AllDownstreamPortsAreClosed()) {
Status = ProcessStatus.Terminated;
}
if (Status == ProcessStatus.Terminated) {
return false;
}
if (pendingPackets.Count > 0) {
Status = ProcessStatus.Blocked;
return false;
}
else {
var packetWasSentThisTick = false;
if (HasInputPacketWaiting) {
Status = ProcessStatus.Active;
foreach (var kvp in InPorts) {
packetWasSentThisTick = kvp.Value.Tick() || packetWasSentThisTick;
}
}
return Update() || packetWasSentThisTick;
}
}
protected virtual void Start() { }
protected virtual bool Update() { return false; }
protected virtual void End() { }
protected void SendNew(string port, object o) {
Send(port, new InformationPacket(o));
}
protected void SendNew(string port, object o, Dictionary<string, object> attributes) {
Send(port, new InformationPacket(o, attributes));
}
protected void Send(string port, InformationPacket ip) {
ValidateOutputPortName(port);
if (!OutPorts[port].TrySend(ip)) {
pendingPackets.Enqueue(new PendingPacket(port, ip));
}
}
protected bool TrySend(string port, InformationPacket ip) {
ValidateOutputPortName(port);
return OutPorts[port].TrySend(ip);
}
protected void SendSequenceStart(string port) {
var id = Guid.NewGuid().ToString();
if (!sequenceIds.ContainsKey(port)) sequenceIds[port] = new Stack<string>();
sequenceIds[port].Push(id);
Send(port, new InformationPacket(id, InformationPacket.PacketType.StartSequence));
}
protected void SendSequenceEnd(string port) {
if (sequenceIds[port].Count == 0) throw new InvalidOperationException(string.Format("No sequences are active for '{0}.{1}'", Name, port));
Send(port, new InformationPacket(sequenceIds[port].Pop(), InformationPacket.PacketType.EndSequence));
}
protected bool OutportIsConnected(string port) {
return OutPorts[port].Connected;
}
protected bool HasCapacity(string port) {
return OutPorts[port].HasCapacity;
}
protected void CreatePorts() {
foreach (var attribute in GetType().GetCustomAttributes(true)) {
if (attribute is InputPortAttribute) {
var inPort = attribute as InputPortAttribute;
var type = typeof(StandardInputPort);
if (inPort.Type != null) {
if (typeof(IInputPort).IsAssignableFrom(inPort.Type)) {
type = inPort.Type;
}
else {
throw new ArgumentException(string.Format("The InputPort type '{0}' specified on port '{1}' of component '{2}' does not implement IInputPort", inPort.Type, inPort.Name, GetType()));
}
}
var port = Activator.CreateInstance(type, new object[] { inPort.Capacity, this }) as IInputPort;
SetInputPort(inPort.Name, port);
}
else if (attribute is OutputPortAttribute) {
var outPort = attribute as OutputPortAttribute;
var type = typeof(StandardOutputPort);
if (outPort.Type != null) {
if (typeof(IOutputPort).IsAssignableFrom(outPort.Type)) {
type = outPort.Type;
}
else {
throw new ArgumentException(string.Format("The OutputPort type '{0}' specified on port '{1}' of component '{2}' does not implement IOutputPort", outPort.Type, outPort.Name, GetType()));
}
}
var port = Activator.CreateInstance(type, new object[] { this }) as IOutputPort;
SetOutputPort(outPort.Name, port);
}
}
}
protected bool AllUpstreamPortsAreClosed() {
var anyConnected = false;
foreach (var port in InPorts.Values) {
if (port.Connected) {
anyConnected = true;
}
}
if (!anyConnected) {
return false;
}
else {
var allClosed = true;
foreach (var port in InPorts.Values) {
if (port.Connected && !port.AllUpstreamPortsClosed) {
allClosed = false;
}
}
return allClosed;
}
}
protected bool AllDownstreamPortsAreClosed() {
var anyConnected = false;
foreach (var port in OutPorts.Values) {
if (port.Connected) {
anyConnected = true;
}
}
if (!anyConnected) {
return false;
}
else {
var allClosed = OutPorts.Count > 0;
foreach (var port in OutPorts.Values) {
if (port.Connected && !port.ConnectedPortClosed) {
allClosed = false;
}
}
return allClosed;
}
}
protected void ValidateInputPortName(string portName) {
if (!InPorts.ContainsKey(portName)) throw new ArgumentException(string.Format("There is no in port named '{0}.{1}'", Name, portName));
}
protected void ValidateOutputPortName(string portName) {
if (!OutPorts.ContainsKey(portName)) throw new ArgumentException(string.Format("There is no out port named '{0}.{1}'", Name, portName));
}
void AddConnectBetween(IOutputPort outPort, string inputPortName) {
ValidateInputPortName(inputPortName);
var inPort = InPorts[inputPortName];
outPort.ConnectTo(inPort);
inPort.NotifyOfConnection(outPort);
}
void AddConnectBetween(IOutputPort port, string inputPortName, int capacity) {
AddConnectBetween(port, inputPortName);
InPorts[inputPortName].ConnectionCapacity = capacity;
}
}
}