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.

90 lines
2.4 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "net"
  7. "github.com/fatih/color"
  8. )
  9. func acceptPeers(peer Peer) {
  10. fmt.Println("accepting peers at: " + peer.Port)
  11. l, err := net.Listen("tcp", peer.IP+":"+peer.Port)
  12. if err != nil {
  13. log.Println("Error accepting peers. Listening port: " + peer.Port)
  14. running = false
  15. }
  16. for running {
  17. conn, err := l.Accept()
  18. if err != nil {
  19. log.Println("Error accepting peers. Error accepting connection")
  20. running = false
  21. }
  22. var newPeer Peer
  23. newPeer.IP = getIPFromConn(conn)
  24. newPeer.Port = getPortFromConn(conn)
  25. newPeer.Conn = conn
  26. /*
  27. //ask to the peer, for the peer ID
  28. resp, err := http.Get("http://" + newPeer.IP + ":" + newPeer.Port)
  29. check(err)
  30. color.Red("-----")
  31. fmt.Println(resp)
  32. //newPeer.ID = resp
  33. */
  34. //incomingPeersList.Peers = append(incomingPeersList.Peers, newPeer)
  35. incomingPeersList = appendPeerIfNoExist(incomingPeersList, newPeer)
  36. go handleConn(conn, newPeer)
  37. }
  38. }
  39. func connectToPeer(peer Peer) {
  40. color.Green("connecting to new peer")
  41. log.Println("Connecting to new peer: " + peer.IP + ":" + peer.Port)
  42. conn, err := net.Dial("tcp", peer.IP+":"+peer.Port)
  43. if err != nil {
  44. log.Println("Error connecting to: " + peer.IP + ":" + peer.Port)
  45. return
  46. }
  47. peer.Conn = conn
  48. //outcomingPeersList.Peers = append(outcomingPeersList.Peers, peer)
  49. outcomingPeersList = appendPeerIfNoExist(outcomingPeersList, peer)
  50. go handleConn(conn, peer)
  51. }
  52. func handleConn(conn net.Conn, connPeer Peer) {
  53. connRunning := true
  54. log.Println("handling conn: " + conn.RemoteAddr().String())
  55. //reply to the conn, send the peerList
  56. var msg Msg
  57. msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList)
  58. msgB := msg.toBytes()
  59. _, err := conn.Write(msgB)
  60. check(err)
  61. for connRunning {
  62. /*
  63. buffer := make([]byte, 1024)
  64. bytesRead, err := conn.Read(buffer)
  65. */
  66. newmsg, err := bufio.NewReader(conn).ReadString('\n')
  67. if err != nil {
  68. log.Println(err)
  69. connRunning = false
  70. } else {
  71. /*
  72. fmt.Println(buffer)
  73. fmt.Println(bytesRead)
  74. */
  75. var msg Msg
  76. //color.Blue(string(buffer[0:bytesRead]))
  77. //msg = msg.createFromBytes([]byte(string(buffer[0:bytesRead])))
  78. msg = msg.createFromBytes([]byte(newmsg))
  79. messageHandler(connPeer, msg)
  80. }
  81. }
  82. //TODO add that if the peer closed is the p2p server, show a warning message at the peer
  83. log.Println("Peer: " + conn.RemoteAddr().String() + " connection closed")
  84. conn.Close()
  85. //TODO delete the peer from the outcomingPeersList
  86. }