Fabric 1.0原始碼分析(13)events(事件服務)

尹成發表於2018-05-20
# Fabric 1.0原始碼筆記 之 events(事件服務)

## 1、events概述

events程式碼分佈在events/producer和events/consumer目錄下,目錄結構如下:

* events/producer目錄:生產者,提供事件伺服器。
    * producer.go,EventsServer結構體及方法。
    * events.go,eventProcessor結構體及方法,handlerList介面及實現。
    * handler.go,handler結構體及方法。
* events/consumer目錄:消費者,獲取事件。

## 2、producer(生產者)

### 2.1、EventsServer結構體及方法

EventsServer結構體定義:(空)

```go
type EventsServer struct {
}

//全域性事件伺服器globalEventsServer
var globalEventsServer *EventsServer
//程式碼在events/producer/producer.go
```

涉及方法如下:

```go
//globalEventsServer建立並初始化
func NewEventsServer(bufferSize uint, timeout time.Duration) *EventsServer
//接收訊息並處理
func (p *EventsServer) Chat(stream pb.Events_ChatServer) error
//程式碼在events/producer/producer.go
```

### 2.2、eventProcessor結構體及方法(事件處理器)

eventProcessor結構體定義:

```go
type eventProcessor struct {
    sync.RWMutex
    eventConsumers map[pb.EventType]handlerList
    eventChannel chan *pb.Event
    timeout time.Duration //生產者傳送事件的超時時間
}

//全域性事件處理器gEventProcessor
var gEventProcessor *eventProcessor
//程式碼在events/producer/events.go
```

涉及方法如下:

```go
func (ep *eventProcessor) start() //啟動並處理事件,從ep.eventChannel通道接收訊息並處理
//初始化gEventProcessor,並新增訊息型別,併發啟動start()
func initializeEvents(bufferSize uint, tout time.Duration)
//新增訊息型別至gEventProcessor.eventConsumers,並初始化handlerList
func AddEventType(eventType pb.EventType) error
//繫結Interest和handler
func registerHandler(ie *pb.Interest, h *handler) error
//取消繫結Interest和handler
func deRegisterHandler(ie *pb.Interest, h *handler) error
func Send(e *pb.Event) error //Event傳送至gEventProcessor.eventChannel
//程式碼在events/producer/events.go
```

### 2.3、handlerList介面及實現

#### 2.3.1、handlerList介面定義(handler列表)

```go
type handlerList interface {
    add(ie *pb.Interest, h *handler) (bool, error) //新增
    del(ie *pb.Interest, h *handler) (bool, error) //刪除
    foreach(ie *pb.Event, action func(h *handler)) //遍歷
}
//程式碼在events/producer/events.go
```

#### 2.3.2、handlerList介面實現

```go
type genericHandlerList struct {
    sync.RWMutex
    handlers map[*handler]bool
}

type chaincodeHandlerList struct {
    sync.RWMutex
    handlers map[string]map[string]map[*handler]bool
}
//程式碼在events/producer/events.go
```

### 2.4、handler結構體及方法

handler結構體定義:

```go
type handler struct {
    ChatStream pb.Events_ChatServer
    interestedEvents map[string]*pb.Interest
}
//程式碼在events/producer/handler.go
```

補充pb.Events_ChatServer和pb.Interest(關注)定義:

```go
type Events_ChatServer interface {
    Send(*Event) error
    Recv() (*SignedEvent, error)
    grpc.ServerStream //type ServerStream interface
}

Interest struct {
    EventType EventType //type EventType int32
    RegInfo isInterest_RegInfo //type isInterest_RegInfo interface
    ChainID string
}

type EventType int32

const (
    EventType_REGISTER EventType = 0
    EventType_BLOCK EventType = 1
    EventType_CHAINCODE EventType = 2
    EventType_REJECTION EventType = 3
)
//程式碼在protos/peer/events.pb.go
```

handler結構體方法如下:

```go
func newEventHandler(stream pb.Events_ChatServer) (*handler, error) //構造handler
func (d *handler) Stop() error //停止handler,取消所有handler註冊
func getInterestKey(interest pb.Interest) string //獲取interest.EventType
func (d *handler) register(iMsg []*pb.Interest) error //逐一繫結Interest和handler
func (d *handler) deregister(iMsg []*pb.Interest) error //逐一取消繫結Interest和handler
func (d *handler) deregisterAll() //取消所有handler繫結
func (d *handler) HandleMessage(msg *pb.SignedEvent) error //處理訊息
func (d *handler) SendMessage(msg *pb.Event) error //通過流向遠端PEER傳送訊息
func validateEventMessage(signedEvt *pb.SignedEvent) (*pb.Event, error) //驗證事件訊息
//程式碼在events/producer/handler.go
```

補充pb.Event和pb.SignedEvent:

```go
type Event struct {
    Event isEvent_Event //type isEvent_Event interface
    Creator []byte //事件建立者
}

SignedEvent struct {
    Signature []byte //在事件位元組上的簽名
    EventBytes []byte //事件物件序列化,即type Event struct
}
//程式碼在protos/peer/events.pb.go
```

## 3、consumer(消費者)

待補充。






網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章