-
Notifications
You must be signed in to change notification settings - Fork 8
/
hub.go
382 lines (309 loc) · 8.81 KB
/
hub.go
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package flowgraph
import (
"github.com/vectaport/fgbase"
)
// Hub interface for flowgraph hubs that are connected by flowgraph streams
type Hub interface {
// Name returns the hub name
Name() string
// SetName sets the hub name
SetName(name string)
// Tracef for debug trace printing. Uses atomic log mechanism.
Tracef(format string, v ...interface{})
// LogError for logging of error messages. Uses atomic log mechanism.
LogError(format string, v ...interface{})
// Panicf for logging of panic messages. Uses atomic log mechanism.
Panicf(format string, v ...interface{})
// Source returns source stream selected by string or int
Source(port interface{}) Stream
// Result returns result stream selected by string or int
Result(port interface{}) Stream
// SetSource sets a stream on a source port selected by string or int
SetSource(port interface{}, s Stream) Hub
// SetResult sets a stream on a result port selected by string or int
SetResult(port interface{}, s Stream) Hub
// AddSources adds a source port for each stream
AddSources(s ...Stream) Hub
// AddResults adds a result port for each stream
AddResults(s ...Stream) Hub
// NumSource returns the number of source ports
NumSource() int
// NumResult returns the number of result ports
NumResult() int
// SetNumSource sets the number of source ports
SetNumSource(n int) Hub
// SetNumResult sets the number of result ports
SetNumResult(n int) Hub
// SourceNames returns the names of the source ports
SourceNames() []string
// ResultNames returns the names of the result ports
ResultNames() []string
// SetSourceNames names the source ports
SetSourceNames(nm ...string) Hub
// SetResultNames names the result ports
SetResultNames(nm ...string) Hub
// SourceIndex returns the index of a source port selected by string or Stream
SourceIndex(port interface{}) int
// ResultIndex returns the index of a result port selected by string or Stream
ResultIndex(port interface{}) int
// ConnectSources connects a list of source Streams to this hub
ConnectSources(source ...Stream) Hub
// ConnectResults connects a list of result Streams to this hub
ConnectResults(result ...Stream) Hub
// HubCode returns code associated with hub.
HubCode() HubCode
// Empty returns true if the underlying implementation is nil
Empty() bool
// Flowgraph returns associated flowgraph
Flowgraph() Flowgraph
// Base returns value of underlying implementation
Base() interface{}
}
// Hub implementation
type hub struct {
base *fgbase.Node
fg *flowgraph
code HubCode
}
// Tracef for debug trace printing. Uses atomic log mechanism.
func (h *hub) Tracef(format string, v ...interface{}) {
h.base.Tracef(format, v...)
}
// LogError for logging of error messages. Uses atomic log mechanism.
func (h *hub) LogError(format string, v ...interface{}) {
h.base.LogError(format, v...)
}
// Panicf for logging of panic messages. Uses atomic log mechanism.
func (h *hub) Panicf(format string, v ...interface{}) {
h.base.Panicf(format, v...)
}
// Name returns the hub name
func (h *hub) Name() string {
return h.base.Name
}
// SetName returns the hub name
func (h *hub) SetName(name string) {
h.base.Name = name
}
// Source returns source stream selected by int or string
func (h *hub) Source(port interface{}) Stream {
var i int
var ok bool
switch v := port.(type) {
case string:
i = h.SourceIndex(v)
ok = i >= 0
case int:
ok = v >= 0 && v < h.NumSource()
i = v
default:
h.Panicf("Need string or int to select port on Hub %s to get source stream\n", h.Name())
}
if !ok {
h.Panicf("Source port %v not found on Hub %v\n", port, h.Name())
}
return &stream{h.base.Src(i), h.fg}
}
// Result returns result stream selected by int or string
func (h *hub) Result(port interface{}) Stream {
var i int
var ok bool
switch v := port.(type) {
case string:
i = h.ResultIndex(v)
ok = i >= 0
case int:
ok = v >= 0 && v < h.NumResult()
i = v
default:
h.Panicf("Need string or int to select port on Hub %s to get result stream\n", h.Name())
}
if !ok {
h.Panicf("Result port %v not found on Hub %v\n", port, h.Name())
}
return &stream{h.base.Dst(i), h.fg}
}
// SetSource sets a stream on a source port selected by string or int
func (h *hub) SetSource(port interface{}, s Stream) Hub {
checkInternalStream(h.Flowgraph(), s)
var i int
var ok bool
switch v := port.(type) {
case string:
i = h.SourceIndex(v)
ok = i >= 0
case int:
ok = v >= 0 && v < h.NumSource()
i = v
default:
h.Panicf("Need string or int to select port on Hub %s to set source stream\n", h.Name())
}
if !ok {
h.Panicf("Source port %v not found on Hub %v\n", port, h.Name())
}
if s != nil && s.Base() != nil {
e := *s.Base().(*fgbase.Edge)
h.base.SrcSet(i, &e)
}
return h
}
// SetResult sets a stream on a result port selected by string or int
func (h *hub) SetResult(port interface{}, s Stream) Hub {
checkInternalStream(h.Flowgraph(), s)
var i int
var ok bool
switch v := port.(type) {
case string:
i = h.ResultIndex(v)
ok = i >= 0
case int:
ok = v >= 0 && v < h.NumResult()
i = v
default:
h.Panicf("Need string or int to select result port on hub %s to set result stream\n", h.Name())
}
if !ok {
h.Panicf("Result port %v not found on Hub %v\n", port, h.Name())
}
e := *s.Base().(*fgbase.Edge)
h.base.DstSet(i, &e)
return h
}
// AddSources adds a source port for each stream
func (h *hub) AddSources(s ...Stream) Hub {
for _, sv := range s {
checkInternalStream(h.Flowgraph(), sv)
h.Base().(*fgbase.Node).SrcAppend(sv.Base().(*fgbase.Edge))
}
return h
}
// AddResults adds a result port for each stream
func (h *hub) AddResults(s ...Stream) Hub {
for _, sv := range s {
checkInternalStream(h.Flowgraph(), sv)
h.Base().(*fgbase.Node).DstAppend(sv.Base().(*fgbase.Edge))
}
return h
}
// NumSource returns the number of source ports
func (h *hub) NumSource() int {
return h.base.SrcCnt()
}
// NumResult returns the number of result ports
func (h *hub) NumResult() int {
return h.base.DstCnt()
}
// SetNumSource sets the number of source ports
func (h *hub) SetNumSource(n int) Hub {
h.base.SetSrcNum(n)
return h
}
// SetNumResult sets the number of result ports
func (h *hub) SetNumResult(n int) Hub {
h.base.SetDstNum(n)
return h
}
// SourceNames returns the names of the source ports
func (h *hub) SourceNames() []string {
return h.base.SrcNames()
}
// ResultNames returns the names of the result ports
func (h *hub) ResultNames() []string {
return h.base.DstNames()
}
// SetSourceNames names the source ports
func (h *hub) SetSourceNames(nm ...string) Hub {
h.base.SetSrcNames(nm...)
return h
}
// SetResultNames names the result ports
func (h *hub) SetResultNames(nm ...string) Hub {
h.base.SetDstNames(nm...)
return h
}
// SourceIndex returns the index of a source port selected by string or stream
func (h *hub) SourceIndex(port interface{}) int {
var i int
var ok bool
switch v := port.(type) {
case string:
i, ok = h.base.FindSrcIndex(v)
case int:
i, ok = v, true
case Stream:
for j := 0; j < h.NumSource(); j++ {
if v.Same(h.Source(j)) {
i, ok = j, true
break
}
}
if !ok {
h.Panicf("Source port for Stream %s not found on Hub %s\n", v.Name(), h.Name())
}
default:
h.Panicf("Need string, int or Stream to select port on Hub %s\n", h.Name())
}
if !ok {
h.Panicf("Source port %T(%+v) not found on Hub %s\n", port, port, h.Name())
}
return i
}
// ResultIndex returns the index of a result port selected by string or stream
func (h *hub) ResultIndex(port interface{}) int {
var i int
var ok bool
switch v := port.(type) {
case string:
i, ok = h.base.FindDstIndex(v)
case int:
i, ok = v, true
case Stream:
for j := 0; j < h.NumResult(); j++ {
if v.Same(h.Result(j)) {
i, ok = j, true
break
}
}
if !ok {
h.Panicf("Result port for Stream %s not found on Hub %s\n", v.Name(), h.Name())
}
default:
h.Panicf("Need string, int or Stream to select port on Hub %s\n", h.Name())
}
if !ok {
h.Panicf("Result port %T(%+v) not found on Hub %s\n", port, port, h.Name())
}
return i
}
// ConnectSources connects a list of source streams to this hub
func (h *hub) ConnectSources(source ...Stream) Hub {
h.SetNumSource(len(source))
for i, v := range source {
h.SetSource(i, v)
}
return h
}
// ConnectResults connects a list of result streams to this hub
func (h *hub) ConnectResults(result ...Stream) Hub {
h.SetNumResult(len(result))
for i, v := range result {
h.SetResult(i, v)
}
return h
}
// HubCode returns code associated with a hub
func (h *hub) HubCode() HubCode {
return h.code
}
// Empty returns true if the underlying implementation is nil
func (h *hub) Empty() bool {
return h.base == nil
}
// Flowgraph returns associated flowgraph interface
func (h *hub) Flowgraph() Flowgraph {
return h.fg
}
// Base returns value of underlying implementation
func (h *hub) Base() interface{} {
return h.base
}