From 97bb4e1dc51e2049fc68a7a618e512a134f77a4c Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 17 Oct 2017 12:03:03 -0600 Subject: [PATCH] Seems to start to work --- test/client.go | 16 +++++++--------- test/main.go | 2 +- test/rendevouz.go | 2 +- test/server.go | 32 ++++++++++++++++++++++++-------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/test/client.go b/test/client.go index e0325bb..1b1bc9d 100644 --- a/test/client.go +++ b/test/client.go @@ -4,11 +4,10 @@ import ( "fmt" "net" "strconv" - "strings" "sync" ) -func runClient() { +func runClient(connectionType string, codePhrase string) { var wg sync.WaitGroup wg.Add(numberConnections) for id := 0; id < numberConnections; id++ { @@ -21,14 +20,13 @@ func runClient() { } defer connection.Close() - var messageByte []byte - var message string - messageByte = make([]byte, 64) - connection.Read(messageByte) - message = strings.Replace(string(messageByte), ":", "", -1) + message := receiveMessage(connection) fmt.Println(message) - message = fillString("r.1-2-3", 64) - connection.Write([]byte(message)) + sendMessage(connectionType+"."+codePhrase, connection) + if connectionType == "s" { + message = receiveMessage(connection) + fmt.Println(message) + } }(id) } diff --git a/test/main.go b/test/main.go index c4cc914..26108a5 100644 --- a/test/main.go +++ b/test/main.go @@ -32,7 +32,7 @@ func main() { if len(fileName) != 0 { runServer() } else if len(serverAddress) != 0 { - runClient() + runClient(connectionTypeFlag, codePhraseFlag) } else { fmt.Println("You must specify either -file (for running as a server) or -server (for running as a client)") } diff --git a/test/rendevouz.go b/test/rendevouz.go index 0ba0621..76c04e1 100644 --- a/test/rendevouz.go +++ b/test/rendevouz.go @@ -53,7 +53,7 @@ func chanFromConn(conn net.Conn) chan []byte { c := make(chan []byte) go func() { - b := make([]byte, 1024) + b := make([]byte, BUFFERSIZE) for { n, err := conn.Read(b) diff --git a/test/server.go b/test/server.go index 7a70c6b..58aa304 100644 --- a/test/server.go +++ b/test/server.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" "sync" + "time" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -79,26 +80,41 @@ func clientCommuncation(id int, connection net.Conn) { connectionType := strings.Split(message, ".")[0] codePhrase := strings.Split(message, ".")[1] // If reciever - connections.Lock() - connections.reciever[codePhrase] = connection - connections.Unlock() if connectionType == "s" { - // periodically check if the receiver has joined - + fmt.Println("Got sender") + connections.Lock() + connections.sender[codePhrase] = connection + connections.Unlock() + for { + fmt.Println("waiting for reciever") + connections.RLock() + if _, ok := connections.reciever[codePhrase]; ok { + break + } + connections.RUnlock() + time.Sleep(100 * time.Millisecond) + } + sendMessage("ok", connection) + } else { + fmt.Println("Got reciever") + connections.Lock() + connections.reciever[codePhrase] = connection + connections.Unlock() } fmt.Println(message) return } func sendMessage(message string, connection net.Conn) { - message = fillString(message, 64) + message = fillString(message, BUFFERSIZE) connection.Write([]byte(message)) } func receiveMessage(connection net.Conn) string { - messageByte := make([]byte, 64) - connection.Read(messageByte) + messageByte := make([]byte, BUFFERSIZE) + n, err := connection.Read(messageByte) + fmt.Println(n, err) return strings.Replace(string(messageByte), ":", "", -1) }