-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
66 lines (52 loc) · 1.3 KB
/
messages.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
package main
import (
"encoding/json"
)
const (
// Top level message types.
PING MessageType = 0
ACK MessageType = 1
JOIN MessageType = 2
CREATE MessageType = 3
APPEND MessageType = 4
CHECK MessageType = 5
REPLICATE MessageType = 6
FILES MessageType = 7
GETFILE MessageType = 8
TEMP_CREATE MessageType = 9
TEMP_APPEND MessageType = 10
DELETE MessageType = 11
MERGE MessageType = 12
// Piggybacked message types.
LEAVE MessageType = 16
FAIL MessageType = 17
HELLO MessageType = 18
)
type MemberInfo struct {
failed bool
// Exported variables
Host string
RingPosition int
}
type MessageType int32
type Message struct {
Kind MessageType
// This might be a JSON encoded string, and should be decoded based on Kind.
Data string
}
type Messages []Message
type PiggybackMessage struct {
message Message
ttl int
}
type PiggybackMessages []PiggybackMessage
func GetEncodedJoinMessage() ([]byte, error) {
joinMessage := Message{Kind: JOIN, Data: ""}
joinMessageEnc, err := json.Marshal(joinMessage)
return joinMessageEnc, err
}
func GetEncodedLeaveMessage(nodeId string) ([]byte, error) {
leaveMessage := Message{Kind: LEAVE, Data: nodeId}
leaveMessageEnc, err := json.Marshal(leaveMessage)
return leaveMessageEnc, err
}