-
Notifications
You must be signed in to change notification settings - Fork 9
/
conn.go
261 lines (204 loc) · 4.98 KB
/
conn.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package bifrost
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/DE-labtory/bifrost/pb"
"github.com/DE-labtory/iLogger"
)
type ConnID = string
type PeerInfo struct {
IP string
PubKeyBytes []byte
IsPrivate bool
MetaData map[string]string
}
type innerMessage struct {
Envelope *pb.Envelope
OnErr func(error)
OnSuccess func(interface{})
}
type Message struct {
Envelope *pb.Envelope
Data []byte
Conn Connection
}
// Respond sends a msg to the source that sent the ReceivedMessageImpl
func (m *Message) Respond(data []byte, protocol string, successCallBack func(interface{}), errCallBack func(error)) {
m.Conn.Send(data, protocol, successCallBack, errCallBack)
}
type Handler interface {
ServeRequest(msg Message)
}
type Connection interface {
Send(data []byte, protocol string, successCallBack func(interface{}), errCallBack func(error))
Close()
GetIP() Address
GetPeerKey() Key
GetID() ConnID
GetMetaData() map[string]string
Start() error
Handle(handler Handler)
}
type GrpcConnection struct {
ID ConnID
peerKey Key
ip Address
streamWrapper StreamWrapper
stopFlag int32
handler Handler
outChannl chan *innerMessage
readChannel chan *pb.Envelope
stopChannel chan struct{}
sync.RWMutex
metaData map[string]string
Crypto
}
func NewConnection(ip string, metaData map[string]string, peerKey Key, streamWrapper StreamWrapper, crypto Crypto) (Connection, error) {
if streamWrapper == nil || peerKey == nil {
return nil, errors.New("fail to create connection streamWrapper or peerKey is nil")
}
ipAddr, err := ToAddress(ip)
if err != nil {
return nil, err
}
return &GrpcConnection{
ID: peerKey.ID(),
peerKey: peerKey,
ip: ipAddr,
streamWrapper: streamWrapper,
outChannl: make(chan *innerMessage, 200),
readChannel: make(chan *pb.Envelope, 200),
stopChannel: make(chan struct{}, 1),
Crypto: crypto,
metaData: metaData,
}, nil
}
func (conn *GrpcConnection) GetMetaData() map[string]string {
return conn.metaData
}
func (conn *GrpcConnection) GetIP() Address {
return conn.ip
}
func (conn *GrpcConnection) GetPeerKey() Key {
return conn.peerKey
}
func (conn *GrpcConnection) GetID() ConnID {
return conn.ID
}
func (conn *GrpcConnection) toDie() bool {
return atomic.LoadInt32(&(conn.stopFlag)) == int32(1)
}
func (conn *GrpcConnection) Handle(handler Handler) {
conn.handler = handler
}
func (conn *GrpcConnection) Send(payload []byte, protocol string, successCallBack func(interface{}), errCallBack func(error)) {
conn.Lock()
defer conn.Unlock()
signedEnvelope, err := conn.build(protocol, payload)
if err != nil {
go errCallBack(errors.New(fmt.Sprintf("fail to sign envelope [%s]", err.Error())))
return
}
m := &innerMessage{
Envelope: signedEnvelope,
OnErr: errCallBack,
OnSuccess: successCallBack,
}
conn.outChannl <- m
}
func (conn *GrpcConnection) build(protocol string, payload []byte) (*pb.Envelope, error) {
sig, err := conn.Sign(payload)
if err != nil {
return nil, err
}
envelope := &pb.Envelope{}
envelope.Signature = sig
envelope.Payload = payload
envelope.Type = pb.Envelope_NORMAL
envelope.Protocol = protocol
envelope.Pubkey = []byte("key")
return envelope, nil
}
func (conn *GrpcConnection) Verify(envelope *pb.Envelope) bool {
flag, err := conn.Crypto.Verify(conn.peerKey, envelope.Signature, envelope.Payload)
if err != nil {
iLogger.Infof(nil, "[Bifrost] %s", err.Error())
return false
}
return flag
}
func (conn *GrpcConnection) writeStream() {
for !conn.toDie() {
select {
case m := <-conn.outChannl:
err := conn.streamWrapper.Send(m.Envelope)
if err != nil {
if m.OnErr != nil {
go m.OnErr(err)
}
} else {
if m.OnSuccess != nil {
go m.OnSuccess("")
}
}
case stop := <-conn.stopChannel:
conn.stopChannel <- stop
return
}
}
}
func (conn *GrpcConnection) readStream(errChan chan error) {
defer func() {
recover()
}()
for !conn.toDie() {
envelope, err := conn.streamWrapper.Recv()
if conn.toDie() {
return
}
if err != nil {
errChan <- err
return
}
conn.readChannel <- envelope
}
}
func (conn *GrpcConnection) Close() {
if conn.toDie() {
return
}
amIFirst := atomic.CompareAndSwapInt32(&conn.stopFlag, int32(0), int32(1))
if !amIFirst {
return
}
conn.stopChannel <- struct{}{}
conn.Lock()
conn.streamWrapper.Close()
conn.Unlock()
}
func (conn *GrpcConnection) Start() error {
errChan := make(chan error, 1)
go conn.readStream(errChan)
go conn.writeStream()
for !conn.toDie() {
select {
case stop := <-conn.stopChannel:
conn.stopChannel <- stop
return nil
case err := <-errChan:
return err
case message := <-conn.readChannel:
if conn.Verify(message) {
if conn.handler != nil {
m := Message{Envelope: message, Conn: conn, Data: message.Payload}
conn.handler.ServeRequest(m)
}
} else {
// todo: verify 결과 false인 경우
}
}
}
return nil
}