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.

119 lines
2.4 KiB

10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. pexReactor *p2p.PEXReactor
  14. }
  15. func NewNode() *Node {
  16. sw := p2p.NewSwitch(nil) // XXX create and pass reactors
  17. book := p2p.NewAddrBook(config.RootDir + "/addrbook.json")
  18. pexReactor := p2p.NewPEXReactor(sw, book)
  19. return &Node{
  20. sw: sw,
  21. book: book,
  22. pexReactor: pexReactor,
  23. }
  24. }
  25. func (n *Node) Start() {
  26. log.Info("Starting node")
  27. for _, l := range n.lz {
  28. go n.inboundConnectionRoutine(l)
  29. }
  30. n.sw.Start()
  31. n.book.Start()
  32. n.pexReactor.Start()
  33. }
  34. func (n *Node) Stop() {
  35. log.Info("Stopping node")
  36. // TODO: gracefully disconnect from peers.
  37. n.sw.Stop()
  38. n.book.Stop()
  39. n.pexReactor.Stop()
  40. }
  41. // Add a Listener to accept inbound peer connections.
  42. func (n *Node) AddListener(l p2p.Listener) {
  43. log.Info("Added %v", l)
  44. n.lz = append(n.lz, l)
  45. n.book.AddOurAddress(l.ExternalAddress())
  46. }
  47. func (n *Node) inboundConnectionRoutine(l p2p.Listener) {
  48. for {
  49. inConn, ok := <-l.Connections()
  50. if !ok {
  51. break
  52. }
  53. // New inbound connection!
  54. peer, err := n.sw.AddPeerWithConnection(inConn, false)
  55. if err != nil {
  56. log.Info("Ignoring error from inbound connection: %v\n%v",
  57. peer, err)
  58. continue
  59. }
  60. // NOTE: We don't yet have the external address of the
  61. // remote (if they have a listener at all).
  62. // PEXReactor's pexRoutine will handle that.
  63. }
  64. // cleanup
  65. }
  66. //-----------------------------------------------------------------------------
  67. func main() {
  68. // Parse config flags
  69. config.ParseFlags()
  70. // Create & start node
  71. n := NewNode()
  72. l := p2p.NewDefaultListener("tcp", config.Config.LAddr)
  73. n.AddListener(l)
  74. n.Start()
  75. // Seed?
  76. if config.Config.Seed != "" {
  77. peer, err := n.sw.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.Seed))
  78. if err != nil {
  79. log.Error("Error dialing seed: %v", err)
  80. //n.book.MarkAttempt(addr)
  81. return
  82. } else {
  83. log.Info("Connected to seed: %v", peer)
  84. }
  85. }
  86. // Sleep forever and then...
  87. trapSignal(func() {
  88. n.Stop()
  89. })
  90. }
  91. func trapSignal(cb func()) {
  92. c := make(chan os.Signal, 1)
  93. signal.Notify(c, os.Interrupt)
  94. go func() {
  95. for sig := range c {
  96. log.Info("captured %v, exiting..", sig)
  97. cb()
  98. os.Exit(1)
  99. }
  100. }()
  101. select {}
  102. }