creating messages library, will be encoding/decoding via encoding/JSON for now
This commit is contained in:
@@ -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")
|
||||
|
@@ -9,26 +9,26 @@ import (
|
||||
|
||||
// GetServerInfo queries the supplied server name for information about the server
|
||||
func GetServerInfo(conn *net.Conn) error {
|
||||
for { // Server should send 'Connected\n' to client after connect, then client can ask for server details
|
||||
for { // sending request for details to server
|
||||
bytesSent, err := fmt.Fprintf(*conn, "Details\n")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to server! ", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if bytesSent > 0 {
|
||||
fmt.Println("Message Sent")
|
||||
}
|
||||
message, err := bufio.NewReader(*conn).ReadString('\n')
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading from connection: %s", err)
|
||||
}
|
||||
fmt.Printf("Message from server: %s", message)
|
||||
switch message {
|
||||
case "Connected\n":
|
||||
fmt.Println("Server has acknowledged connection, asking for server details")
|
||||
bytesSent, err := fmt.Fprintf(*conn, "Details\n")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to server! ", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if bytesSent > 0 {
|
||||
fmt.Println("Message Sent")
|
||||
}
|
||||
case "Details\n":
|
||||
fmt.Println("Server details: ", message)
|
||||
return nil
|
||||
default:
|
||||
fmt.Println("Details: ", message)
|
||||
os.Exit(0)
|
||||
return fmt.Errorf("unrecognized message: %s", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -40,4 +40,5 @@ func RefreshContent(conf *clientconfig.Gvcconfig) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user