-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_comm.go
197 lines (127 loc) · 3.52 KB
/
mock_comm.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mock
import (
"time"
proto "github.com/mcc-github/blockchain-protos-go/gossip"
"github.com/mcc-github/blockchain/gossip/api"
"github.com/mcc-github/blockchain/gossip/comm"
"github.com/mcc-github/blockchain/gossip/common"
"github.com/mcc-github/blockchain/gossip/protoext"
"github.com/mcc-github/blockchain/gossip/util"
)
type socketMock struct {
endpoint string
socket chan interface{}
}
type packetMock struct {
src *socketMock
dst *socketMock
msg interface{}
}
type channelMock struct {
accept common.MessageAcceptor
channel chan protoext.ReceivedMessage
}
type commMock struct {
id string
members map[string]*socketMock
acceptors []*channelMock
deadChannel chan common.PKIidType
done chan struct{}
}
var logger = util.GetLogger(util.CommMockLogger, "")
func NewCommMock(id string, members map[string]*socketMock) comm.Comm {
res := &commMock{
id: id,
members: members,
acceptors: make([]*channelMock, 0),
done: make(chan struct{}),
deadChannel: make(chan common.PKIidType),
}
go res.start()
return res
}
func (packet *packetMock) Respond(msg *proto.GossipMessage) {
sMsg, _ := protoext.NoopSign(msg)
packet.src.socket <- &packetMock{
src: packet.dst,
dst: packet.src,
msg: sMsg,
}
}
func (packet *packetMock) Ack(err error) {
}
func (packet *packetMock) GetSourceEnvelope() *proto.Envelope {
return nil
}
func (packet *packetMock) GetGossipMessage() *protoext.SignedGossipMessage {
return packet.msg.(*protoext.SignedGossipMessage)
}
func (packet *packetMock) GetConnectionInfo() *protoext.ConnectionInfo {
return nil
}
func (mock *commMock) start() {
logger.Debug("Starting communication mock module...")
for {
select {
case <-mock.done:
{
logger.Debug("Exiting...")
return
}
case msg := <-mock.members[mock.id].socket:
{
logger.Debug("Got new message", msg)
packet := msg.(*packetMock)
for _, channel := range mock.acceptors {
if channel.accept(packet) {
channel.channel <- packet
}
}
}
}
}
}
func (mock *commMock) GetPKIid() common.PKIidType {
return common.PKIidType(mock.id)
}
func (mock *commMock) Send(msg *protoext.SignedGossipMessage, peers ...*comm.RemotePeer) {
for _, peer := range peers {
logger.Debug("Sending message to peer ", peer.Endpoint, "from ", mock.id)
mock.members[peer.Endpoint].socket <- &packetMock{
src: mock.members[mock.id],
dst: mock.members[peer.Endpoint],
msg: msg,
}
}
}
func (mock *commMock) SendWithAck(_ *protoext.SignedGossipMessage, _ time.Duration, _ int, _ ...*comm.RemotePeer) comm.AggregatedSendResult {
panic("not implemented")
}
func (mock *commMock) Probe(peer *comm.RemotePeer) error {
return nil
}
func (mock *commMock) Handshake(peer *comm.RemotePeer) (api.PeerIdentityType, error) {
return nil, nil
}
func (mock *commMock) Accept(accept common.MessageAcceptor) <-chan protoext.ReceivedMessage {
ch := make(chan protoext.ReceivedMessage)
mock.acceptors = append(mock.acceptors, &channelMock{accept, ch})
return ch
}
func (mock *commMock) PresumedDead() <-chan common.PKIidType {
return mock.deadChannel
}
func (mock *commMock) CloseConn(peer *comm.RemotePeer) {
}
func (mock *commMock) Stop() {
logger.Debug("Stopping communication module, closing all accepting channels.")
for _, accept := range mock.acceptors {
close(accept.channel)
}
logger.Debug("[XXX]: Sending done signal to close the module.")
mock.done <- struct{}{}
}