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.

116 lines
2.3 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. // Create & start node
  69. n := NewNode()
  70. l := p2p.NewDefaultListener("tcp", config.Config.LAddr)
  71. n.AddListener(l)
  72. n.Start()
  73. // Seed?
  74. if config.Config.Seed != "" {
  75. peer, err := n.sw.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.Seed))
  76. if err != nil {
  77. log.Error("Error dialing seed: %v", err)
  78. //n.book.MarkAttempt(addr)
  79. return
  80. } else {
  81. log.Info("Connected to seed: %v", peer)
  82. }
  83. }
  84. // Sleep forever and then...
  85. trapSignal(func() {
  86. n.Stop()
  87. })
  88. }
  89. func trapSignal(cb func()) {
  90. c := make(chan os.Signal, 1)
  91. signal.Notify(c, os.Interrupt)
  92. go func() {
  93. for sig := range c {
  94. log.Info("captured %v, exiting..", sig)
  95. cb()
  96. os.Exit(1)
  97. }
  98. }()
  99. select {}
  100. }