github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"time"
    22  
    23  	cb "github.com/hyperledger/fabric/protos/common"
    24  	ab "github.com/hyperledger/fabric/protos/orderer"
    25  	"github.com/spf13/viper"
    26  	"golang.org/x/net/context"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  type BroadcastClient interface {
    31  	//Send data to orderer
    32  	Send(env *cb.Envelope) error
    33  	Close() error
    34  }
    35  
    36  type broadcastClient struct {
    37  	conn   *grpc.ClientConn
    38  	client ab.AtomicBroadcast_BroadcastClient
    39  }
    40  
    41  // GetBroadcastClient creates a simple instance of the BroadcastClient interface
    42  func GetBroadcastClient() (BroadcastClient, error) {
    43  	var orderer string
    44  	if viper.GetBool("peer.committer.enabled") {
    45  		orderer = viper.GetString("peer.committer.ledger.orderer")
    46  	}
    47  
    48  	if orderer == "" {
    49  		return nil, fmt.Errorf("Can't get orderer address")
    50  	}
    51  
    52  	var opts []grpc.DialOption
    53  	opts = append(opts, grpc.WithInsecure())
    54  	opts = append(opts, grpc.WithTimeout(3*time.Second))
    55  	opts = append(opts, grpc.WithBlock())
    56  
    57  	conn, err := grpc.Dial(orderer, opts...)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
    60  	}
    61  	client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    62  	if err != nil {
    63  		conn.Close()
    64  		return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
    65  	}
    66  
    67  	return &broadcastClient{conn: conn, client: client}, nil
    68  }
    69  
    70  func (s *broadcastClient) getAck() error {
    71  	msg, err := s.client.Recv()
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if msg.Status != cb.Status_SUCCESS {
    76  		return fmt.Errorf("Got unexpected status: %v", msg.Status)
    77  	}
    78  	return nil
    79  }
    80  
    81  //Send data to orderer
    82  func (s *broadcastClient) Send(env *cb.Envelope) error {
    83  	if err := s.client.Send(env); err != nil {
    84  		return fmt.Errorf("Could not send :%s)", err)
    85  	}
    86  
    87  	err := s.getAck()
    88  
    89  	return err
    90  }
    91  
    92  func (s *broadcastClient) Close() error {
    93  	return s.conn.Close()
    94  }