Skip to content

Commit

Permalink
Fixed two node error where second node doesn't get chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-X2 committed Nov 5, 2018
1 parent 80cee62 commit f3bbd47
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions p2p/TracebookMessenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ type Node struct {
ChatHistory []string
KnownNodes []string
NodeAddress string
Name string
Name string
}

var State = Node{}

func main() {
var exit = make(chan bool)

var broadcastPort string
var thisAddress string
var initConnAddress string
Expand All @@ -35,7 +33,6 @@ func main() {
broadcastPort = ":" + broadcastPort
//broadcastPort := ":8080"


fmt.Print("Enter address of peer: ")
fmt.Scanln(&initConnAddress)
fmt.Print("What is your name? : ")
Expand All @@ -45,7 +42,7 @@ func main() {
thisAddress = localAddr.IP.String() + broadcastPort
State = Node{ChatHistory: []string{}, KnownNodes: []string{}, NodeAddress: thisAddress, Name: name}
State.KnownNodes = append(State.KnownNodes, thisAddress)

fmt.Println("Node hosted at : " + thisAddress)
fmt.Println("--------------------------------------------")
fmt.Println("| Welcome to TracebookMessenger! |")
Expand All @@ -58,9 +55,6 @@ func main() {
go listen(broadcastPort)

sendMessage()
// Blocking operation
// Allows the go routines to excecute indefinitely
<-exit
}

// Connects to a node to get updated
Expand Down Expand Up @@ -113,7 +107,7 @@ func decode(conn net.Conn) {

// Add node if previously unknown
if !addressIsKnown(decodedStruct.NodeAddress) {
fmt.Println("New node connected!")
fmt.Println("New node connected: " + decodedStruct.Name)
State.KnownNodes = append(State.KnownNodes, decodedStruct.NodeAddress)
}
//fmt.Println("Gob recieved from: " + decodedStruct.NodeAddress)
Expand All @@ -124,7 +118,6 @@ func decode(conn net.Conn) {
//fmt.Println("My KnownNodes have been updated!")
}
if len(decodedStruct.ChatHistory) > len(State.ChatHistory) {
//fmt.Println("My ChatHistory has been updated!")

previousLength := len(State.ChatHistory)
// Update state
Expand All @@ -141,7 +134,7 @@ func decode(conn net.Conn) {
}

// Send update to new node, and update every other known node
if len(decodedStruct.KnownNodes) < len(State.KnownNodes) {
if len(decodedStruct.KnownNodes) < len(State.KnownNodes) || len(decodedStruct.ChatHistory) < len(State.ChatHistory) {
updateNetwork()
}

Expand Down

0 comments on commit f3bbd47

Please sign in to comment.