# HG changeset patch # User sqwishy # Date 1392676483 28800 # Mon Feb 17 14:34:43 2014 -0800 # Node ID a8361125163ac5deb3fd3a36c9fdf6205eaee729 # Parent 8d4983409d85efaa54c1d82a754bd8df9737d518 implements very basic getting of input and sending them as commands to the server and printing responses diff --git a/gorcon/client.go b/gorcon/client.go --- a/gorcon/client.go +++ b/gorcon/client.go @@ -38,17 +38,21 @@ return } -func (c *Client) Auth(data string) (chan error) { +func (c *Client) Auth(data string) (<-chan error) { _, errch := c.writeCommand(AUTH_ID, SERVERDATA_AUTH, []byte(data)) return errch } -func (c *Client) writeCommand(id int32, cmdtype CmdType, body []byte) (chan *Message, chan error) { +func (c *Client) Exec(data string) (<-chan *Message, <-chan error) { + return c.writeCommand(EXEC_ID, SERVERDATA_EXECCOMMAND, []byte(data)) +} + +func (c *Client) writeCommand(id int32, cmdtype CmdType, body []byte) (<-chan *Message, <-chan error) { msgch := make(chan *Message, 1) // Keep these async errch := make(chan error, 1) if err := writeCommand(c, id, cmdtype, body); err != nil { errch <- err - close(msgch) + //close(msgch) } else { c.queue.Push(&comm{msgch, errch}) } @@ -81,7 +85,7 @@ } else { comm.errch <- fmt.Errorf("Authentication Failed") } - close(comm.msgch) + //close(comm.msgch) } else if resp.Id == AUTH_ID && resp.Type == SERVERDATA_RESPONSE_VALUE { // Packet one of two of response to auth request log.Println("Received one of two of responses to auth request") @@ -90,7 +94,7 @@ log.Println("Received response to exec request") c.queue.Pop() comm.msgch <- &Message{[]*packet{resp}} - close(comm.errch) + //close(comm.errch) } else { log.Println("Received ... fuck if I know ... I'm likely going to crash and burn!") c.queue.Pop() @@ -100,10 +104,18 @@ //////////////////////////////////////////////////////////////////////////////// +// This needs help ... type Message struct { packets []*packet } +func (m *Message) AllText() (s string) { + for _, p := range m.packets { + s += string(p.Body) + } + return +} + //////////////////////////////////////////////////////////////////////////////// type comm struct { diff --git a/main.go b/main.go --- a/main.go +++ b/main.go @@ -1,5 +1,6 @@ package main +import "bufio" import "flag" import "fmt" import "log" @@ -33,6 +34,7 @@ if err != nil { log.Panicln(err) } + defer client.Close() log.Println("Connected to server") @@ -41,17 +43,29 @@ case err := <-errch: if err != nil { log.Println(err) + return } else { - log.Println("success?") + log.Println("success!") } } - //select { - //case msg := <-msgch: - // log.Println(msg) - //case err := <-errch: - // log.Panicln(err) - //} + + scanner := bufio.NewScanner(os.Stdin) + for { + fmt.Printf("> ") + if false == scanner.Scan() { + break + } + inp := scanner.Text() + msgch, errch := client.Exec(inp) + select { + case msg := <-msgch: + log.Println(msg) + log.Print(string(msg.AllText())) + case err := <-errch: + log.Panicln(err) + } + } log.Println("done...") }