creating messages library, will be encoding/decoding via encoding/JSON for now
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
version = "0.1.5"
|
||||
rootPath = ""
|
||||
currentBranch = "master"
|
||||
currentbranch = "master"
|
||||
|
||||
[[remote]]
|
||||
name = "test2"
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -4,6 +4,8 @@ github.com/deranjer/clir v1.0.5 h1:tEunZj5qJLYNBtzMQ/vH8hEAIv4NptWFmTldsP9U2qY=
|
||||
github.com/deranjer/clir v1.0.5/go.mod h1:x/FAjr5CHGsBT0yjs+NYxX3qFxx8G15gbeCcN6FFuyU=
|
||||
github.com/deranjer/store v0.0.0-20200526205429-464dd59c6031 h1:sPjxPMNILoBbu6uhDfa97AhlUhTgtPY2HqySAzuLd4o=
|
||||
github.com/deranjer/store v0.0.0-20200526205429-464dd59c6031/go.mod h1:wPOs9IJ77lRTXyjEOQeegCFjIlm21qOFcv33lXmU7gE=
|
||||
github.com/mailru/easyjson v0.7.1 h1:mdxE1MF9o53iCb2Ghj1VfWvh7ZOwHpnVG/xwXrV90U8=
|
||||
github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
|
7
messages/go.mod
Normal file
7
messages/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module github.com/deranjer/gvc/messages
|
||||
|
||||
go 1.14
|
||||
|
||||
replace derajnet.duckdns.org/git/deranjer/gvc => ../gvc //alias for local development
|
||||
|
||||
replace github.com/deranjer/gvc => ../gvc
|
28
messages/structure.go
Normal file
28
messages/structure.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package messages
|
||||
|
||||
// ID forces the command IDs to always be a certain type.
|
||||
type ID int
|
||||
|
||||
// ID forces all these commands to be locked in
|
||||
const (
|
||||
COMMIT ID = iota
|
||||
REFRESH
|
||||
INFO
|
||||
PUSH
|
||||
PULL
|
||||
REVERT
|
||||
LOCK
|
||||
)
|
||||
|
||||
// Modifiers contains any arguments/modifiers to the command ID
|
||||
type Modifiers struct {
|
||||
ModifierID string
|
||||
ModifierData string
|
||||
}
|
||||
|
||||
// Command is the structure that all messages must adhere to
|
||||
type Command struct {
|
||||
CmdID ID //locked into the ID type
|
||||
Mods []Modifiers
|
||||
Body []byte
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/deranjer/gvc/messages"
|
||||
"github.com/firstrow/tcp_server"
|
||||
)
|
||||
|
||||
@@ -18,8 +20,9 @@ func main() {
|
||||
c.Send("Connected\n")
|
||||
})
|
||||
server.OnNewMessage(func(c *tcp_server.Client, message string) {
|
||||
fmt.Println("new Message from client: ", message)
|
||||
switch message {
|
||||
newMessage := json.Unmarshal(message, messages.Command)
|
||||
fmt.Println("new Message from client: ", newMessage.CmdID)
|
||||
switch newMessage.CmdID {
|
||||
case "Details\n":
|
||||
fmt.Println("Sending server details to client")
|
||||
detailsMessage := fmt.Sprintf("Server Details are as follows: Version: %s \n", version)
|
||||
@@ -34,4 +37,4 @@ func main() {
|
||||
})
|
||||
|
||||
server.Listen()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user