github.com/lzy4123/fabric@v2.1.1+incompatible/internal/peer/common/broadcastclient.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package common 8 9 import ( 10 cb "github.com/hyperledger/fabric-protos-go/common" 11 ab "github.com/hyperledger/fabric-protos-go/orderer" 12 "github.com/pkg/errors" 13 ) 14 15 type BroadcastClient interface { 16 //Send data to orderer 17 Send(env *cb.Envelope) error 18 Close() error 19 } 20 21 type BroadcastGRPCClient struct { 22 Client ab.AtomicBroadcast_BroadcastClient 23 } 24 25 // GetBroadcastClient creates a simple instance of the BroadcastClient interface 26 func GetBroadcastClient() (BroadcastClient, error) { 27 oc, err := NewOrdererClientFromEnv() 28 if err != nil { 29 return nil, err 30 } 31 bc, err := oc.Broadcast() 32 if err != nil { 33 return nil, err 34 } 35 36 return &BroadcastGRPCClient{Client: bc}, nil 37 } 38 39 func (s *BroadcastGRPCClient) getAck() error { 40 msg, err := s.Client.Recv() 41 if err != nil { 42 return err 43 } 44 if msg.Status != cb.Status_SUCCESS { 45 return errors.Errorf("got unexpected status: %v -- %s", msg.Status, msg.Info) 46 } 47 return nil 48 } 49 50 //Send data to orderer 51 func (s *BroadcastGRPCClient) Send(env *cb.Envelope) error { 52 if err := s.Client.Send(env); err != nil { 53 return errors.WithMessage(err, "could not send") 54 } 55 56 err := s.getAck() 57 58 return err 59 } 60 61 func (s *BroadcastGRPCClient) Close() error { 62 return s.Client.CloseSend() 63 }