forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
256 lines (222 loc) · 6.86 KB
/
consumer.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package consumer
import (
"errors"
"fmt"
"io"
"sync"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/comm"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
ehpb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
)
var consumerLogger = flogging.MustGetLogger("eventhub_consumer")
//EventsClient holds the stream and adapter for consumer to work with
type EventsClient struct {
sync.RWMutex
peerAddress string
regTimeout time.Duration
stream ehpb.Events_ChatClient
adapter EventAdapter
}
//NewEventsClient Returns a new grpc.ClientConn to the configured local PEER.
func NewEventsClient(peerAddress string, regTimeout time.Duration, adapter EventAdapter) (*EventsClient, error) {
var err error
if regTimeout < 100*time.Millisecond {
regTimeout = 100 * time.Millisecond
err = fmt.Errorf("regTimeout >= 0, setting to 100 msec")
} else if regTimeout > 60*time.Second {
regTimeout = 60 * time.Second
err = fmt.Errorf("regTimeout > 60, setting to 60 sec")
}
return &EventsClient{sync.RWMutex{}, peerAddress, regTimeout, nil, adapter}, err
}
//newEventsClientConnectionWithAddress Returns a new grpc.ClientConn to the configured local PEER.
func newEventsClientConnectionWithAddress(peerAddress string) (*grpc.ClientConn, error) {
if comm.TLSEnabled() {
return comm.NewClientConnectionWithAddress(peerAddress, true, true, comm.InitTLSForPeer())
}
return comm.NewClientConnectionWithAddress(peerAddress, true, false, nil)
}
func (ec *EventsClient) send(emsg *ehpb.Event) error {
ec.Lock()
defer ec.Unlock()
// obtain the default signing identity for this peer; it will be used to sign the event
localMsp := mspmgmt.GetLocalMSP()
if localMsp == nil {
return errors.New("nil local MSP manager")
}
signer, err := localMsp.GetDefaultSigningIdentity()
if err != nil {
return fmt.Errorf("could not obtain the default signing identity, err %s", err)
}
//pass the signer's cert to Creator
signerCert, err := signer.Serialize()
if err != nil {
return fmt.Errorf("fail to serialize the default signing identity, err %s", err)
}
emsg.Creator = signerCert
signedEvt, err := utils.GetSignedEvent(emsg, signer)
if err != nil {
return fmt.Errorf("could not sign outgoing event, err %s", err)
}
return ec.stream.Send(signedEvt)
}
// RegisterAsync - registers interest in a event and doesn't wait for a response
func (ec *EventsClient) RegisterAsync(ies []*ehpb.Interest) error {
creator, err := getCreatorFromLocalMSP()
if err != nil {
return fmt.Errorf("error getting creator from MSP: %s", err)
}
emsg := &ehpb.Event{Event: &ehpb.Event_Register{Register: &ehpb.Register{Events: ies}}, Creator: creator}
if err = ec.send(emsg); err != nil {
consumerLogger.Errorf("error on Register send %s\n", err)
}
return err
}
// register - registers interest in a event
func (ec *EventsClient) register(ies []*ehpb.Interest) error {
var err error
if err = ec.RegisterAsync(ies); err != nil {
return err
}
regChan := make(chan struct{})
go func() {
defer close(regChan)
in, inerr := ec.stream.Recv()
if inerr != nil {
err = inerr
return
}
switch in.Event.(type) {
case *ehpb.Event_Register:
case nil:
err = fmt.Errorf("invalid nil object for register")
default:
err = fmt.Errorf("invalid registration object")
}
}()
select {
case <-regChan:
case <-time.After(ec.regTimeout):
err = fmt.Errorf("timeout waiting for registration")
}
return err
}
// UnregisterAsync - Unregisters interest in a event and doesn't wait for a response
func (ec *EventsClient) UnregisterAsync(ies []*ehpb.Interest) error {
creator, err := getCreatorFromLocalMSP()
if err != nil {
return fmt.Errorf("error getting creator from MSP: %s", err)
}
emsg := &ehpb.Event{Event: &ehpb.Event_Unregister{Unregister: &ehpb.Unregister{Events: ies}}, Creator: creator}
if err = ec.send(emsg); err != nil {
err = fmt.Errorf("error on unregister send %s\n", err)
}
return err
}
// Recv receives next event - use when client has not called Start
func (ec *EventsClient) Recv() (*ehpb.Event, error) {
in, err := ec.stream.Recv()
if err == io.EOF {
// read done.
if ec.adapter != nil {
ec.adapter.Disconnected(nil)
}
return nil, err
}
if err != nil {
if ec.adapter != nil {
ec.adapter.Disconnected(err)
}
return nil, err
}
return in, nil
}
func (ec *EventsClient) processEvents() error {
defer ec.stream.CloseSend()
for {
in, err := ec.stream.Recv()
if err == io.EOF {
// read done.
if ec.adapter != nil {
ec.adapter.Disconnected(nil)
}
return nil
}
if err != nil {
if ec.adapter != nil {
ec.adapter.Disconnected(err)
}
return err
}
if ec.adapter != nil {
cont, err := ec.adapter.Recv(in)
if !cont {
return err
}
}
}
}
//Start establishes connection with Event hub and registers interested events with it
func (ec *EventsClient) Start() error {
conn, err := newEventsClientConnectionWithAddress(ec.peerAddress)
if err != nil {
return fmt.Errorf("could not create client conn to %s:%s", ec.peerAddress, err)
}
ies, err := ec.adapter.GetInterestedEvents()
if err != nil {
return fmt.Errorf("error getting interested events:%s", err)
}
if len(ies) == 0 {
return fmt.Errorf("must supply interested events")
}
serverClient := ehpb.NewEventsClient(conn)
ec.stream, err = serverClient.Chat(context.Background())
if err != nil {
return fmt.Errorf("could not create client conn to %s:%s", ec.peerAddress, err)
}
if err = ec.register(ies); err != nil {
return err
}
go ec.processEvents()
return nil
}
//Stop terminates connection with event hub
func (ec *EventsClient) Stop() error {
if ec.stream == nil {
// in case the steam/chat server has not been established earlier, we assume that it's closed, successfully
return nil
}
return ec.stream.CloseSend()
}
func getCreatorFromLocalMSP() ([]byte, error) {
localMsp := mspmgmt.GetLocalMSP()
if localMsp == nil {
return nil, errors.New("nil local MSP manager")
}
signer, err := localMsp.GetDefaultSigningIdentity()
if err != nil {
return nil, fmt.Errorf("could not obtain the default signing identity, err %s", err)
}
creator, err := signer.Serialize()
if err != nil {
return nil, fmt.Errorf("error serializing the signer: %s", err)
}
return creator, nil
}