forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
137 lines (119 loc) · 3.92 KB
/
general.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package msgsupport
//https://docs.google.com/spreadsheets/d/1wy9JDEqyM2uRYhZ6Y1e9C3hIDm2prIILebztQ5BGlr8/edit#gid=1997221100
import (
"fmt"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/messages/electionMsgs"
)
func init() {
// KLUDGE: a way to allow logs from p2p/connection.go
messages.FP = UnmarshalMessage
}
func UnmarshalMessage(data []byte) (interfaces.IMsg, error) {
_, msg, err := UnmarshalMessageData(data)
return msg, err
}
func CreateMsg(messageType byte) interfaces.IMsg {
switch messageType {
case constants.EOM_MSG:
return new(messages.EOM)
case constants.ACK_MSG:
return new(messages.Ack)
case constants.COMMIT_CHAIN_MSG:
return new(messages.CommitChainMsg)
case constants.COMMIT_ENTRY_MSG:
return new(messages.CommitEntryMsg)
case constants.DIRECTORY_BLOCK_SIGNATURE_MSG:
return new(messages.DirectoryBlockSignature)
case constants.FACTOID_TRANSACTION_MSG:
return new(messages.FactoidTransaction)
case constants.HEARTBEAT_MSG:
return new(messages.Heartbeat)
case constants.MISSING_MSG:
return new(messages.MissingMsg)
case constants.MISSING_MSG_RESPONSE:
return new(messages.MissingMsgResponse)
case constants.MISSING_DATA:
return new(messages.MissingData)
case constants.DATA_RESPONSE:
return new(messages.DataResponse)
case constants.REVEAL_ENTRY_MSG:
return new(messages.RevealEntryMsg)
case constants.REQUEST_BLOCK_MSG:
return new(messages.RequestBlock)
case constants.DBSTATE_MISSING_MSG:
return new(messages.DBStateMissing)
case constants.DBSTATE_MSG:
return new(messages.DBStateMsg)
case constants.ADDSERVER_MSG:
return new(messages.AddServerMsg)
case constants.CHANGESERVER_KEY_MSG:
return new(messages.ChangeServerKeyMsg)
case constants.REMOVESERVER_MSG:
return new(messages.RemoveServerMsg)
case constants.BOUNCE_MSG:
return new(messages.Bounce)
case constants.BOUNCEREPLY_MSG:
return new(messages.BounceReply)
case constants.SYNC_MSG:
return new(electionMsgs.SyncMsg)
case constants.VOLUNTEERAUDIT:
msg := new(electionMsgs.FedVoteVolunteerMsg)
msg.SetFullBroadcast(true)
return msg
case constants.VOLUNTEERPROPOSAL:
msg := new(electionMsgs.FedVoteProposalMsg)
msg.SetFullBroadcast(true)
return msg
case constants.VOLUNTEERLEVELVOTE:
msg := new(electionMsgs.FedVoteLevelMsg)
msg.SetFullBroadcast(true)
return msg
default:
return nil
}
}
func UnmarshalMessageData(data []byte) (newdata []byte, msg interfaces.IMsg, err error) {
if data == nil {
return nil, nil, fmt.Errorf("No data provided")
}
if len(data) == 0 {
return nil, nil, fmt.Errorf("No data provided")
}
messageType := data[0]
msg = CreateMsg(messageType)
if msg == nil {
fmt.Printf("***** Marshal Failed to create message for %d %s", messageType, constants.MessageName(messageType))
return data, nil, fmt.Errorf("Unknown message type %d %x", messageType, data[0])
}
newdata, err = msg.UnmarshalBinaryData(data[:])
if err != nil {
fmt.Printf("***** Marshal Failed to unmarshal %d %s %x\n",
messageType,
constants.MessageName(messageType),
data)
return data, nil, err
}
return newdata, msg, nil
}
// GeneralFactory is used to get around package import loops.
type GeneralFactory struct {
}
var _ interfaces.IGeneralMsg = (*GeneralFactory)(nil)
func (GeneralFactory) CreateMsg(messageType byte) interfaces.IMsg {
return CreateMsg(messageType)
}
func (GeneralFactory) MessageName(Type byte) string {
return constants.MessageName(Type)
}
func (GeneralFactory) UnmarshalMessageData(data []byte) (newdata []byte, msg interfaces.IMsg, err error) {
return UnmarshalMessageData(data)
}
func (GeneralFactory) UnmarshalMessage(data []byte) (interfaces.IMsg, error) {
return UnmarshalMessage(data)
}