-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
276 lines (220 loc) · 6.21 KB
/
client.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Stores functionality for initiating messages.
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"time"
)
// This starts the Client to repeatedly send PING messages to all members in the group.
func startClient(clientServerChan chan int) {
// Ensures that sending starts after listener has started and introduction is complete.
_, _ = <-clientServerChan, <-clientServerChan
for {
members := GetMembers()
var member_ids []string
for k := range members {
member_ids = append(member_ids, k)
}
Shuffle(member_ids)
for _, nodeId := range member_ids {
go PingMember(nodeId)
time.Sleep(PING_INTERVAL_MILLISECONDS * time.Millisecond)
}
}
}
// Sends a PING to a given node ID. Handles piggybacking of messages.
func PingMember(nodeId string) {
connection, err := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if err != nil {
LogError(fmt.Sprintf("Node %s connection is nil, it might have been removed from the group\n",
nodeId))
DeleteMemberAndReReplicate(nodeId)
return
}
defer connection.Close()
member, membererr := GetMemberInfo(nodeId)
if member.failed || !membererr {
return
}
var messagesToPiggyback Messages = GetUnexpiredPiggybackMessages()
pingMessageEnc, err := EncodePingMessage(messagesToPiggyback)
if err != nil {
LogError("Unable to encode ping message")
return
}
var pingMessage Message
err = json.Unmarshal(pingMessageEnc, &pingMessage)
if err != nil {
LogError("Unable to decode outgoing PING message")
return
}
PrintMessage("outgoing", pingMessage, nodeId)
connection.Write(pingMessageEnc)
buffer := make([]byte, 8192)
connection.SetReadDeadline(time.Now().Add(TIMEOUT_DETECTION_MILLISECONDS * time.Millisecond))
mLen, err := connection.Read(buffer)
if err != nil {
// Just mark the node as failed
LogError(fmt.Sprintf("Error in reading from connection for nodeId [%s] Err: [%s]\n", nodeId, err))
LogMessage(fmt.Sprintf("DETECTED NODE %s as FAILED", nodeId))
fmt.Printf("DETECTED NODE %s as FAILED\n", nodeId)
DeleteMemberAndReReplicate(nodeId)
// Start propagating FAIL message.
failedMessage := Message{
Kind: FAIL,
Data: nodeId,
}
AddPiggybackMessage(failedMessage)
return
}
messages, err := DecodeAckMessage(buffer[:mLen])
if err != nil {
LogError(fmt.Sprintf("Unable to decode ACK message from node: %s", nodeId))
return
}
var ackMessage Message
err = json.Unmarshal(buffer[:mLen], &ackMessage)
if err != nil {
return
}
PrintMessage("incoming", ackMessage, nodeId)
for _, subMessage := range messages {
switch subMessage.Kind {
case HELLO:
ProcessHelloMessage(subMessage)
case LEAVE:
ProcessFailOrLeaveMessage(subMessage)
case FAIL:
ProcessFailOrLeaveMessage(subMessage)
default:
log.Fatalf("Unexpected submessage kind in ACK")
}
}
}
// Sends replication requests for all files to the given node.
func SendAnyReplicationMessage(nodeId string, message Message, ch chan error) {
PrintMessage("outgoing", message, nodeId)
encodedMessage, err := json.Marshal(message)
if err != nil {
ch <- err
return
}
connection, err := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if err != nil {
ch <- err
return
}
defer connection.Close()
connection.Write(encodedMessage)
buffer := make([]byte, 8192)
_, err = connection.Read(buffer)
if err != nil {
ch <- err
return
}
ch <- nil
}
// Sends replication requests for all files to the given node.
// This is used for both, replicate-on-create and replicate-on-fail
func SendReplicationMessages(nodeId string, files []*FileInfo, ch chan error) {
fmt.Printf("Sending %d Replication messages to %s\n", len(files), nodeId)
encodedFiles, err := json.Marshal(files)
if err != nil {
ch <- err
return
}
message := Message{Kind: REPLICATE, Data: string(encodedFiles)}
encodedMessage, err := json.Marshal(message)
if err != nil {
ch <- err
return
}
connection, err := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if err != nil {
ch <- err
return
}
defer connection.Close()
connection.Write(encodedMessage)
buffer := make([]byte, 8192)
_, err = connection.Read(buffer)
if err != nil {
ch <- err
return
}
ch <- nil
}
// Sends a message to the given Node.
func SendMessage(nodeId string, message Message) error {
encodedMessage, err := json.Marshal(message)
if err != nil {
return err
}
connection, err := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if err != nil {
return err
}
defer connection.Close()
connection.Write(encodedMessage)
buffer := make([]byte, 8192)
_, err = connection.Read(buffer)
if err != nil {
return err
}
// response := string(buffer[:mLen])
// fmt.Println("Replicated response: ", response)
return nil
}
// Sends a message to the given Node, waits for a reply, and returns the response.
func SendMessageGetReply(nodeId string, message Message) (Message, error) {
// Make an object to store the reply message
var responseMessage Message
encodedMessage, err := json.Marshal(message)
if err != nil {
return responseMessage, err
}
connection, err := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if err != nil {
return responseMessage, err
}
defer connection.Close()
// Send the query message
connection.Write(encodedMessage)
buffer := make([]byte, 8192)
// Read the buffer to see if you have a response
mLen, err := connection.Read(buffer)
if err != nil {
return responseMessage, err
}
// Unmarshal the response message and return that
err = json.Unmarshal(buffer[:mLen], &responseMessage)
return responseMessage, err
}
// Leave the group.
func ExitGroup() {
fmt.Printf("Exiting gracefully %s\n", NODE_ID)
// Leave message just contains the NODE_ID
leaveMessageEnc, err := GetEncodedLeaveMessage(NODE_ID)
if err != nil {
LogError("Unable to encode leave message")
return
}
members := GetMembers()
for nodeId := range members {
connection, _ := net.Dial("tcp", GetServerEndpoint(membershipInfo[nodeId].Host))
if connection != nil {
var leaveMessage Message
err = json.Unmarshal(leaveMessageEnc, &leaveMessage)
if err != nil {
continue
}
PrintMessage("outgoing", leaveMessage, nodeId)
connection.Write(leaveMessageEnc)
connection.Close()
}
}
os.Exit(0)
}