Fabric 1.0原始碼分析(38) Peer #BroadcastClient(Broadcast客戶端)
# Fabric 1.0原始碼筆記 之 Peer #BroadcastClient(Broadcast客戶端)
## 1、BroadcastClient概述
BroadcastClient程式碼分佈如下:
* peer/common/ordererclient.go,BroadcastClient介面定義及實現,及BroadcastClient工具函式。
* protos/orderer/ab.pb.go,AtomicBroadcastClient介面定義及實現,AtomicBroadcast_BroadcastClient介面定義及實現。
## 2、BroadcastClient介面定義及實現
### 2.1、BroadcastClient工具函式
```go
//構造broadcastClient
func GetBroadcastClient(orderingEndpoint string, tlsEnabled bool, caFile string) (BroadcastClient, error)
//程式碼在peer/common/ordererclient.go
```
程式碼如下:
```go
func GetBroadcastClient(orderingEndpoint string, tlsEnabled bool, caFile string) (BroadcastClient, error) {
var opts []grpc.DialOption
if tlsEnabled { //檢查TLS
if caFile != "" {
creds, err := credentials.NewClientTLSFromFile(caFile, "")
opts = append(opts, grpc.WithTransportCredentials(creds))
}
} else {
opts = append(opts, grpc.WithInsecure())
}
opts = append(opts, grpc.WithTimeout(3*time.Second))
opts = append(opts, grpc.WithBlock())
conn, err := grpc.Dial(orderingEndpoint, opts...)
//NewAtomicBroadcastClient構造atomicBroadcastClient
//.Broadcast構造atomicBroadcastBroadcastClient
client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
return &broadcastClient{conn: conn, client: client}, nil
}
//程式碼在peer/common/ordererclient.go
```
### 2.2、BroadcastClient介面定義及實現
```go
type BroadcastClient interface {
//Send data to orderer
Send(env *cb.Envelope) error
Close() error
}
type broadcastClient struct {
conn *grpc.ClientConn
client ab.AtomicBroadcast_BroadcastClient
}
//獲取應答
func (s *broadcastClient) getAck() error {
msg, err := s.client.Recv()
if msg.Status != cb.Status_SUCCESS {
//是否成功
}
return nil
}
//向orderer傳送資料
func (s *broadcastClient) Send(env *cb.Envelope) error {
err := s.client.Send(env) //傳送cb.Envelope
err := s.getAck() //獲取應答
return err
}
func (s *broadcastClient) Close() error { //關閉連線
return s.conn.Close()
}
//程式碼在peer/common/ordererclient.go
```
## 3、AtomicBroadcastClient介面定義及實現
### 3.1、AtomicBroadcastClient工具函式
```go
//構造atomicBroadcastClient
func NewAtomicBroadcastClient(cc *grpc.ClientConn) AtomicBroadcastClient {
return &atomicBroadcastClient{cc}
}
//程式碼在protos/orderer/ab.pb.go
```
### 3.2、AtomicBroadcastClient介面定義
```go
type AtomicBroadcastClient interface {
//廣播
Broadcast(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_BroadcastClient, error)
//投遞
Deliver(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_DeliverClient, error)
}
//程式碼在protos/orderer/ab.pb.go
```
### 3.3、AtomicBroadcastClient介面實現
AtomicBroadcastClient介面實現,即atomicBroadcastClient結構體及方法。
```go
type atomicBroadcastClient struct {
cc *grpc.ClientConn
}
//構造atomicBroadcastBroadcastClient
func (c *atomicBroadcastClient) Broadcast(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_BroadcastClient, error) {
stream, err := grpc.NewClientStream(ctx, &_AtomicBroadcast_serviceDesc.Streams[0], c.cc, "/orderer.AtomicBroadcast/Broadcast", opts...)
x := &atomicBroadcastBroadcastClient{stream}
}
func (c *atomicBroadcastClient) Deliver(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_DeliverClient, error) {
stream, err := grpc.NewClientStream(ctx, &_AtomicBroadcast_serviceDesc.Streams[1], c.cc, "/orderer.AtomicBroadcast/Deliver", opts...)
x := &atomicBroadcastDeliverClient{stream}
}
var _AtomicBroadcast_serviceDesc = grpc.ServiceDesc{
ServiceName: "orderer.AtomicBroadcast",
HandlerType: (*AtomicBroadcastServer)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{
{
StreamName: "Broadcast",
Handler: _AtomicBroadcast_Broadcast_Handler,
ServerStreams: true,
ClientStreams: true,
},
{
StreamName: "Deliver",
Handler: _AtomicBroadcast_Deliver_Handler,
ServerStreams: true,
ClientStreams: true,
},
},
Metadata: "orderer/ab.proto",
}
//程式碼在protos/orderer/ab.pb.go
```
### 3.4、atomicBroadcastBroadcastClient
```go
type AtomicBroadcast_BroadcastClient interface {
Send(*common.Envelope) error //傳送
Recv() (*BroadcastResponse, error) //接收
grpc.ClientStream
}
type atomicBroadcastBroadcastClient struct {
grpc.ClientStream
}
func (x *atomicBroadcastBroadcastClient) Send(m *common.Envelope) error {
return x.ClientStream.SendMsg(m)
}
func (x *atomicBroadcastBroadcastClient) Recv() (*BroadcastResponse, error) {
m := new(BroadcastResponse)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
//程式碼在protos/orderer/ab.pb.go
```
### 3.5、atomicBroadcastDeliverClient
```go
type AtomicBroadcast_DeliverClient interface {
Send(*common.Envelope) error //傳送
Recv() (*DeliverResponse, error) //接收
grpc.ClientStream
}
type atomicBroadcastDeliverClient struct {
grpc.ClientStream
}
func (x *atomicBroadcastDeliverClient) Send(m *common.Envelope) error {
return x.ClientStream.SendMsg(m)
}
func (x *atomicBroadcastDeliverClient) Recv() (*DeliverResponse, error) {
m := new(DeliverResponse)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
//程式碼在protos/orderer/ab.pb.go
```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(32) Peer #peer node start命令實現原始碼
- Fabric 1.0原始碼分析(33) Peer #peer channel命令及子命令實現原始碼
- Fabric 1.0原始碼分析(34) Peer #peer chaincode命令及子命令實現原始碼AI
- Fabric 1.0原始碼分析(17)gossip(流言演算法) #deliverclient(deliver客戶端)原始碼Go演算法client客戶端
- Fabric 1.0原始碼分析(32)Peer #peer根命令入口及載入子命令原始碼
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric1.4原始碼解析:客戶端建立通道過程原始碼客戶端
- Broadcast原始碼分析AST原始碼
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Tars-Java客戶端原始碼分析Java客戶端原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Netty原始碼分析(三):客戶端啟動Netty原始碼客戶端
- MapReduce——客戶端提交任務原始碼分析客戶端原始碼
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(16)gossip(流言演算法) #GossipServer(Gossip服務端)原始碼Go演算法Server服務端
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)原始碼
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- Fabric1.4原始碼解析:Peer節點加入通道原始碼