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.

125 lines
2.9 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. }
  22. */
  23. func peerIsInPeersList(p Peer, pl []Peer) int {
  24. r := -1
  25. for i, peer := range pl {
  26. if peer.IP+":"+peer.Port == p.IP+":"+p.Port {
  27. r = i
  28. }
  29. }
  30. return r
  31. }
  32. func appendPeerIfNoExist(pl PeersList, p Peer) PeersList {
  33. i := peerIsInPeersList(p, pl.Peers)
  34. if i == -1 {
  35. pl.Peers = append(pl.Peers, p)
  36. }
  37. return pl
  38. }
  39. func updateNetworkPeersList(conn net.Conn, newPeersList PeersList) {
  40. for _, peer := range newPeersList.Peers {
  41. if getIPPortFromConn(conn) == peer.IP+":"+peer.Port {
  42. peer.ID = newPeersList.PeerID
  43. color.Yellow(peer.ID)
  44. }
  45. i := peerIsInPeersList(peer, networkPeersList.Peers)
  46. if i == -1 {
  47. networkPeersList.Peers = append(networkPeersList.Peers, peer)
  48. } else {
  49. fmt.Println(networkPeersList.Peers[i])
  50. networkPeersList.Peers[i].ID = peer.ID
  51. }
  52. }
  53. }
  54. func searchPeerAndUpdate(p Peer) {
  55. for _, peer := range outcomingPeersList.Peers {
  56. color.Red(p.IP + ":" + p.Port)
  57. color.Yellow(peer.IP + ":" + peer.Port)
  58. if p.IP+":"+p.Port == peer.IP+":"+peer.Port {
  59. peer.ID = p.ID
  60. }
  61. }
  62. }
  63. //send the outcomingPeersList to all the peers except the peer that has send the outcomingPeersList
  64. func propagatePeersList(p Peer) {
  65. for _, peer := range networkPeersList.Peers {
  66. if peer.Conn != nil {
  67. if peer.ID != p.ID && p.ID != "" {
  68. color.Yellow(peer.ID + " - " + p.ID)
  69. var msg Msg
  70. msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList)
  71. msgB := msg.toBytes()
  72. _, err := peer.Conn.Write(msgB)
  73. check(err)
  74. } else {
  75. //to the peer that has sent the peerList, we send our ID
  76. /*
  77. var msg Msg
  78. var pl PeersList
  79. msg = msg.construct("MyID", runningPeer.ID, pl)
  80. msgB := msg.toBytes()
  81. _, err := p.Conn.Write(msgB)
  82. check(err)
  83. */
  84. var msg Msg
  85. msg = msg.construct("PeersList_Response", "here my outcomingPeersList", outcomingPeersList)
  86. msgB := msg.toBytes()
  87. _, err := peer.Conn.Write(msgB)
  88. check(err)
  89. }
  90. } else {
  91. //connect to peer
  92. if peer.ID != p.ID && peer.ID != runningPeer.ID {
  93. if peerIsInPeersList(peer, outcomingPeersList.Peers) == -1 {
  94. connectToPeer(peer)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. func printPeersList() {
  101. fmt.Println("")
  102. color.Blue("runningPeer.ID: " + runningPeer.ID)
  103. color.Green("OUTCOMING PEERSLIST:")
  104. for _, peer := range outcomingPeersList.Peers {
  105. fmt.Println(peer)
  106. }
  107. color.Green("INCOMING PEERSLIST:")
  108. for _, peer := range incomingPeersList.Peers {
  109. fmt.Println(peer)
  110. }
  111. color.Green("NETWORK PEERSLIST:")
  112. for _, peer := range networkPeersList.Peers {
  113. fmt.Println(peer)
  114. }
  115. fmt.Println("")
  116. }