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.

153 lines
3.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/fatih/color"
  7. )
  8. type PeersList struct {
  9. PeerID string
  10. Peers []Peer `json:"peerslist"`
  11. Date time.Time `json:"date"`
  12. }
  13. var outcomingPeersList PeersList
  14. var incomingPeersList PeersList
  15. var networkPeersList PeersList //the peers that have been received in the lists from other peers
  16. /*
  17. a future option is to put:
  18. type PeersList struct {
  19. Incoming PeersList
  20. Outcoming PeersList
  21. Network PeersList
  22. }
  23. */
  24. func peerIsInPeersList(p Peer, pl []Peer) int {
  25. r := -1
  26. for i, peer := range pl {
  27. if peer.IP+":"+peer.Port == p.IP+":"+p.Port {
  28. r = i
  29. }
  30. }
  31. return r
  32. }
  33. func deletePeerFromPeersList(p Peer, pl *PeersList) {
  34. i := peerIsInPeersList(p, pl.Peers)
  35. if i != -1 {
  36. //delete peer from pl.Peers
  37. pl.Peers = append(pl.Peers[:i], pl.Peers[i+1:]...)
  38. }
  39. }
  40. func appendPeerIfNoExist(pl PeersList, p Peer) PeersList {
  41. i := peerIsInPeersList(p, pl.Peers)
  42. if i == -1 {
  43. pl.Peers = append(pl.Peers, p)
  44. }
  45. return pl
  46. }
  47. func updateNetworkPeersList(conn net.Conn, newPeersList PeersList) {
  48. for _, peer := range newPeersList.Peers {
  49. if getIPPortFromConn(conn) == peer.IP+":"+peer.Port {
  50. peer.ID = newPeersList.PeerID
  51. color.Yellow(peer.ID)
  52. }
  53. i := peerIsInPeersList(peer, networkPeersList.Peers)
  54. if i == -1 {
  55. networkPeersList.Peers = append(networkPeersList.Peers, peer)
  56. } else {
  57. fmt.Println(networkPeersList.Peers[i])
  58. networkPeersList.Peers[i].ID = peer.ID
  59. }
  60. }
  61. }
  62. func searchPeerAndUpdate(p Peer) {
  63. for _, peer := range outcomingPeersList.Peers {
  64. color.Red(p.IP + ":" + p.Port)
  65. color.Yellow(peer.IP + ":" + peer.Port)
  66. if p.IP+":"+p.Port == peer.IP+":"+peer.Port {
  67. peer.ID = p.ID
  68. }
  69. }
  70. }
  71. //send the outcomingPeersList to all the peers except the peer p that has send the outcomingPeersList
  72. func propagatePeersList(p Peer) {
  73. for _, peer := range networkPeersList.Peers {
  74. if peer.Conn != nil {
  75. if peer.ID != p.ID && p.ID != "" {
  76. color.Yellow(peer.ID + " - " + p.ID)
  77. var msg Msg
  78. msg.construct("PeersList", "here my outcomingPeersList")
  79. msg.PeersList = outcomingPeersList
  80. msgB := msg.toBytes()
  81. _, err := peer.Conn.Write(msgB)
  82. check(err)
  83. } else {
  84. /*
  85. for the moment, this is not being called, due that in the IncomingPeersList,
  86. there is no peer.ID, so in the comparation wih the peer that has send the
  87. peersList, is comparing ID with "", so nevere enters this 'else' section
  88. maybe it's not needed. TODO check if it's needed the PeerList_Response
  89. For the moment is working without it
  90. */
  91. //to the peer that has sent the peerList, we send our PeersList
  92. var msg Msg
  93. msg.construct("PeersList_Response", "here my outcomingPeersList")
  94. msg.PeersList = outcomingPeersList
  95. msgB := msg.toBytes()
  96. _, err := peer.Conn.Write(msgB)
  97. check(err)
  98. }
  99. } else {
  100. //connect to peer
  101. if peer.ID != p.ID && peer.ID != runningPeer.ID {
  102. if peerIsInPeersList(peer, outcomingPeersList.Peers) == -1 {
  103. color.Red("no connection, connecting to peer: " + peer.Port)
  104. connectToPeer(peer)
  105. }
  106. }
  107. }
  108. }
  109. }
  110. func printPeersList() {
  111. fmt.Println("")
  112. color.Blue("runningPeer.ID: " + runningPeer.ID)
  113. color.Green("OUTCOMING PEERSLIST:")
  114. for _, peer := range outcomingPeersList.Peers {
  115. fmt.Println(peer)
  116. }
  117. color.Green("INCOMING PEERSLIST:")
  118. for _, peer := range incomingPeersList.Peers {
  119. fmt.Println(peer)
  120. }
  121. color.Green("NETWORK PEERSLIST:")
  122. for _, peer := range networkPeersList.Peers {
  123. fmt.Println(peer)
  124. }
  125. fmt.Println("")
  126. }
  127. //send the block to all the peers of the outcomingPeersList
  128. func propagateBlock(b Block) {
  129. //prepare the msg to send to all connected peers
  130. var msg Msg
  131. msg.construct("Block", "new block")
  132. msg.Block = b
  133. msgB := msg.toBytes()
  134. for _, peer := range outcomingPeersList.Peers {
  135. if peer.Conn != nil {
  136. _, err := peer.Conn.Write(msgB)
  137. check(err)
  138. }
  139. }
  140. }