4 cmn "github.com/tendermint/tmlibs/common"
6 "github.com/bytom/p2p/connection"
9 //Reactor is responsible for handling incoming messages of one or more `Channels`
10 type Reactor interface {
11 cmn.Service // Start, Stop
13 // SetSwitch allows setting a switch.
16 // GetChannels returns the list of channel descriptors.
17 GetChannels() []*connection.ChannelDescriptor
19 // AddPeer is called by the switch when a new peer is added.
20 AddPeer(peer *Peer) error
22 // RemovePeer is called by the switch when the peer is stopped (due to error
24 RemovePeer(peer *Peer, reason interface{})
26 // Receive is called when msgBytes is received from peer.
28 // NOTE reactor can not keep msgBytes around after Receive completes without
31 // CONTRACT: msgBytes are not nil.
32 Receive(chID byte, peer *Peer, msgBytes []byte)
35 //BaseReactor base service of a reactor
36 type BaseReactor struct {
37 cmn.BaseService // Provides Start, Stop, .Quit
41 //NewBaseReactor create new base Reactor
42 func NewBaseReactor(name string, impl Reactor) *BaseReactor {
44 BaseService: *cmn.NewBaseService(nil, name, impl),
49 //SetSwitch setting a switch for reactor
50 func (br *BaseReactor) SetSwitch(sw *Switch) {
54 //GetChannels returns the list of channel descriptors
55 func (*BaseReactor) GetChannels() []*connection.ChannelDescriptor { return nil }
57 //AddPeer is called by the switch when a new peer is added
58 func (*BaseReactor) AddPeer(peer *Peer) {}
60 //RemovePeer is called by the switch when the peer is stopped (due to error or other reason)
61 func (*BaseReactor) RemovePeer(peer *Peer, reason interface{}) {}
63 //Receive is called when msgBytes is received from peer
64 func (*BaseReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {}