-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (145 loc) · 4.21 KB
/
main.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
package main
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/evscott/Distributed-RST/Node"
)
// main is the entry point for this distributed system.
func main() {
ipAddr, err := net.InterfaceAddrs()
if err != nil {
fmt.Print(err)
return
}
ip := strings.Split(ipAddr[0].String(), "/")[0]
runExample(ip)
}
// displayNeighbors prints to standard output the neighbors belonging to a set of nodes.
func displayNeighbors(nodeArr ...*Node.Info) {
for _, node := range nodeArr {
if port, err := strconv.Atoi(node.Port); err == nil {
p := port % 1000
fmt.Printf("Node %d: neighbors: {", p)
for _, neighbour := range node.Neighbours {
if c, err := strconv.Atoi(neighbour); err == nil {
c := c % 1000
fmt.Printf("%d,", c)
}
}
fmt.Printf("}\n")
}
}
}
// displayNeighbors prints to standard output the children belonging to a set of nodes.
func displayChildren(nodeArr ...*Node.Info) {
for _, node := range nodeArr {
if port, err := strconv.Atoi(node.Port); err == nil {
p := port % 1000
fmt.Printf("Node %d: children: {", p)
for child, _ := range node.Children {
if c, err := strconv.Atoi(child); err == nil {
c := c % 1000
fmt.Printf("%d,", c)
}
}
fmt.Printf("}\n")
}
}
}
// displayRSTAdjMatrix prints to standard output the adjacency matrix
// representation of a rooted spanning tree given a set of nodes.
func displayRSTAdjMatrix(nodeArr ...*Node.Info) {
size := len(nodeArr)
adjMatrix := make([][]uint8, size)
for i := range adjMatrix {
adjMatrix[i] = make([]uint8, size)
}
for _, node := range nodeArr {
if port, err := strconv.Atoi(node.Port); err == nil {
p := port % 1000
for child, _ := range node.Children {
if c, err := strconv.Atoi(child); err == nil {
c := c % 1000
adjMatrix[c][p] = 1
}
}
}
}
printMatrix(size, adjMatrix)
}
// displayCGAdjMatrix prints to standard output the adjacency matrix
// representation of a communication graph given a set of nodes.
func displayCGAdjMatrix(nodeArr ...*Node.Info) {
size := len(nodeArr)
adjMatrix := make([][]uint8, size)
for i := range adjMatrix {
adjMatrix[i] = make([]uint8, size)
}
for _, node := range nodeArr {
if port, err := strconv.Atoi(node.Port); err == nil {
p := port % 1000
for _, neighbour := range node.Neighbours {
if c, err := strconv.Atoi(neighbour); err == nil {
c := c % 1000
adjMatrix[c][p] = 1
adjMatrix[p][c] = 1
}
}
}
}
printMatrix(size, adjMatrix)
}
func printMatrix(size int, adjMatrix [][]uint8) {
for i := 0; i < size; i++ {
fmt.Printf("[")
for j := 0; j < size; j++ {
if j == size-1 {
fmt.Printf("%d", adjMatrix[i][j])
} else {
fmt.Printf("%d,", adjMatrix[i][j])
}
}
fmt.Printf("]\n")
}
}
// runExample creates a rooted spanning tree from an arbitrary communication graph and display it.
//
// This example borrows the example put forward by Raynal in `Distributed Algorithm's for Message Passing Systems, but
// substitutes letters for numbers in the identification of nodes.
// `a` = `8000`
// `b` = `8001`
// `c` = `8002`
// ... etc
func runExample(ip string) {
n1 := Node.Create(ip, "8000", []string{"8001", "8006", "8007"})
n2 := Node.Create(ip, "8001", []string{"8000", "8002", "8007"})
n3 := Node.Create(ip, "8002", []string{"8001", "8003"})
n4 := Node.Create(ip, "8003", []string{"8002", "8007", "8004", "8005"})
n5 := Node.Create(ip, "8004", []string{"8005", "8003"})
n6 := Node.Create(ip, "8005", []string{"8003", "8004", "8006"})
n7 := Node.Create(ip, "8006", []string{"8000", "8007", "8003", "8005"})
n8 := Node.Create(ip, "8007", []string{"8007", "8000", "8001", "8003", "8006"})
go n1.ListenOnPort()
go n2.ListenOnPort()
go n3.ListenOnPort()
go n4.ListenOnPort()
go n5.ListenOnPort()
go n6.ListenOnPort()
go n7.ListenOnPort()
go n8.ListenOnPort()
time.Sleep(time.Second / 10)
fmt.Println()
n1.Start()
time.Sleep(time.Second)
fmt.Printf("\nCommunication graph: \n\n")
displayNeighbors(n1, n2, n3, n4, n5, n6, n7, n8)
fmt.Println()
displayCGAdjMatrix(n1, n2, n3, n4, n5, n6, n7, n7)
fmt.Printf("\nRooted spanning tree: \n\n")
displayChildren(n1, n2, n3, n4, n5, n6, n7, n8)
fmt.Println()
displayRSTAdjMatrix(n1, n2, n3, n4, n5, n6, n7, n8)
}