creating messages library, will be encoding/decoding via encoding/JSON for now

This commit is contained in:
2020-06-01 14:38:17 -04:00
parent 1a76a8f2aa
commit 33b1c78eb2
9 changed files with 80 additions and 19 deletions

View File

@@ -1,6 +1,8 @@
package clientcmd
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
@@ -8,6 +10,7 @@ import (
"strconv"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
"github.com/deranjer/gvc/messages"
)
// ConfigPath is the global path to the config that is injected from the main client package.
@@ -76,8 +79,24 @@ func ConnectToServer(serverName string, branchName string, conf *clientconfig.Gv
if err != nil {
return nil, fmt.Errorf("error connecting to %s", connectionString)
}
fmt.Println("Connection started on: ", connectionString)
return &conn, nil
fmt.Println("Attempting connection on: ", connectionString)
decoder := json.NewDecoder(conn)
newMessage := messages.Cmd{}
for { // Doing our 'handshake'
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return nil, fmt.Errorf("error reading from connection: %s", err)
}
fmt.Printf("Message from server: %s", message)
switch message {
case "Connected\n":
fmt.Println("Server recognized client")
return &conn, nil
default:
return nil, fmt.Errorf("unexpected response from server: ", message)
}
}
}
}
return nil, fmt.Errorf("unable to find server name in config")