forked from vguardbc/vguardbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
93 lines (75 loc) · 1.95 KB
/
handlers.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
package main
import (
"encoding/gob"
"errors"
"net"
"sync"
)
var requestQueue []chan *Proposal
var concierge = struct {
n [NOP][]*ConnDock // Three phases
//b map[int][]int //map[booth#] int{server blockIDs in this booth}
mu sync.RWMutex
}{}
//var requestQueue = make(chan *Proposal, MaxQueue)
func initConns(numOfServers int) {
// initialize concierge
for i := 0; i < len(concierge.n); i++ {
concierge.n[i] = make([]*ConnDock, numOfServers)
}
// initialize dialog conns
for i := 0; i < len(dialogMgr.conns); i++ {
dialogMgr.conns[i] = make(map[ServerId]ConnDock)
}
}
var dialogMgr = struct {
sync.RWMutex
conns []map[ServerId]ConnDock
}{conns: make([]map[ServerId]ConnDock, NOP)}
type ConnDock struct {
SID ServerId
conn *net.TCPConn
enc *gob.Encoder
dec *gob.Decoder
}
func connRegistration(sconn net.TCPConn, phase int) (ServerId, error) {
concierge.mu.Lock()
defer concierge.mu.Unlock()
defer serverIdLookup.RUnlock()
serverIdLookup.RLock()
if sid, ok := serverIdLookup.m[sconn.RemoteAddr().String()]; ok {
concierge.n[phase][sid] = &ConnDock{
SID: sid,
conn: &sconn,
enc: gob.NewEncoder(&sconn),
dec: gob.NewDecoder(&sconn),
}
log.Infof("%s | new server registered | Id: %v -> Addr: %v\n", cmdPhase[phase], sid, sconn.RemoteAddr())
return sid, nil
} else {
return -1, errors.New("incoming connection not recognized")
}
}
func dialSendBack(m interface{}, encoder *gob.Encoder, phaseNumber int) {
if encoder == nil {
log.Errorf("%s | encoder is nil", rpyPhase[phaseNumber])
}
if err := encoder.Encode(m); err != nil {
log.Errorf("%s | send back failed | err: %v", rpyPhase[phaseNumber], err)
}
}
func takingInitRoles(proposer ServerId) {
if proposer == ServerId(ServerID) {
go runAsProposer(proposer)
} else {
proposerLookup.Lock()
for i := 0; i < NOP; i++ {
proposerLookup.m[Phase(i)] = proposer
}
proposerLookup.Unlock()
go runAsValidator()
}
}
func start() {
takingInitRoles(ServerId(0))
}