-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathport.go
More file actions
241 lines (205 loc) · 6.21 KB
/
port.go
File metadata and controls
241 lines (205 loc) · 6.21 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
package flowbase
import (
"fmt"
"sync"
)
// ------------------------------------------------------------------------
// InPort
// ------------------------------------------------------------------------
// InPort represents a pluggable connection to multiple out-ports from other
// processes, from its own process, and with which it is communicating via
// channels under the hood
type InPort struct {
Chan chan *Packet
name string
process Node
RemotePorts map[string]*OutPort
ready bool
closeLock sync.Mutex
}
// NewInPort returns a new InPort struct
func NewInPort(name string) *InPort {
inp := &InPort{
name: name,
RemotePorts: map[string]*OutPort{},
Chan: make(chan *Packet, getBufsize()), // This one will contain merged inputs from inChans
ready: false,
}
return inp
}
// Name returns the name of the InPort
func (pt *InPort) Name() string {
return pt.name
}
// Process returns the process connected to the port
func (pt *InPort) Process() Node {
if pt.process == nil {
pt.Fail("No connected process!")
}
return pt.process
}
// SetProcess sets the process of the port to p
func (pt *InPort) SetProcess(p Node) {
pt.process = p
}
// AddRemotePort adds a remote OutPort to the InPort
func (pt *InPort) AddRemotePort(rpt *OutPort) {
if pt.RemotePorts[rpt.Name()] != nil {
pt.Failf("A remote port with name (%s) already exists", rpt.Name())
}
pt.RemotePorts[rpt.Name()] = rpt
}
// From connects an OutPort to the InPort
func (pt *InPort) From(rpt *OutPort) {
pt.AddRemotePort(rpt)
rpt.AddRemotePort(pt)
pt.SetReady(true)
rpt.SetReady(true)
}
// Disconnect disconnects the (out-)port with name rptName, from the InPort
func (pt *InPort) Disconnect(rptName string) {
pt.removeRemotePort(rptName)
if len(pt.RemotePorts) == 0 {
pt.SetReady(false)
}
}
// removeRemotePort removes the (out-)port with name rptName, from the InPort
func (pt *InPort) removeRemotePort(rptName string) {
if _, ok := pt.RemotePorts[rptName]; !ok {
pt.Failf("No remote port with name (%s) exists", rptName)
}
delete(pt.RemotePorts, rptName)
}
// SetReady sets the ready status of the InPort
func (pt *InPort) SetReady(ready bool) {
pt.ready = ready
}
// Ready tells whether the port is ready or not
func (pt *InPort) Ready() bool {
return pt.ready
}
// Send sends IPs to the in-port, and is supposed to be called from the remote
// (out-) port, to send to this in-port
func (pt *InPort) Send(ip *Packet) {
pt.Chan <- ip
}
// Recv receives IPs from the port
func (pt *InPort) Recv() *Packet {
return <-pt.Chan
}
// CloseConnection closes the connection to the remote out-port with name
// rptName, on the InPort
func (pt *InPort) CloseConnection(rptName string) {
pt.closeLock.Lock()
delete(pt.RemotePorts, rptName)
if len(pt.RemotePorts) == 0 {
close(pt.Chan)
}
pt.closeLock.Unlock()
}
// Failf fails with a message that includes the process name
func (pt *InPort) Failf(msg string, parts ...interface{}) {
pt.Fail(fmt.Sprintf(msg, parts...))
}
// Fail fails with a message that includes the process name
func (pt *InPort) Fail(msg interface{}) {
Failf("[In-Port:%s] %s", pt.Name(), msg)
}
// ------------------------------------------------------------------------
// OutPort
// ------------------------------------------------------------------------
// OutPort represents a pluggable connection to multiple in-ports from other
// processes, from its own process, and with which it is communicating via
// channels under the hood
type OutPort struct {
name string
process Node
RemotePorts map[string]*InPort
ready bool
}
// NewOutPort returns a new OutPort struct
func NewOutPort(name string) *OutPort {
outp := &OutPort{
name: name,
RemotePorts: map[string]*InPort{},
ready: false,
}
return outp
}
// Name returns the name of the OutPort
func (pt *OutPort) Name() string {
return pt.name
}
// Process returns the process connected to the port
func (pt *OutPort) Process() Node {
if pt.process == nil {
pt.Fail("No connected process!")
}
return pt.process
}
// SetProcess sets the process of the port to p
func (pt *OutPort) SetProcess(p Node) {
pt.process = p
}
// AddRemotePort adds a remote InPort to the OutPort
func (pt *OutPort) AddRemotePort(rpt *InPort) {
if _, ok := pt.RemotePorts[rpt.Name()]; ok {
pt.Failf("A remote port with name (%s) already exists", rpt.Name())
}
pt.RemotePorts[rpt.Name()] = rpt
}
// removeRemotePort removes the (in-)port with name rptName, from the OutPort
func (pt *OutPort) removeRemotePort(rptName string) {
if _, ok := pt.RemotePorts[rptName]; !ok {
pt.Failf("No remote port with name (%s) exists", rptName)
}
delete(pt.RemotePorts, rptName)
}
// To connects an InPort to the OutPort
func (pt *OutPort) To(rpt *InPort) {
pt.AddRemotePort(rpt)
rpt.AddRemotePort(pt)
pt.SetReady(true)
rpt.SetReady(true)
}
// Disconnect disconnects the (in-)port with name rptName, from the OutPort
func (pt *OutPort) Disconnect(rptName string) {
pt.removeRemotePort(rptName)
if len(pt.RemotePorts) == 0 {
pt.SetReady(false)
}
}
// SetReady sets the ready status of the OutPort
func (pt *OutPort) SetReady(ready bool) {
pt.ready = ready
}
// Ready tells whether the port is ready or not
func (pt *OutPort) Ready() bool {
return pt.ready
}
// Send sends an Packet to all the in-ports connected to the OutPort
func (pt *OutPort) Send(data any) {
for _, rpt := range pt.RemotePorts {
Debug.Printf("Sending on out-port (%s) connected to in-port (%s)", pt.Name(), rpt.Name())
ip := NewPacket(data)
rpt.Send(ip)
}
}
// Close closes the connection between this port and all the ports it is
// connected to. If this port is the last connected port to an in-port, that
// in-ports channel will also be closed.
func (pt *OutPort) Close() {
for _, rpt := range pt.RemotePorts {
Debug.Printf("Closing out-port (%s) connected to in-port (%s)", pt.Name(), rpt.Name())
rpt.CloseConnection(pt.Name())
pt.removeRemotePort(rpt.Name())
}
}
// Failf fails with a message that includes the process name
func (pt *OutPort) Failf(msg string, parts ...interface{}) {
pt.Fail(fmt.Sprintf(msg, parts...))
}
// Fail fails with a message that includes the process name
func (pt *OutPort) Fail(msg interface{}) {
Failf("[Out-Port:%s] %s", pt.Name(), msg)
}