-
Notifications
You must be signed in to change notification settings - Fork 1
/
flowbase.go
155 lines (122 loc) · 2.95 KB
/
flowbase.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
package flowbase
import (
"fmt"
)
var (
BUFSIZE = 128
)
// ----------------------------------------------------------------------------
// IP
// ----------------------------------------------------------------------------
type IP interface{}
type BaseIP struct {
data []byte
}
// ----------------------------------------------------------------------------
// Port
// ----------------------------------------------------------------------------
type Port interface {
Name() string
}
func NewBasePort(name string) *BasePort {
return &BasePort{
name: name,
remotePorts: make(map[string]Port),
channel: make(chan IP, BUFSIZE),
}
}
type BasePort struct {
name string
remotePorts map[string]Port
channel chan IP
}
func (bp *BasePort) Name() string {
return bp.name
}
func (bp *BasePort) Connect(rpt Port) {
bp.remotePorts[rpt.Name()] = rpt
}
// ----------------------------------------------------------------------------
// InPort
// ----------------------------------------------------------------------------
func NewInPort(name string) *InPort {
return &InPort{BasePort{name: name}}
}
type InPort struct {
BasePort
}
func (ip *InPort) From(op *OutPort) {
ip.Connect(op)
}
// ----------------------------------------------------------------------------
// OutPort
// ----------------------------------------------------------------------------
func NewOutPort(name string) *OutPort {
return &OutPort{
BasePort: *NewBasePort(name),
}
}
type OutPort struct {
BasePort
}
func (op *OutPort) To(ipt *OutPort) {
op.Connect(ipt)
}
func (op *OutPort) Send(ip IP) {}
func (op *OutPort) Close() {
close(op.channel)
}
// ----------------------------------------------------------------------------
// Process
// ----------------------------------------------------------------------------
type Process interface {
Run()
Name() string
}
func NewBaseProcess(name string) *BaseProcess {
return &BaseProcess{
name: name,
inPorts: make(map[string]*InPort),
outPorts: make(map[string]*OutPort),
}
}
type BaseProcess struct {
name string
inPorts map[string]*InPort
outPorts map[string]*OutPort
}
func (bp *BaseProcess) Name() string {
return bp.name
}
func (bp *BaseProcess) Run() {}
// ----------------------------------------------------------------------------
// Network
// ----------------------------------------------------------------------------
type Network interface {
Process
}
func NewBaseNetwork(name string) *BaseNetwork {
return &BaseNetwork{
BaseProcess: *NewBaseProcess(name),
processes: []Process{},
sink: NewBaseProcess("sink"),
}
}
type BaseNetwork struct {
BaseProcess
processes []Process
sink Process
}
func (n *BaseNetwork) Add(p Process) {
n.processes = append(n.processes, p)
}
func (n *BaseNetwork) Run() {
if len(n.processes) == 0 {
fmt.Printf("WARNING: No processes in network [%s]\n", n.Name())
return
}
for _, p := range n.processes {
go p.Run()
}
n.sink.Run()
}