-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.go
59 lines (53 loc) · 1.4 KB
/
main2.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
package main
import (
"elevatorlib/network/peers"
"flag"
"fmt"
)
func main() {
id := "0"
flag.StringVar(&id, "id", "0", "id of this elevator")
flag.Parse()
masterMode := true
peerChan := make(chan peers.PeerUpdate)
peerEnable := make(chan bool)
fmt.Println("Starting reciever")
go peers.Receiver(1000, peerChan)
fmt.Println("Starting transmitter")
go peers.Transmitter(1000, id, peerEnable)
for {
select {
case peerUpdate := <-peerChan:
fmt.Printf("Peer update:\n")
fmt.Printf(" Peers: %q\n", peerUpdate.Peers)
fmt.Printf(" New: %q\n", peerUpdate.New)
fmt.Printf(" Lost: %q\n", peerUpdate.Lost)
if len(peerUpdate.Peers) == 1 && peerUpdate.Peers[0] != id {
fmt.Println("only one peer that is not me ignoring")
} else if len(peerUpdate.Peers) == 1 && peerUpdate.Peers[0] == id {
fmt.Println("I am the only peer")
fmt.Println("")
if len(peerUpdate.Lost) > 0 && peerUpdate.Peers[0] == id {
fmt.Println("Im master")
masterMode = true
}
} else if peerUpdate.Peers[0] == id {
if !masterMode {
masterMode = true
fmt.Println("I am the new master")
} else {
fmt.Println("I am still master")
}
} else {
if masterMode {
masterMode = false
fmt.Println("I am no longer master")
} else {
fmt.Println("I am still slave")
}
}
case enable := <-peerEnable:
fmt.Println("enable", enable)
}
}
}