2014-02-09 18:40:51 +01:00
|
|
|
package goSam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2014-02-11 14:08:52 +01:00
|
|
|
"log"
|
2014-02-10 19:27:24 +01:00
|
|
|
"net"
|
2014-02-09 18:40:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2014-02-11 14:08:52 +01:00
|
|
|
SamConn net.Conn
|
|
|
|
verbose bool
|
2014-02-09 18:40:51 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 13:10:24 +01:00
|
|
|
// create a new client, connecting to the default host:port at localhost:7656
|
2014-02-10 19:27:24 +01:00
|
|
|
func NewDefaultClient() (*Client, error) {
|
|
|
|
return NewClient("localhost:7656")
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:10:24 +01:00
|
|
|
// create a new client, connecting to a specified port
|
2014-02-10 19:27:24 +01:00
|
|
|
func NewClient(addr string) (*Client, error) {
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-11 14:08:52 +01:00
|
|
|
c := &Client{conn, false}
|
2014-02-11 13:10:24 +01:00
|
|
|
return c, c.hello()
|
2014-02-09 18:40:51 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
func (c *Client) ToggleVerbose() {
|
|
|
|
c.verbose = !c.verbose
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:10:24 +01:00
|
|
|
// send the initial handshake command and check that the reply is ok
|
|
|
|
func (c *Client) hello() (err error) {
|
|
|
|
var r *Reply
|
2014-02-09 18:40:51 +01:00
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
r, err = c.sendCmd("HELLO VERSION MIN=3.0 MAX=3.0")
|
2014-02-11 13:10:24 +01:00
|
|
|
if err != nil {
|
2014-02-09 18:40:51 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:10:24 +01:00
|
|
|
if r.Topic != "HELLO" {
|
|
|
|
return fmt.Errorf("Unknown Reply: %+v\n", r)
|
|
|
|
}
|
2014-02-09 18:40:51 +01:00
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
if r.Pairs["RESULT"] != "OK" || r.Pairs["VERSION"] != "3.0" {
|
2014-02-11 13:10:24 +01:00
|
|
|
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
|
2014-02-09 18:40:51 +01:00
|
|
|
}
|
2014-02-11 13:10:24 +01:00
|
|
|
|
2014-02-09 18:40:51 +01:00
|
|
|
return nil
|
2014-02-11 13:11:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper to send one command and parse the reply by sam
|
|
|
|
func (c *Client) sendCmd(cmd string) (r *Reply, err error) {
|
2014-02-11 14:08:52 +01:00
|
|
|
if _, err = fmt.Fprintln(c.SamConn, cmd); err != nil {
|
2014-02-11 13:11:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
if c.verbose {
|
|
|
|
log.Printf(">Send>'%s'\n", cmd)
|
2014-02-11 13:11:26 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
reader := bufio.NewReader(c.SamConn)
|
|
|
|
line, err := reader.ReadString('\n')
|
2014-02-11 13:11:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-02-11 13:10:24 +01:00
|
|
|
|
2014-02-11 14:08:52 +01:00
|
|
|
if c.verbose {
|
|
|
|
log.Printf("<Rcvd<'%s'\n", line)
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:11:26 +01:00
|
|
|
r, err = parseReply(line)
|
|
|
|
return
|
2014-02-09 18:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() error {
|
2014-02-11 14:08:52 +01:00
|
|
|
return c.SamConn.Close()
|
2014-02-09 18:40:51 +01:00
|
|
|
}
|