This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
165 lines (139 loc) · 2.97 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
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
package raft
import (
"bytes"
"encoding/gob"
)
type Message interface {
Source() int
Dest() int
Term() int
}
func init() {
gob.Register(AppendEntries{})
gob.Register(AppendEntriesResponse{})
gob.Register(RequestVote{})
gob.Register(RequestVoteResponse{})
}
func Encode(msg Message) ([]byte, error) {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
if err := encoder.Encode(&msg); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func Decode(b []byte) (Message, error) {
buf := bytes.NewBuffer(b)
decoder := gob.NewDecoder(buf)
var data Message
if err := decoder.Decode(&data); err != nil {
return nil, err
}
return data, nil
}
type AppendEntries struct {
Src int
Dst int
Tm int
PrevLogIdx int
PrevLogTm int
Entries []LogEntry
LeaderCommit int
}
func NewAppendEntries(source, dest, term, preLogIdx, preLogTerm int, entries []LogEntry, leaderCommit int) AppendEntries {
return AppendEntries{
Src: source,
Dst: dest,
Tm: term,
PrevLogIdx: preLogIdx,
PrevLogTm: preLogTerm,
Entries: entries,
LeaderCommit: leaderCommit,
}
}
func (ap AppendEntries) Source() int {
return ap.Src
}
func (ap AppendEntries) Dest() int {
return ap.Dst
}
func (ap AppendEntries) Term() int {
return ap.Tm
}
type AppendEntriesResponse struct {
Src int
Dst int
Tm int
Success bool
MatchIdx int
}
func NewAppendEntriesResponse(source, dest, term int, success bool, matchIdx int) AppendEntriesResponse {
return AppendEntriesResponse{
Src: source,
Dst: dest,
Tm: term,
Success: success,
MatchIdx: matchIdx,
}
}
func (ap AppendEntriesResponse) Source() int {
return ap.Src
}
func (ap AppendEntriesResponse) Dest() int {
return ap.Dst
}
func (ap AppendEntriesResponse) Term() int {
return ap.Tm
}
type RequestVote struct {
Src int
Dst int
Tm int
LastLogIdx int
LastLogTm int
}
func NewRequestVote(source, dest, term, lastLogIdx, lastLogTerm int) RequestVote {
return RequestVote{
Src: source,
Dst: dest,
Tm: term,
LastLogIdx: lastLogIdx,
LastLogTm: lastLogTerm,
}
}
func (rv RequestVote) Source() int {
return rv.Src
}
func (rv RequestVote) Dest() int {
return rv.Dst
}
func (rv RequestVote) Term() int {
return rv.Tm
}
type RequestVoteResponse struct {
Src int
Dst int
Tm int
VoteGranted int
}
func NewRequestVoteResponse(source, dest, term, voteGranted int) RequestVoteResponse {
return RequestVoteResponse{
Src: source,
Dst: dest,
Tm: term,
VoteGranted: voteGranted,
}
}
func (rvr RequestVoteResponse) Source() int {
return rvr.Src
}
func (rvr RequestVoteResponse) Dest() int {
return rvr.Dst
}
func (rvr RequestVoteResponse) Term() int {
return rvr.Tm
}
var _ Message = (*AppendEntries)(nil)
var _ Message = (*AppendEntriesResponse)(nil)
var _ Message = (*RequestVote)(nil)
var _ Message = (*RequestVoteResponse)(nil)