github.com/annchain/OG@v0.0.9/engine/engine.go (about)

     1  package engine
     2  
     3  import (
     4  	"github.com/annchain/OG/communication"
     5  	"github.com/annchain/OG/debug/debuglog"
     6  	"github.com/annchain/OG/eventbus"
     7  	"github.com/annchain/OG/message"
     8  	"github.com/prometheus/common/log"
     9  	"github.com/sirupsen/logrus"
    10  	"sync"
    11  )
    12  
    13  type Engine struct {
    14  	plugins       []communication.GeneralMessageHandlerPlugin
    15  	messageRouter map[message.GeneralMessageType]communication.GeneralMessageEventHandler
    16  	eventBus      *eventbus.DefaultEventBus
    17  	//PeerOutgoing  communication.GeneralPeerCommunicatorOutgoing
    18  	//PeerIncoming  communication.GeneralPeerCommunicatorIncoming
    19  	components []Component
    20  
    21  	quit        chan bool
    22  	quitWg      sync.WaitGroup
    23  	performance map[string]interface{}
    24  }
    25  
    26  func (a *Engine) Name() string {
    27  	return "Engine"
    28  }
    29  
    30  func (a *Engine) GetBenchmarks() map[string]interface{} {
    31  	return a.performance
    32  }
    33  
    34  func (a *Engine) InitDefault() {
    35  	a.messageRouter = make(map[message.GeneralMessageType]communication.GeneralMessageEventHandler)
    36  	a.eventBus = &eventbus.DefaultEventBus{
    37  		NodeLogger: debuglog.NodeLogger{
    38  			Logger: debuglog.SetupOrderedLog(a.Config.Id),
    39  		},
    40  	}
    41  	a.eventBus.InitDefault()
    42  	a.quitWg = sync.WaitGroup{}
    43  	a.quit = make(chan bool)
    44  	a.performance = make(map[string]interface{})
    45  	a.performance["mps"] = uint(0)
    46  
    47  }
    48  
    49  func (a *Engine) RegisterPlugin(plugin communication.GeneralMessageHandlerPlugin) {
    50  	a.plugins = append(a.plugins, plugin)
    51  	handler := plugin.GetMessageEventHandler()
    52  	// message handlers
    53  	for _, msgType := range plugin.SupportedMessageTypes() {
    54  		a.messageRouter[msgType] = handler
    55  	}
    56  	// event handlers
    57  	for _, handler := range plugin.SupportedEventHandlers() {
    58  		a.eventBus.ListenTo(handler)
    59  	}
    60  }
    61  
    62  func (ap *Engine) Start() {
    63  	if ap.quit == nil {
    64  		panic("not initialized.")
    65  	}
    66  	// build eventbus
    67  	ap.eventBus.Build()
    68  	// start the plugins
    69  	for _, plugin := range ap.plugins {
    70  		plugin.Start()
    71  	}
    72  	// start the receiver
    73  	go ap.loop()
    74  	log.Info("Engine Started")
    75  }
    76  
    77  func (ap *Engine) Stop() {
    78  	ap.quit <- true
    79  	ap.quitWg.Wait()
    80  	logrus.Debug("Engine stopped")
    81  }
    82  
    83  func (ap *Engine) loop() {
    84  	for {
    85  		select {
    86  		case <-ap.quit:
    87  			ap.quitWg.Done()
    88  			logrus.Debug("AnnsensusPartner quit")
    89  			return
    90  		case msgEvent := <-ap.PeerIncoming.GetPipeOut():
    91  			ap.HandleGeneralMessage(msgEvent)
    92  			ap.performance["mps"] = ap.performance["mps"].(uint) + 1
    93  		}
    94  	}
    95  }
    96  
    97  func (ap *Engine) HandleGeneralMessage(msgEvent *message.GeneralMessageEvent) {
    98  	generalMessage := msgEvent.Message
    99  	handler, ok := ap.messageRouter[generalMessage.GetType()]
   100  	if !ok {
   101  		logrus.WithField("type", generalMessage.GetType()).Warn("message type not recognized by engine. not registered?")
   102  		return
   103  	}
   104  	handler.Handle(msgEvent)
   105  }