github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/orderer/server.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  	"github.com/hyperledger/fabric/common/crypto"
    21  	"github.com/hyperledger/fabric/orderer/common/broadcast"
    22  	"github.com/hyperledger/fabric/orderer/common/deliver"
    23  	"github.com/hyperledger/fabric/orderer/configupdate"
    24  	"github.com/hyperledger/fabric/orderer/multichain"
    25  	ab "github.com/hyperledger/fabric/protos/orderer"
    26  )
    27  
    28  type configUpdateSupport struct {
    29  	multichain.Manager
    30  }
    31  
    32  func (cus configUpdateSupport) GetChain(chainID string) (configupdate.Support, bool) {
    33  	return cus.Manager.GetChain(chainID)
    34  }
    35  
    36  type broadcastSupport struct {
    37  	multichain.Manager
    38  	broadcast.ConfigUpdateProcessor
    39  }
    40  
    41  func (bs broadcastSupport) GetChain(chainID string) (broadcast.Support, bool) {
    42  	return bs.Manager.GetChain(chainID)
    43  }
    44  
    45  type deliverSupport struct {
    46  	multichain.Manager
    47  }
    48  
    49  func (bs deliverSupport) GetChain(chainID string) (deliver.Support, bool) {
    50  	return bs.Manager.GetChain(chainID)
    51  }
    52  
    53  type server struct {
    54  	bh broadcast.Handler
    55  	dh deliver.Handler
    56  }
    57  
    58  // NewServer creates an ab.AtomicBroadcastServer based on the broadcast target and ledger Reader
    59  func NewServer(ml multichain.Manager, signer crypto.LocalSigner) ab.AtomicBroadcastServer {
    60  	logger.Infof("Starting orderer")
    61  
    62  	s := &server{
    63  		dh: deliver.NewHandlerImpl(deliverSupport{Manager: ml}),
    64  		bh: broadcast.NewHandlerImpl(broadcastSupport{
    65  			Manager:               ml,
    66  			ConfigUpdateProcessor: configupdate.New(ml.SystemChannelID(), configUpdateSupport{Manager: ml}, signer),
    67  		}),
    68  	}
    69  	return s
    70  }
    71  
    72  // Broadcast receives a stream of messages from a client for ordering
    73  func (s *server) Broadcast(srv ab.AtomicBroadcast_BroadcastServer) error {
    74  	logger.Debugf("Starting new Broadcast handler")
    75  	return s.bh.Handle(srv)
    76  }
    77  
    78  // Deliver sends a stream of blocks to a client after ordering
    79  func (s *server) Deliver(srv ab.AtomicBroadcast_DeliverServer) error {
    80  	logger.Debugf("Starting new Deliver handler")
    81  	return s.dh.Handle(srv)
    82  }