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.

150 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package main
  2. import (
  3. "os"
  4. "os/signal"
  5. "github.com/tendermint/tendermint/config"
  6. "github.com/tendermint/tendermint/consensus"
  7. "github.com/tendermint/tendermint/p2p"
  8. )
  9. type Node struct {
  10. lz []p2p.Listener
  11. sw *p2p.Switch
  12. book *p2p.AddrBook
  13. pmgr *p2p.PeerManager
  14. }
  15. func NewNode() *Node {
  16. // Define channels for our app
  17. chDescs := []*p2p.ChannelDescriptor{
  18. // PEX
  19. &p2p.ChannelDescriptor{
  20. Id: p2p.PexCh,
  21. SendQueueCapacity: 2,
  22. RecvQueueCapacity: 2,
  23. RecvBufferSize: 1024,
  24. DefaultPriority: 1,
  25. },
  26. // CONSENSUS
  27. &p2p.ChannelDescriptor{
  28. Id: consensus.ProposalCh,
  29. SendQueueCapacity: 2,
  30. RecvQueueCapacity: 10,
  31. RecvBufferSize: 10240,
  32. DefaultPriority: 5,
  33. },
  34. &p2p.ChannelDescriptor{
  35. Id: consensus.KnownPartsCh,
  36. SendQueueCapacity: 2,
  37. RecvQueueCapacity: 10,
  38. RecvBufferSize: 1024,
  39. DefaultPriority: 5,
  40. },
  41. &p2p.ChannelDescriptor{
  42. Id: consensus.VoteCh,
  43. SendQueueCapacity: 100,
  44. RecvQueueCapacity: 1000,
  45. RecvBufferSize: 10240,
  46. DefaultPriority: 5,
  47. },
  48. // TODO: MEMPOOL
  49. }
  50. sw := p2p.NewSwitch(chDescs)
  51. book := p2p.NewAddrBook(config.RootDir + "/addrbook.json")
  52. pmgr := p2p.NewPeerManager(sw, book)
  53. return &Node{
  54. sw: sw,
  55. book: book,
  56. pmgr: pmgr,
  57. }
  58. }
  59. func (n *Node) Start() {
  60. log.Info("Starting node")
  61. for _, l := range n.lz {
  62. go n.inboundConnectionRoutine(l)
  63. }
  64. n.sw.Start()
  65. n.book.Start()
  66. n.pmgr.Start()
  67. }
  68. func (n *Node) Stop() {
  69. log.Info("Stopping node")
  70. // TODO: gracefully disconnect from peers.
  71. n.sw.Stop()
  72. n.book.Stop()
  73. n.pmgr.Stop()
  74. }
  75. // Add a Listener to accept inbound peer connections.
  76. func (n *Node) AddListener(l p2p.Listener) {
  77. log.Info("Added %v", l)
  78. n.lz = append(n.lz, l)
  79. n.book.AddOurAddress(l.ExternalAddress())
  80. }
  81. func (n *Node) inboundConnectionRoutine(l p2p.Listener) {
  82. for {
  83. inConn, ok := <-l.Connections()
  84. if !ok {
  85. break
  86. }
  87. // New inbound connection!
  88. peer, err := n.sw.AddPeerWithConnection(inConn, false)
  89. if err != nil {
  90. log.Info("Ignoring error from inbound connection: %v\n%v",
  91. peer, err)
  92. continue
  93. }
  94. // NOTE: We don't yet have the external address of the
  95. // remote (if they have a listener at all).
  96. // PeerManager's pexRoutine will handle that.
  97. }
  98. // cleanup
  99. }
  100. //-----------------------------------------------------------------------------
  101. func main() {
  102. // Create & start node
  103. n := NewNode()
  104. l := p2p.NewDefaultListener("tcp", config.Config.LAddr)
  105. n.AddListener(l)
  106. n.Start()
  107. // Seed?
  108. if config.Config.Seed != "" {
  109. peer, err := n.sw.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.Seed))
  110. if err != nil {
  111. log.Error("Error dialing seed: %v", err)
  112. //n.book.MarkAttempt(addr)
  113. return
  114. } else {
  115. log.Info("Connected to seed: %v", peer)
  116. }
  117. }
  118. // Sleep forever and then...
  119. trapSignal(func() {
  120. n.Stop()
  121. })
  122. }
  123. func trapSignal(cb func()) {
  124. c := make(chan os.Signal, 1)
  125. signal.Notify(c, os.Interrupt)
  126. go func() {
  127. for sig := range c {
  128. log.Info("captured %v, exiting..", sig)
  129. cb()
  130. os.Exit(1)
  131. }
  132. }()
  133. select {}
  134. }