From 2a01ef03948783f2087fef64ea9ca1474c1fcb60 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 24 Nov 2017 18:11:55 +0100 Subject: [PATCH] implemented peersList propagation, each peer connects with each other --- peer/config.json | 4 +-- peer/connections.go | 14 +++++++--- peer/main.go | 9 ++++--- peer/messages.go | 4 +-- peer/peersList.go | 63 ++++++++++++++++++++++++++++++++++----------- 5 files changed, 68 insertions(+), 26 deletions(-) diff --git a/peer/config.json b/peer/config.json index ffef812..da24af1 100755 --- a/peer/config.json +++ b/peer/config.json @@ -1,8 +1,8 @@ { "ip": "127.0.0.1", - "port": "3007", + "port": "3001", "restip": "127.0.0.1", - "restport": "3008", + "restport": "3002", "serverip": "127.0.0.1", "serverport": "3000" } diff --git a/peer/connections.go b/peer/connections.go index d9b5137..dbd30d6 100644 --- a/peer/connections.go +++ b/peer/connections.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "net" + + "github.com/fatih/color" ) func acceptPeers(peer Peer) { @@ -32,18 +34,22 @@ func acceptPeers(peer Peer) { fmt.Println(resp) //newPeer.ID = resp */ - peersList.Peers = append(peersList.Peers, newPeer) + //incomingPeersList.Peers = append(incomingPeersList.Peers, newPeer) + incomingPeersList = appendPeerIfNoExist(incomingPeersList, newPeer) go handleConn(conn, newPeer) } } func connectToPeer(peer Peer) { + color.Green("connecting to new peer") + log.Println("Connecting to new peer: " + peer.IP + ":" + peer.Port) conn, err := net.Dial("tcp", peer.IP+":"+peer.Port) if err != nil { log.Println("Error connecting to: " + peer.IP + ":" + peer.Port) return } peer.Conn = conn - peersList.Peers = append(peersList.Peers, peer) + //outcomingPeersList.Peers = append(outcomingPeersList.Peers, peer) + outcomingPeersList = appendPeerIfNoExist(outcomingPeersList, peer) go handleConn(conn, peer) } func handleConn(conn net.Conn, connPeer Peer) { @@ -51,7 +57,7 @@ func handleConn(conn net.Conn, connPeer Peer) { log.Println("handling conn: " + conn.RemoteAddr().String()) //reply to the conn, send the peerList var msg Msg - msg = msg.construct("PeersList", "here my peersList", peersList) + msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList) msgB := msg.toBytes() _, err := conn.Write(msgB) check(err) @@ -80,5 +86,5 @@ func handleConn(conn net.Conn, connPeer Peer) { //TODO add that if the peer closed is the p2p server, show a warning message at the peer log.Println("Peer: " + conn.RemoteAddr().String() + " connection closed") conn.Close() - //TODO delete the peer from the peersList + //TODO delete the peer from the outcomingPeersList } diff --git a/peer/main.go b/peer/main.go index 0fd503e..dd2f1e0 100644 --- a/peer/main.go +++ b/peer/main.go @@ -49,19 +49,22 @@ func main() { } } thisPeerID = runningPeer.ID - peersList.PeerID = runningPeer.ID + outcomingPeersList.PeerID = runningPeer.ID fmt.Println(runningPeer) - peersList.Peers = append(peersList.Peers, runningPeer) - fmt.Println(peersList) + //outcomingPeersList.Peers = append(outcomingPeersList.Peers, runningPeer) + outcomingPeersList = appendPeerIfNoExist(outcomingPeersList, runningPeer) + fmt.Println(outcomingPeersList) if runningPeer.Role == "server" { go acceptPeers(runningPeer) } if runningPeer.Role == "client" { var newPeer Peer + newPeer.ID = "0" newPeer.IP = config.ServerIP newPeer.Port = config.ServerPort newPeer.Role = "server" connectToPeer(newPeer) + go acceptPeers(runningPeer) } for running { diff --git a/peer/messages.go b/peer/messages.go index 1c129ce..c947ff7 100644 --- a/peer/messages.go +++ b/peer/messages.go @@ -31,7 +31,7 @@ func messageHandler(peer Peer, msg Msg) { fmt.Println(msg.PeersList) //time.Sleep(1000 * time.Millisecond) - updatePeersList(peer.Conn, msg.PeersList) + updateNetworkPeersList(peer.Conn, msg.PeersList) propagatePeersList(peer) printPeersList() break @@ -40,7 +40,7 @@ func messageHandler(peer Peer, msg Msg) { fmt.Println(msg.PeersList) //time.Sleep(1000 * time.Millisecond) - updatePeersList(peer.Conn, msg.PeersList) + updateNetworkPeersList(peer.Conn, msg.PeersList) printPeersList() break case "MyID": diff --git a/peer/peersList.go b/peer/peersList.go index 6bb836c..dbd8869 100644 --- a/peer/peersList.go +++ b/peer/peersList.go @@ -14,8 +14,17 @@ type PeersList struct { Date time.Time `json:"date"` } -//var peersList []Peer -var peersList PeersList +var outcomingPeersList PeersList +var incomingPeersList PeersList +var networkPeersList PeersList //the peers that have been received in the lists from other peers + +/* +a future option is to put: + type PeersList struct { + Incoming PeersList + Outcoming PeersList + } +*/ func peerIsInPeersList(p Peer, pl []Peer) int { r := -1 @@ -26,23 +35,30 @@ func peerIsInPeersList(p Peer, pl []Peer) int { } return r } -func updatePeersList(conn net.Conn, newPeersList PeersList) { +func appendPeerIfNoExist(pl PeersList, p Peer) PeersList { + i := peerIsInPeersList(p, pl.Peers) + if i == -1 { + pl.Peers = append(pl.Peers, p) + } + return pl +} +func updateNetworkPeersList(conn net.Conn, newPeersList PeersList) { for _, peer := range newPeersList.Peers { if getIPPortFromConn(conn) == peer.IP+":"+peer.Port { peer.ID = newPeersList.PeerID color.Yellow(peer.ID) } - i := peerIsInPeersList(peer, peersList.Peers) + i := peerIsInPeersList(peer, networkPeersList.Peers) if i == -1 { - peersList.Peers = append(peersList.Peers, peer) + networkPeersList.Peers = append(networkPeersList.Peers, peer) } else { - fmt.Println(peersList.Peers[i]) - peersList.Peers[i].ID = peer.ID + fmt.Println(networkPeersList.Peers[i]) + networkPeersList.Peers[i].ID = peer.ID } } } func searchPeerAndUpdate(p Peer) { - for _, peer := range peersList.Peers { + for _, peer := range outcomingPeersList.Peers { color.Red(p.IP + ":" + p.Port) color.Yellow(peer.IP + ":" + peer.Port) if p.IP+":"+p.Port == peer.IP+":"+peer.Port { @@ -51,14 +67,14 @@ func searchPeerAndUpdate(p Peer) { } } -//send the peersList to all the peers except the peer that has send the peersList +//send the outcomingPeersList to all the peers except the peer that has send the outcomingPeersList func propagatePeersList(p Peer) { - for _, peer := range peersList.Peers { + for _, peer := range networkPeersList.Peers { if peer.Conn != nil { if peer.ID != p.ID && p.ID != "" { color.Yellow(peer.ID + " - " + p.ID) var msg Msg - msg = msg.construct("PeersList", "here my peersList", peersList) + msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList) msgB := msg.toBytes() _, err := peer.Conn.Write(msgB) check(err) @@ -73,19 +89,36 @@ func propagatePeersList(p Peer) { check(err) */ var msg Msg - msg = msg.construct("PeersList_Response", "here my peersList", peersList) + msg = msg.construct("PeersList_Response", "here my outcomingPeersList", outcomingPeersList) msgB := msg.toBytes() _, err := peer.Conn.Write(msgB) check(err) } + } else { + //connect to peer + if peer.ID != p.ID && peer.ID != runningPeer.ID { + if peerIsInPeersList(peer, outcomingPeersList.Peers) == -1 { + connectToPeer(peer) + } + } + } } } func printPeersList() { fmt.Println("") - color.Green("PEERSLIST:") - color.Green("runningPeer.ID: " + runningPeer.ID) - for _, peer := range peersList.Peers { + color.Blue("runningPeer.ID: " + runningPeer.ID) + color.Green("OUTCOMING PEERSLIST:") + for _, peer := range outcomingPeersList.Peers { + fmt.Println(peer) + } + color.Green("INCOMING PEERSLIST:") + for _, peer := range incomingPeersList.Peers { + fmt.Println(peer) + } + + color.Green("NETWORK PEERSLIST:") + for _, peer := range networkPeersList.Peers { fmt.Println(peer) } fmt.Println("")