-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmsg.go
More file actions
169 lines (145 loc) · 3.61 KB
/
msg.go
File metadata and controls
169 lines (145 loc) · 3.61 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
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
166
167
168
169
package main
import (
"encoding/json"
"fmt"
"reflect"
)
const headerLength = 12
type HeaderMsg string
const (
hRequest HeaderMsg = "Request"
hPrePrepare HeaderMsg = "PrePrepare"
hPrepare HeaderMsg = "Prepare"
hCommit HeaderMsg = "Commit"
hReply HeaderMsg = "Reply"
)
type Msg interface {
String() string
}
//<REQUEST, o, t, c>
type RequestMsg struct {
Operation string `json:"operation"`
Timestamp int `json:"timestamp"`
ClientID int `json:"clientID"`
CRequest Request `json:"request"`
}
func (msg RequestMsg) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
//<<PRE-PREPARE,v,n,d>,m>
type PrePrepareMsg struct {
Request RequestMsg `json:"request"`
Digest string `json:"digest"`
ViewID int `json:"viewID"`
SequenceID int `json:"sequenceID"`
}
func (msg PrePrepareMsg) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
//<PREPARE, v, n, d, i>
type PrepareMsg struct {
Digest string `json:"digest"`
ViewID int `json:"viewID"`
SequenceID int `json:"sequenceID"`
NodeID int `json:"nodeid"`
}
func (msg PrepareMsg) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
//<COMMIT, v, n, d, i>
type CommitMsg struct {
Digest string `json:"digest"`
ViewID int `json:"viewID"`
SequenceID int `json:"sequenceID"`
NodeID int `json:"nodeid"`
}
func (msg CommitMsg) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
//<REPLY, v, t, c, i, r>
type ReplyMsg struct {
ViewID int `json:"viewID"`
Timestamp int `json:"timestamp"`
ClientID int `json:"clientID"`
NodeID int `json:"nodeid"`
Result string `json:"result"`
}
func (msg ReplyMsg) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
type Request struct {
Message string `json:"message"`
Digest string `json:"digest"`
}
func (msg Request) String() string {
bmsg, _ := json.MarshalIndent(msg,""," ")
return string(bmsg) + "\n"
}
func ComposeMsg(header HeaderMsg, payload interface{}, sig []byte) []byte{
var bpayload []byte
var err error
t := reflect.TypeOf(payload)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
switch t.Kind() {
case reflect.Struct:
bpayload, err = json.Marshal(payload)
if err != nil {
panic(err)
}
case reflect.Slice:
bpayload = payload.([]byte)
default:
panic(fmt.Errorf("not support type"))
}
b := make([]byte, headerLength)
for i, h := range []byte(header){
b[i] = h
}
res := make([]byte, headerLength+len(bpayload)+len(sig))
copy(res[:headerLength],b)
copy(res[headerLength:],bpayload)
if len(sig) > 0 {
copy(res[headerLength+len(bpayload):], sig)
}
return res
}
func SplitMsg(bmsg []byte) (HeaderMsg, []byte, []byte){
var header HeaderMsg
var payload []byte
var signature []byte
hbyte := bmsg[:headerLength]
hhbyte := make([]byte,0)
for _, h := range hbyte{
if h != byte(0){
hhbyte = append(hhbyte,h)
}
}
header = HeaderMsg(hhbyte)
switch header {
case hRequest, hPrePrepare, hPrepare, hCommit:
payload = bmsg[headerLength:len(bmsg)-256]
signature = bmsg[len(bmsg)-256:]
case hReply:
payload = bmsg[headerLength:]
signature = []byte{}
}
return header, payload, signature
}
func printMsgLog(msg Msg){
fmt.Println(msg.String())
}
func logHandleMsg(header HeaderMsg, msg Msg, from int){
fmt.Printf("Receive %s msg from localhost:%d\n", header, nodeIdToPort(from))
printMsgLog(msg)
}
func logBroadcastMsg(header HeaderMsg, msg Msg, ){
fmt.Printf("send/broadcast %s msg \n", header)
printMsgLog(msg)
}