This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
base_reactor.go
64 lines (49 loc) · 1.83 KB
/
base_reactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package p2p
import (
cmn "github.com/tendermint/tmlibs/common"
"github.com/bytom/bytom/p2p/connection"
)
//Reactor is responsible for handling incoming messages of one or more `Channels`
type Reactor interface {
cmn.Service // Start, Stop
// SetSwitch allows setting a switch.
SetSwitch(*Switch)
// GetChannels returns the list of channel descriptors.
GetChannels() []*connection.ChannelDescriptor
// AddPeer is called by the switch when a new peer is added.
AddPeer(peer *Peer) error
// RemovePeer is called by the switch when the peer is stopped (due to error
// or other reason).
RemovePeer(peer *Peer, reason interface{})
// Receive is called when msgBytes is received from peer.
//
// NOTE reactor can not keep msgBytes around after Receive completes without
// copying.
//
// CONTRACT: msgBytes are not nil.
Receive(chID byte, peer *Peer, msgBytes []byte)
}
//BaseReactor base service of a reactor
type BaseReactor struct {
cmn.BaseService // Provides Start, Stop, .Quit
Switch *Switch
}
//NewBaseReactor create new base Reactor
func NewBaseReactor(name string, impl Reactor) *BaseReactor {
return &BaseReactor{
BaseService: *cmn.NewBaseService(nil, name, impl),
Switch: nil,
}
}
//SetSwitch setting a switch for reactor
func (br *BaseReactor) SetSwitch(sw *Switch) {
br.Switch = sw
}
//GetChannels returns the list of channel descriptors
func (*BaseReactor) GetChannels() []*connection.ChannelDescriptor { return nil }
//AddPeer is called by the switch when a new peer is added
func (*BaseReactor) AddPeer(peer *Peer) {}
//RemovePeer is called by the switch when the peer is stopped (due to error or other reason)
func (*BaseReactor) RemovePeer(peer *Peer, reason interface{}) {}
//Receive is called when msgBytes is received from peer
func (*BaseReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {}