-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
191 lines (168 loc) · 3.47 KB
/
server.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
package main
import "net"
import "fmt"
import "log"
import "bufio"
//import "strings"
import "time"
type Server struct {
priority string
state string
king string
joins chan net.Conn
pong bool
}
type Message struct {
msg string
priority string
}
func (s *Server) Start() {
ln, _ := net.Listen("tcp", PRIORITY_MAP[s.priority])
go func() {
for {
select {
case conn := <-s.joins:
s.handleRequest(conn)
}
}
}()
go func() {
for {
conn, _ := ln.Accept()
s.joins <- conn
}
}()
go func() {
c := time.Tick(2*T)
for _ = range c {
if s.state == "worker" {
go s.pingKing()
}
}
}()
s.startElections()
}
func (s *Server) pingKing() {
fmt.Println("pingKing")
if s.king == "" || s.state == "king" {
return
}
if s.pong {
s.sendMessage("PING", PRIORITY_MAP[s.king])
s.pong = false
} else {
go s.startElections()
}
}
func (s *Server) startElections() {
log.Printf("Start elections")
s.setState("election")
for priority, server := range PRIORITY_MAP {
if s.priority < priority {
go s.sendMessage("ALIVE?", server)
}
}
finish := time.After(time.Duration(T))
for {
select {
case <-finish:
fmt.Println("Election timeout")
s.checkElectionResults()
return
}
}
}
func (s *Server) sendMessage(msg string, address string) {
message := "[" + s.priority + "]" + msg
fmt.Println("Send msg", message, address)
conn, err := net.Dial("tcp", address)
if err != nil {
//println("Connect to server failed:", err.Error())
return
}
fmt.Fprintf(conn, message+"\n")
}
func (s *Server) checkElectionResults() {
switch {
case s.state == "election":
s.setState("king")
go s.sendToAll("IMTHEKING")
//case s.state == "waiting-for-king":
}
}
func (s *Server) handleRequest(conn net.Conn) {
message, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Message Received:", string(message))
defer conn.Close()
msg, ok := s.parseProtocol(message)
if !ok {
return
}
switch {
case msg.msg == "ALIVE?\n":
go s.handleAliveMsg(msg)
case msg.msg == "IMTHEKING\n":
//go s.handleKingMsg(msg)
s.setState("worker")
s.king = msg.priority
s.pong = true
fmt.Println("Set king to ", msg.priority)
case msg.msg == "PING\n":
if s.state == "king" {
go s.sendMessage("PONG", msg.Server())
}
case msg.msg == "PONG\n":
if s.state == "worker" && s.king == msg.priority {
s.pong = true
}
case msg.msg == "FINETHANKS\n":
if s.state == "election" {
s.setState("waiting-for-king")
go func() {
finish := time.After(time.Duration(2*T))
for {
select {
case <-finish:
fmt.Println("Waiting for king timeout")
if s.state == "waiting-for-king" {
//go s.startElections()
fmt.Println("Election should be started")
}
}
}
}()
}
default:
return
}
}
func (s *Server) parseProtocol(msg string) (Message, bool) {
priority := string(msg[1])
message := string(msg[3:])
return Message{
msg: message,
priority: priority,
}, true
}
func (s *Server) handleAliveMsg(msg Message) {
if s.state == "king" {
go s.sendMessage("IMTHEKING", msg.Server())
} else {
go s.startElections()
go s.sendMessage("FINETHANKS", msg.Server())
}
}
func (s *Server) sendToAll(msg string) {
for priority, server := range PRIORITY_MAP {
if s.priority != priority {
go s.sendMessage(msg, server)
}
}
}
func (s *Server) setState(state string) {
fmt.Println("Set state:", state)
s.state = state
}
func (m *Message) Server() string {
return PRIORITY_MAP[m.priority]
}