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.

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