-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.go
104 lines (82 loc) · 1.71 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
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
package main
// Operation type mapping
// ADD - 0
// PENDING - 1
// DELETE - 2
// NOTHING - 3
/// Message type mapping
// REQ - 0
// OK - 1
// NEWVIEW - 2
// NEWLEADER - 3
type Message struct {
Type int
Data map[string]int
}
func ReqMessage(rid int, cid int, pid int, opType int) Message {
m := make(map[string]int)
m["reqId"] = rid
m["curViewId"] = cid
m["procId"] = pid
m["opType"] = opType
return Message{
Type: 0, // REQ message
Data: m,
}
}
func IsReqMessage(message *Message) bool {
return message.Type == 0
}
func IsOkMessage(message *Message) bool {
return message.Type == 1
}
func IsNewViewMessage(message *Message) bool {
return message.Type == 2
}
func IsNewLeaderMessage(message *Message) bool {
return message.Type == 3
}
func AddReqMessage(rid int, cid int, pid int) Message {
return ReqMessage(rid, cid, pid, 0)
}
func IsAddReqMessage(msg *Message) bool {
return msg.Data["opType"] == 0
}
func DeleteReqMessage(rid int, cid int, pid int) Message {
return ReqMessage(rid, cid, pid, 2)
}
func IsDeleteReqMessage(msg *Message) bool {
return msg.Data["opType"] == 2
}
func OkMessage(rid int, cid int) Message {
m := make(map[string]int)
m["reqId"] = rid
m["curViewId"] = cid
return Message{
Type: 1,
Data: m,
}
}
func NewViewMessage(cid int, mMap map[string]int) Message {
mMap["curViewId"] = cid
return Message{
Type: 2,
Data: mMap,
}
}
func NewLeaderMessage(rid int, cid int) Message {
m := make(map[string]int)
m["reqId"] = rid
m["curViewId"] = cid
m["opType"] = 1
return Message{
Type: 3,
Data: m,
}
}
func IsPendingReqMessage(msg *Message) bool {
return msg.Data["opType"] == 1
}
func IsNothingReqMessage(msg *Message) bool {
return msg.Data["opType"] == 3
}