-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
86 lines (68 loc) · 1.59 KB
/
message.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
package go_cluster
import (
"encoding/gob"
)
// The Message interface, this is supposed to be customized (for example Msg is encoded in gob).
type Message interface {
Type() string
Msg() interface{}
}
// A message containing only errors
type ErrorMessage struct {
Err error
}
func (m ErrorMessage) Type() string {
return "error"
}
func (m ErrorMessage) Msg() interface{} {
return m.Err.Error()
}
// The message the master sends to a newly connected node with its id
type ReadyMessage struct {
Id int
EntryId int
}
func (m ReadyMessage) Msg() interface{} {
return m
}
func (m ReadyMessage) Type() string {
return "readyreq"
}
// The message a node sends to the node it's newly connected to with its id to make authentication easier
type GreetingMessage struct {
Id int
Data interface{}
}
func (m GreetingMessage) Msg() interface{} {
return m
}
func (m GreetingMessage) Type() string {
return "greetreq"
}
// The message the master sends when all nodes when a new node joins
type NewNodeMessage struct {
Id int // The Id
Addr string // The address to connect to
Data interface{} // The new node's data
}
func (m NewNodeMessage) Msg() interface{} {
return m
}
func (m NewNodeMessage) Type() string {
return "newnodereq"
}
// The message a new node sends to the master with its information
type IntroduceMessage struct {
Addr string
Data interface{}
}
func (m IntroduceMessage) Msg() interface{} {
return m.Addr
}
func (m IntroduceMessage) Type() string {
return "introreq"
}
// Register the message type to gob
func RegisterMessage(msg Message) {
gob.Register(msg)
}