-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage.go
More file actions
76 lines (64 loc) · 2.1 KB
/
message.go
File metadata and controls
76 lines (64 loc) · 2.1 KB
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
package main
import "encoding/json"
// Message represents a network message exchanged between peers. It includes the
// operation type (Ops), an optional JSON-encoded payload, and the sender info.
type Message struct {
Ops string `json:"ops"`
Payload string `json:"payload"`
Sender Peer `json:"sender"`
}
// Json serializes the message into JSON. Errors are ignored in this demo code.
func (m Message) Json() []byte {
json, _ := json.Marshal(m)
return json
}
// WithSender returns a shallow copy of the message with the provided sender set.
func (m Message) WithSender(sender Peer) Message {
m.Sender = sender
return m
}
// NewRegisterMessage creates a registration message that carries the sender's
// listening address and port so that peers can learn about a new node.
func NewRegisterMessage(listenAddr string, listenPort int) Message {
payload, _ := json.Marshal(Peer{listenAddr, listenPort})
return Message{
Ops: "register",
Payload: string(payload),
}
}
// NewReanounceMessage announces this node again to peers. Used after discovery
// to propagate our presence.
func NewReanounceMessage(listenAddr string, listenPort int) Message {
payload, _ := json.Marshal(Peer{listenAddr, listenPort})
return Message{
Ops: "reanounce",
Payload: string(payload),
}
}
// NewPropogateMessage is used to propagate information about a new peer to
// others.
func NewPropogateMessage(listenAddr string, listenPort int) Message {
payload, _ := json.Marshal(Peer{listenAddr, listenPort})
return Message{
Ops: "propogate",
Payload: string(payload),
}
}
// NewPromoteMessage starts a leader election (RequestVote equivalent in Raft).
func NewPromoteMessage() Message {
return Message{
Ops: "promote",
}
}
// NewHeartbeatMessage represents a leader heartbeat to followers.
func NewHeartbeatMessage() Message {
return Message{
Ops: "beat",
}
}
// NewVoteMessage represents a positive vote response to a candidate.
func NewVoteMessage() Message {
return Message{
Ops: "vote",
}
}