-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
131 lines (107 loc) · 3.17 KB
/
listener.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
package main
import (
"bigw-voting/commands"
"bigw-voting/ui"
"bigw-voting/util"
"crypto/sha256"
"encoding/json"
"fmt"
"strconv"
"strings"
)
var receivedPeerShares = make(map[string][]int)
var receivedPeerOutputs = make(map[string]int)
// listener is the function primarily responsible for listening to and
// responding to messages.
func listener(v *Voter) {
for {
msg := <-v.Peer.Messages
msgSplit := strings.Split(string(msg), " ")
if msgSplit[0] == "VotepackVerify" {
localHash := sha256.Sum256(votepack.Export())
// Only respond to peer if there is a difference in votepack
if msgSplit[1] != string(localHash[:]) {
err := v.Peer.SendMessage([]byte("VotepackInvalid"))
if err != nil {
util.Errorf("Unable to send message to %v, %v\n", v.Peer.PeerAddress.IP.String(), err)
}
}
continue
}
if msgSplit[0] == "VotepackInvalid" {
ui.Stop()
panic(fmt.Errorf("peer %v is using a different votepack", v.Peer.PeerAddress.IP.String()))
}
if msgSplit[0] == "StatusUpdate" {
v.Status = strings.Join(msgSplit[1:], " ")
util.Infof("Peer %v is now %v\n", v.Peer.PeerAddress.IP.String(), v.Status)
ui.SetStatusOfPeer(v.Peer.PeerAddress.IP.String(), v.Status)
var dontBegin bool
for _, v := range allVoters {
if v.Status != "Voting Complete" {
util.Infoln("Peers are still voting")
// Don't begin BGW if peers have not finished voting
dontBegin = true
break
}
}
if dontBegin || localStatus != "Voting Complete" {
continue
}
// All peers have voted, proceed with BGW
go RunBGW()
continue
}
if msgSplit[0] == "YourShares" {
var shares []int
err := json.Unmarshal([]byte(strings.Join(msgSplit[1:], " ")), &shares)
if err != nil {
util.Errorln("Failed to unmarshal peer shares")
}
receivedPeerShares[v.Peer.PeerAddress.IP.String()] = shares
continue
}
if msgSplit[0] == "MyOutput" {
conv, err := strconv.Atoi(msgSplit[1])
if err != nil {
util.Errorln("Unable to parse output recieved from peer")
}
util.Errorf("Received output %v from %v\n", conv, v.Peer.PeerAddress.IP.String())
receivedPeerOutputs[v.Peer.PeerAddress.IP.String()] = conv
continue
}
if msgSplit[0] == "Nick" {
nick := strings.Join(msgSplit[1:], " ")
ui.SetNickOfPeer(v.Peer.PeerAddress.IP.String(), nick)
continue
}
if msgSplit[0] == "TrusteeVote" {
trustee := strings.Join(msgSplit[1:], " ")
var found bool
for _, v := range votepack.TrusteeVotes {
if v == trustee {
found = true
break
}
}
if !found {
util.Errorf("Peer %v has been caught cheating. Attempted to add trustee vote not in votepack.\n", v.Peer.PeerAddress.IP.String())
continue
}
found = false
for _, v := range commands.AllTrusteeVotes {
if v == trustee {
found = true
break
}
}
if found {
util.Errorln("Peer %v has been caught cheating. Attempted to add trustee vote for person who already has trustee.\n", v.Peer.PeerAddress.IP.String())
continue
}
commands.AllTrusteeVotes = append(commands.AllTrusteeVotes, trustee)
util.Infof("Peer %v is trustee for %v\n", v.Peer.PeerAddress.IP.String(), trustee)
continue
}
}
}