Browse Source

implemented peersList propagation, each peer connects with each other

master
arnaucode 7 years ago
parent
commit
2a01ef0394
5 changed files with 68 additions and 26 deletions
  1. +2
    -2
      peer/config.json
  2. +10
    -4
      peer/connections.go
  3. +6
    -3
      peer/main.go
  4. +2
    -2
      peer/messages.go
  5. +48
    -15
      peer/peersList.go

+ 2
- 2
peer/config.json

@ -1,8 +1,8 @@
{ {
"ip": "127.0.0.1", "ip": "127.0.0.1",
"port": "3007",
"port": "3001",
"restip": "127.0.0.1", "restip": "127.0.0.1",
"restport": "3008",
"restport": "3002",
"serverip": "127.0.0.1", "serverip": "127.0.0.1",
"serverport": "3000" "serverport": "3000"
} }

+ 10
- 4
peer/connections.go

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"github.com/fatih/color"
) )
func acceptPeers(peer Peer) { func acceptPeers(peer Peer) {
@ -32,18 +34,22 @@ func acceptPeers(peer Peer) {
fmt.Println(resp) fmt.Println(resp)
//newPeer.ID = resp //newPeer.ID = resp
*/ */
peersList.Peers = append(peersList.Peers, newPeer)
//incomingPeersList.Peers = append(incomingPeersList.Peers, newPeer)
incomingPeersList = appendPeerIfNoExist(incomingPeersList, newPeer)
go handleConn(conn, newPeer) go handleConn(conn, newPeer)
} }
} }
func connectToPeer(peer Peer) { 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) conn, err := net.Dial("tcp", peer.IP+":"+peer.Port)
if err != nil { if err != nil {
log.Println("Error connecting to: " + peer.IP + ":" + peer.Port) log.Println("Error connecting to: " + peer.IP + ":" + peer.Port)
return return
} }
peer.Conn = conn peer.Conn = conn
peersList.Peers = append(peersList.Peers, peer)
//outcomingPeersList.Peers = append(outcomingPeersList.Peers, peer)
outcomingPeersList = appendPeerIfNoExist(outcomingPeersList, peer)
go handleConn(conn, peer) go handleConn(conn, peer)
} }
func handleConn(conn net.Conn, connPeer 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()) log.Println("handling conn: " + conn.RemoteAddr().String())
//reply to the conn, send the peerList //reply to the conn, send the peerList
var msg Msg var msg Msg
msg = msg.construct("PeersList", "here my peersList", peersList)
msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList)
msgB := msg.toBytes() msgB := msg.toBytes()
_, err := conn.Write(msgB) _, err := conn.Write(msgB)
check(err) 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 //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") log.Println("Peer: " + conn.RemoteAddr().String() + " connection closed")
conn.Close() conn.Close()
//TODO delete the peer from the peersList
//TODO delete the peer from the outcomingPeersList
} }

+ 6
- 3
peer/main.go

@ -49,19 +49,22 @@ func main() {
} }
} }
thisPeerID = runningPeer.ID thisPeerID = runningPeer.ID
peersList.PeerID = runningPeer.ID
outcomingPeersList.PeerID = runningPeer.ID
fmt.Println(runningPeer) 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" { if runningPeer.Role == "server" {
go acceptPeers(runningPeer) go acceptPeers(runningPeer)
} }
if runningPeer.Role == "client" { if runningPeer.Role == "client" {
var newPeer Peer var newPeer Peer
newPeer.ID = "0"
newPeer.IP = config.ServerIP newPeer.IP = config.ServerIP
newPeer.Port = config.ServerPort newPeer.Port = config.ServerPort
newPeer.Role = "server" newPeer.Role = "server"
connectToPeer(newPeer) connectToPeer(newPeer)
go acceptPeers(runningPeer)
} }
for running { for running {

+ 2
- 2
peer/messages.go

@ -31,7 +31,7 @@ func messageHandler(peer Peer, msg Msg) {
fmt.Println(msg.PeersList) fmt.Println(msg.PeersList)
//time.Sleep(1000 * time.Millisecond) //time.Sleep(1000 * time.Millisecond)
updatePeersList(peer.Conn, msg.PeersList)
updateNetworkPeersList(peer.Conn, msg.PeersList)
propagatePeersList(peer) propagatePeersList(peer)
printPeersList() printPeersList()
break break
@ -40,7 +40,7 @@ func messageHandler(peer Peer, msg Msg) {
fmt.Println(msg.PeersList) fmt.Println(msg.PeersList)
//time.Sleep(1000 * time.Millisecond) //time.Sleep(1000 * time.Millisecond)
updatePeersList(peer.Conn, msg.PeersList)
updateNetworkPeersList(peer.Conn, msg.PeersList)
printPeersList() printPeersList()
break break
case "MyID": case "MyID":

+ 48
- 15
peer/peersList.go

@ -14,8 +14,17 @@ type PeersList struct {
Date time.Time `json:"date"` 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 { func peerIsInPeersList(p Peer, pl []Peer) int {
r := -1 r := -1
@ -26,23 +35,30 @@ func peerIsInPeersList(p Peer, pl []Peer) int {
} }
return r 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 { for _, peer := range newPeersList.Peers {
if getIPPortFromConn(conn) == peer.IP+":"+peer.Port { if getIPPortFromConn(conn) == peer.IP+":"+peer.Port {
peer.ID = newPeersList.PeerID peer.ID = newPeersList.PeerID
color.Yellow(peer.ID) color.Yellow(peer.ID)
} }
i := peerIsInPeersList(peer, peersList.Peers)
i := peerIsInPeersList(peer, networkPeersList.Peers)
if i == -1 { if i == -1 {
peersList.Peers = append(peersList.Peers, peer)
networkPeersList.Peers = append(networkPeersList.Peers, peer)
} else { } 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) { func searchPeerAndUpdate(p Peer) {
for _, peer := range peersList.Peers {
for _, peer := range outcomingPeersList.Peers {
color.Red(p.IP + ":" + p.Port) color.Red(p.IP + ":" + p.Port)
color.Yellow(peer.IP + ":" + peer.Port) color.Yellow(peer.IP + ":" + peer.Port)
if p.IP+":"+p.Port == 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) { func propagatePeersList(p Peer) {
for _, peer := range peersList.Peers {
for _, peer := range networkPeersList.Peers {
if peer.Conn != nil { if peer.Conn != nil {
if peer.ID != p.ID && p.ID != "" { if peer.ID != p.ID && p.ID != "" {
color.Yellow(peer.ID + " - " + p.ID) color.Yellow(peer.ID + " - " + p.ID)
var msg Msg var msg Msg
msg = msg.construct("PeersList", "here my peersList", peersList)
msg = msg.construct("PeersList", "here my outcomingPeersList", outcomingPeersList)
msgB := msg.toBytes() msgB := msg.toBytes()
_, err := peer.Conn.Write(msgB) _, err := peer.Conn.Write(msgB)
check(err) check(err)
@ -73,19 +89,36 @@ func propagatePeersList(p Peer) {
check(err) check(err)
*/ */
var msg Msg var msg Msg
msg = msg.construct("PeersList_Response", "here my peersList", peersList)
msg = msg.construct("PeersList_Response", "here my outcomingPeersList", outcomingPeersList)
msgB := msg.toBytes() msgB := msg.toBytes()
_, err := peer.Conn.Write(msgB) _, err := peer.Conn.Write(msgB)
check(err) 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() { func printPeersList() {
fmt.Println("") 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(peer)
} }
fmt.Println("") fmt.Println("")

Loading…
Cancel
Save