You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package p2p
  2. import (
  3. "github.com/tendermint/tendermint/libs/service"
  4. "github.com/tendermint/tendermint/p2p/conn"
  5. )
  6. // Reactor is responsible for handling incoming messages on one or more
  7. // Channel. Switch calls GetChannels when reactor is added to it. When a new
  8. // peer joins our node, InitPeer and AddPeer are called. RemovePeer is called
  9. // when the peer is stopped. Receive is called when a message is received on a
  10. // channel associated with this reactor.
  11. //
  12. // Peer#Send or Peer#TrySend should be used to send the message to a peer.
  13. type Reactor interface {
  14. service.Service // Start, Stop
  15. // SetSwitch allows setting a switch.
  16. SetSwitch(*Switch)
  17. // GetChannels returns the list of MConnection.ChannelDescriptor. Make sure
  18. // that each ID is unique across all the reactors added to the switch.
  19. GetChannels() []*conn.ChannelDescriptor
  20. // InitPeer is called by the switch before the peer is started. Use it to
  21. // initialize data for the peer (e.g. peer state).
  22. //
  23. // NOTE: The switch won't call AddPeer nor RemovePeer if it fails to start
  24. // the peer. Do not store any data associated with the peer in the reactor
  25. // itself unless you don't want to have a state, which is never cleaned up.
  26. InitPeer(peer Peer) Peer
  27. // AddPeer is called by the switch after the peer is added and successfully
  28. // started. Use it to start goroutines communicating with the peer.
  29. AddPeer(peer Peer)
  30. // RemovePeer is called by the switch when the peer is stopped (due to error
  31. // or other reason).
  32. RemovePeer(peer Peer, reason interface{})
  33. // Receive is called by the switch when msgBytes is received from the peer.
  34. //
  35. // NOTE reactor can not keep msgBytes around after Receive completes without
  36. // copying.
  37. //
  38. // CONTRACT: msgBytes are not nil.
  39. Receive(chID byte, peer Peer, msgBytes []byte)
  40. }
  41. //--------------------------------------
  42. type BaseReactor struct {
  43. service.BaseService // Provides Start, Stop, .Quit
  44. Switch *Switch
  45. }
  46. func NewBaseReactor(name string, impl Reactor) *BaseReactor {
  47. return &BaseReactor{
  48. BaseService: *service.NewBaseService(nil, name, impl),
  49. Switch: nil,
  50. }
  51. }
  52. func (br *BaseReactor) SetSwitch(sw *Switch) {
  53. br.Switch = sw
  54. }
  55. func (*BaseReactor) GetChannels() []*conn.ChannelDescriptor { return nil }
  56. func (*BaseReactor) AddPeer(peer Peer) {}
  57. func (*BaseReactor) RemovePeer(peer Peer, reason interface{}) {}
  58. func (*BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte) {}
  59. func (*BaseReactor) InitPeer(peer Peer) Peer { return peer }