github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/peer/common/ordererclient.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package common 18 19 import ( 20 "fmt" 21 "strings" 22 "time" 23 24 cb "github.com/hyperledger/fabric/protos/common" 25 ab "github.com/hyperledger/fabric/protos/orderer" 26 "golang.org/x/net/context" 27 "google.golang.org/grpc" 28 "google.golang.org/grpc/credentials" 29 ) 30 31 type BroadcastClient interface { 32 //Send data to orderer 33 Send(env *cb.Envelope) error 34 Close() error 35 } 36 37 type broadcastClient struct { 38 conn *grpc.ClientConn 39 client ab.AtomicBroadcast_BroadcastClient 40 } 41 42 // GetBroadcastClient creates a simple instance of the BroadcastClient interface 43 func GetBroadcastClient(orderingEndpoint string, tlsEnabled bool, caFile string) (BroadcastClient, error) { 44 45 if len(strings.Split(orderingEndpoint, ":")) != 2 { 46 return nil, fmt.Errorf("Ordering service endpoint %s is not valid or missing", orderingEndpoint) 47 } 48 49 var opts []grpc.DialOption 50 // check for TLS 51 if tlsEnabled { 52 if caFile != "" { 53 creds, err := credentials.NewClientTLSFromFile(caFile, "") 54 if err != nil { 55 return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err) 56 } 57 opts = append(opts, grpc.WithTransportCredentials(creds)) 58 } 59 } else { 60 opts = append(opts, grpc.WithInsecure()) 61 } 62 63 opts = append(opts, grpc.WithTimeout(3*time.Second)) 64 opts = append(opts, grpc.WithBlock()) 65 66 conn, err := grpc.Dial(orderingEndpoint, opts...) 67 if err != nil { 68 return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err) 69 } 70 client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO()) 71 if err != nil { 72 conn.Close() 73 return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err) 74 } 75 76 return &broadcastClient{conn: conn, client: client}, nil 77 } 78 79 func (s *broadcastClient) getAck() error { 80 msg, err := s.client.Recv() 81 if err != nil { 82 return err 83 } 84 if msg.Status != cb.Status_SUCCESS { 85 return fmt.Errorf("Got unexpected status: %v", msg.Status) 86 } 87 return nil 88 } 89 90 //Send data to orderer 91 func (s *broadcastClient) Send(env *cb.Envelope) error { 92 if err := s.client.Send(env); err != nil { 93 return fmt.Errorf("Could not send :%s)", err) 94 } 95 96 err := s.getAck() 97 98 return err 99 } 100 101 func (s *broadcastClient) Close() error { 102 return s.conn.Close() 103 }