github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/sample_clients/broadcast_config/client.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 main
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    27  	"github.com/hyperledger/fabric/orderer/localconfig"
    28  	cb "github.com/hyperledger/fabric/protos/common"
    29  	ab "github.com/hyperledger/fabric/protos/orderer"
    30  )
    31  
    32  var conf *config.TopLevel
    33  var genConf *genesisconfig.Profile
    34  
    35  type broadcastClient struct {
    36  	ab.AtomicBroadcast_BroadcastClient
    37  }
    38  
    39  func (bc *broadcastClient) broadcast(env *cb.Envelope) error {
    40  	var err error
    41  	var resp *ab.BroadcastResponse
    42  
    43  	err = bc.Send(env)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	resp, err = bc.Recv()
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	fmt.Println("Status:", resp)
    54  	return nil
    55  }
    56  
    57  // cmdImpl holds the command and its arguments.
    58  type cmdImpl struct {
    59  	name string
    60  	args argsImpl
    61  }
    62  
    63  // argsImpl holds all the possible arguments for all possible commands.
    64  type argsImpl struct {
    65  	consensusType  string
    66  	creationPolicy string
    67  	chainID        string
    68  }
    69  
    70  func init() {
    71  	conf = config.Load()
    72  	genConf = genesisconfig.Load(genesisconfig.SampleInsecureProfile)
    73  }
    74  
    75  func main() {
    76  	cmd := new(cmdImpl)
    77  	var srv string
    78  
    79  	flag.StringVar(&srv, "server", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort), "The RPC server to connect to.")
    80  	flag.StringVar(&cmd.name, "cmd", "newChain", "The action that this client is requesting via the config transaction.")
    81  	flag.StringVar(&cmd.args.consensusType, "consensusType", genConf.Orderer.OrdererType, "In case of a newChain command, the type of consensus the ordering service is running on.")
    82  	flag.StringVar(&cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", "In case of a newChain command, the chain creation policy this request should be validated against.")
    83  	flag.StringVar(&cmd.args.chainID, "chainID", "NewChannelId", "In case of a newChain command, the chain ID to create.")
    84  	flag.Parse()
    85  
    86  	conn, err := grpc.Dial(srv, grpc.WithInsecure())
    87  	defer func() {
    88  		_ = conn.Close()
    89  	}()
    90  	if err != nil {
    91  		fmt.Println("Error connecting:", err)
    92  		return
    93  	}
    94  
    95  	client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    96  	if err != nil {
    97  		fmt.Println("Error connecting:", err)
    98  		return
    99  	}
   100  
   101  	bc := &broadcastClient{client}
   102  
   103  	switch cmd.name {
   104  	case "newChain":
   105  		env := newChainRequest(cmd.args.consensusType, cmd.args.creationPolicy, cmd.args.chainID)
   106  		fmt.Println("Requesting the creation of chain", cmd.args.chainID)
   107  		fmt.Println(bc.broadcast(env))
   108  	default:
   109  		panic("Invalid command given")
   110  	}
   111  }