github.com/anjalikarhana/fabric@v2.1.1+incompatible/internal/peer/common/ordererclient.go (about)

     1  /*
     2  Copyright IBM Corp. 2016-2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package common
     7  
     8  import (
     9  	"context"
    10  	"crypto/tls"
    11  
    12  	ab "github.com/hyperledger/fabric-protos-go/orderer"
    13  	"github.com/hyperledger/fabric/internal/pkg/comm"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // OrdererClient represents a client for communicating with an ordering
    18  // service
    19  type OrdererClient struct {
    20  	CommonClient
    21  }
    22  
    23  // NewOrdererClientFromEnv creates an instance of an OrdererClient from the
    24  // global Viper instance
    25  func NewOrdererClientFromEnv() (*OrdererClient, error) {
    26  	address, override, clientConfig, err := configFromEnv("orderer")
    27  	if err != nil {
    28  		return nil, errors.WithMessage(err, "failed to load config for OrdererClient")
    29  	}
    30  	gClient, err := comm.NewGRPCClient(clientConfig)
    31  	if err != nil {
    32  		return nil, errors.WithMessage(err, "failed to create OrdererClient from config")
    33  	}
    34  	oClient := &OrdererClient{
    35  		CommonClient: CommonClient{
    36  			GRPCClient: gClient,
    37  			Address:    address,
    38  			sn:         override}}
    39  	return oClient, nil
    40  }
    41  
    42  // Broadcast returns a broadcast client for the AtomicBroadcast service
    43  func (oc *OrdererClient) Broadcast() (ab.AtomicBroadcast_BroadcastClient, error) {
    44  	conn, err := oc.CommonClient.NewConnection(oc.Address, comm.ServerNameOverride(oc.sn))
    45  	if err != nil {
    46  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.Address)
    47  	}
    48  	// TODO: check to see if we should actually handle error before returning
    49  	return ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    50  }
    51  
    52  // Deliver returns a deliver client for the AtomicBroadcast service
    53  func (oc *OrdererClient) Deliver() (ab.AtomicBroadcast_DeliverClient, error) {
    54  	conn, err := oc.CommonClient.NewConnection(oc.Address, comm.ServerNameOverride(oc.sn))
    55  	if err != nil {
    56  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.Address)
    57  	}
    58  	// TODO: check to see if we should actually handle error before returning
    59  	return ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
    60  
    61  }
    62  
    63  // Certificate returns the TLS client certificate (if available)
    64  func (oc *OrdererClient) Certificate() tls.Certificate {
    65  	return oc.CommonClient.Certificate()
    66  }