Seems to start to work

This commit is contained in:
Zack Scholl 2017-10-17 12:03:03 -06:00
parent ef3cfc26b5
commit 97bb4e1dc5
4 changed files with 33 additions and 19 deletions

View File

@ -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)
}

View File

@ -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)")
}

View File

@ -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)

View File

@ -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)
}