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.

74 lines
2.6 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. //
  40. // XXX: do not call any methods that can block or incur heavy processing.
  41. // https://github.com/tendermint/tendermint/issues/2888
  42. Receive(chID byte, peer Peer, msgBytes []byte)
  43. }
  44. //--------------------------------------
  45. type BaseReactor struct {
  46. service.BaseService // Provides Start, Stop, .Quit
  47. Switch *Switch
  48. }
  49. func NewBaseReactor(name string, impl Reactor) *BaseReactor {
  50. return &BaseReactor{
  51. BaseService: *service.NewBaseService(nil, name, impl),
  52. Switch: nil,
  53. }
  54. }
  55. func (br *BaseReactor) SetSwitch(sw *Switch) {
  56. br.Switch = sw
  57. }
  58. func (*BaseReactor) GetChannels() []*conn.ChannelDescriptor { return nil }
  59. func (*BaseReactor) AddPeer(peer Peer) {}
  60. func (*BaseReactor) RemovePeer(peer Peer, reason interface{}) {}
  61. func (*BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte) {}
  62. func (*BaseReactor) InitPeer(peer Peer) Peer { return peer }