-
Notifications
You must be signed in to change notification settings - Fork 1
/
viz.go
198 lines (166 loc) · 4.13 KB
/
viz.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
package streamingviz
import (
"encoding/json"
"fmt"
"sync"
)
const (
NodeIdle = iota // 0
NodeBroadcasting // 1
NodeConsuming // 2
NodeRelaying // 3
)
const (
LinkSending = iota
LinkReceiveing
)
const (
initialNetworkSize = 50
initialStreams = 10
)
type Node struct {
ID string
Group map[string]int // For a string streamID, what group is it in
}
func (self *Node) GroupForStream(streamID string) int {
val, ok := self.Group[streamID]
if ok == false {
self.Group[streamID] = NodeIdle
return NodeIdle
}
return val
}
type Link struct {
Source *Node
Target *Node
Value map[string]int // For a string streamID, what Value does it have
}
type Network struct {
Nodes map[string]*Node
Links []*Link
StreamIDs []string // All known streams
lock sync.Mutex
}
func NewNetwork() *Network {
return &Network{
Nodes: make(map[string]*Node),
Links: make([]*Link, 0),
StreamIDs: make([]string, initialStreams),
}
}
// Does the node exist in the network already
func (self *Network) hasNode(id string) bool {
_, ok := self.Nodes[id]
return ok
}
// Does the link exist in the network already
func (self *Network) hasLink(src string, target string) bool {
for _, link := range self.Links {
if (link.Source.ID == src && link.Target.ID == target) ||
(link.Target.ID == src && link.Source.ID == target) {
return true
}
}
return false
}
func (self *Network) addNode(id string) {
self.Nodes[id] = &Node{
ID: id,
Group: make(map[string]int),
}
}
func (self *Network) addLink(src string, target string) {
sNode, ok1 := self.findNode(src)
tNode, ok2 := self.findNode(target)
if ok1 && ok2 {
self.Links = append(self.Links, &Link{
Source: sNode,
Target: tNode,
Value: make(map[string]int),
})
}
}
func (self *Network) findNode(id string) (*Node, bool) {
node, ok := self.Nodes[id]
return node, ok
}
// Reconstructs the links array removing links for this node
func (self *Network) removeLinksForNode(nodeID string) {
node, ok := self.findNode(nodeID)
if ok {
links := make([]*Link, 0)
for _, link := range self.Links {
if link.Source != node && link.Target != node {
links = append(links, link)
}
}
self.Links = links
}
}
// Messages that may be received
// 1. Node sends its peers - can construct the peer graph
// 2. Node says it's publishing a stream
// 3. Node says it's requesting a stream
// 4. Node says it's relaying a stream
// 5. Finish publishing, requesting, relaying. TBD whether this is explicit or on a timeout
// Add this node to the network and add its peers as links. Remove existing links first to keep state consistent
func (self *Network) ReceivePeersForNode(nodeID string, peerIDs []string) {
self.lock.Lock()
defer self.lock.Unlock()
if !self.hasNode(nodeID) {
self.addNode(nodeID)
fmt.Println("Adding node:", nodeID)
}
self.removeLinksForNode(nodeID)
for _, p := range peerIDs {
if !self.hasNode(p) {
self.addNode(p)
fmt.Println("Adding node from peer:", p)
}
if !self.hasLink(nodeID, p) {
self.addLink(nodeID, p)
fmt.Println("Adding link", nodeID, p)
}
}
}
func (self *Network) StartBroadcasting(nodeID string, streamID string) {
self.lock.Lock()
defer self.lock.Unlock()
node, ok := self.findNode(nodeID)
if ok {
node.Group[streamID] = NodeBroadcasting
}
}
func (self *Network) StartConsuming(nodeID string, streamID string) {
self.lock.Lock()
defer self.lock.Unlock()
node, ok := self.findNode(nodeID)
if ok {
node.Group[streamID] = NodeConsuming
}
}
func (self *Network) StartRelaying(nodeID string, streamID string) {
self.lock.Lock()
defer self.lock.Unlock()
node, ok := self.findNode(nodeID)
if ok {
node.Group[streamID] = NodeRelaying
}
}
func (self *Network) DoneWithStream(nodeID string, streamID string) {
self.lock.Lock()
defer self.lock.Unlock()
node, ok := self.findNode(nodeID)
if ok {
node.Group[streamID] = NodeIdle
}
}
func (self *Network) String() string {
self.lock.Lock()
defer self.lock.Unlock()
b, err := json.Marshal(self)
if err != nil {
return fmt.Sprintf("Error creating json from this network %v", err)
}
return fmt.Sprintf("%s", b)
}