Added Verbose mode and be more fixed some newline inconsistencies

This commit is contained in:
Henry
2014-02-11 14:08:52 +01:00
parent 3575c9897f
commit b0e9ab001e
7 changed files with 45 additions and 35 deletions

View File

@ -3,14 +3,13 @@ package goSam
import (
"bufio"
"fmt"
"log"
"net"
)
type Client struct {
samConn net.Conn
fromSam *bufio.Reader
toSam *bufio.Writer
SamConn net.Conn
verbose bool
}
// create a new client, connecting to the default host:port at localhost:7656
@ -24,20 +23,19 @@ func NewClient(addr string) (*Client, error) {
if err != nil {
return nil, err
}
c := &Client{
samConn: conn,
fromSam: bufio.NewReader(conn),
toSam: bufio.NewWriter(conn),
}
c := &Client{conn, false}
return c, c.hello()
}
func (c *Client) ToggleVerbose() {
c.verbose = !c.verbose
}
// send the initial handshake command and check that the reply is ok
func (c *Client) hello() (err error) {
const hello = "HELLO VERSION MIN=3.0 MAX=3.0\n"
var r *Reply
r, err = c.sendCmd(hello)
r, err = c.sendCmd("HELLO VERSION MIN=3.0 MAX=3.0")
if err != nil {
return err
}
@ -46,7 +44,7 @@ func (c *Client) hello() (err error) {
return fmt.Errorf("Unknown Reply: %+v\n", r)
}
if r.Pairs["RESULT"] != "OK" || r.Pairs["VERSION"] != "3.0\n" {
if r.Pairs["RESULT"] != "OK" || r.Pairs["VERSION"] != "3.0" {
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
}
@ -55,23 +53,28 @@ func (c *Client) hello() (err error) {
// helper to send one command and parse the reply by sam
func (c *Client) sendCmd(cmd string) (r *Reply, err error) {
if _, err = c.toSam.WriteString(cmd); err != nil {
if _, err = fmt.Fprintln(c.SamConn, cmd); err != nil {
return
}
if err = c.toSam.Flush(); err != nil {
return
if c.verbose {
log.Printf(">Send>'%s'\n", cmd)
}
line, err := c.fromSam.ReadString('\n')
reader := bufio.NewReader(c.SamConn)
line, err := reader.ReadString('\n')
if err != nil {
return
}
if c.verbose {
log.Printf("<Rcvd<'%s'\n", line)
}
r, err = parseReply(line)
return
}
func (c *Client) Close() error {
return c.samConn.Close()
return c.SamConn.Close()
}

14
dial.go
View File

@ -1,7 +1,6 @@
package goSam
import (
"fmt"
"net"
"strings"
)
@ -17,8 +16,6 @@ func (c *Client) Dial(network, addr string) (net.Conn, error) {
return nil, err
}
fmt.Println("Dial Lookup:", addr)
id, _, err := c.createStreamSession("")
if err != nil {
return nil, err
@ -29,13 +26,14 @@ func (c *Client) Dial(network, addr string) (net.Conn, error) {
return nil, err
}
fmt.Println("newC Hello OK")
if c.verbose {
newC.ToggleVerbose()
}
if newC.StreamConnect(id, addr) != nil {
err = newC.StreamConnect(id, addr)
if err != nil {
return nil, err
}
fmt.Println("StreamConnect OK")
return newC.samConn, nil
return newC.SamConn, nil
}

View File

@ -28,7 +28,7 @@ func (r ReplyError) Error() string {
func (c *Client) Lookup(name string) (addr string, err error) {
var r *Reply
r, err = c.sendCmd(fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name))
r, err = c.sendCmd(fmt.Sprintf("NAMING LOOKUP NAME=%s", name))
if err != nil {
return
}

View File

@ -13,6 +13,7 @@ type Reply struct {
}
func parseReply(line string) (r *Reply, err error) {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
if len(parts) < 3 {
return nil, fmt.Errorf("Malformed Reply.\n%s\n", line)

View File

@ -10,7 +10,7 @@ var validCases = []struct {
}{
// hello handshake reply
{
"HELLO REPLY RESULT=OK VERSION=3.0",
"HELLO REPLY RESULT=OK VERSION=3.0\n",
Reply{
Topic: "HELLO",
Type: "REPLY",
@ -22,7 +22,7 @@ var validCases = []struct {
},
// result of a naming lookup
{
"NAMING REPLY RESULT=OK NAME=zzz.i2p VALUE=SomeValueForTesting",
"NAMING REPLY RESULT=OK NAME=zzz.i2p VALUE=SomeValueForTesting\n",
Reply{
Topic: "NAMING",
Type: "REPLY",
@ -35,7 +35,7 @@ var validCases = []struct {
},
// session status reply
{
"SESSION STATUS RESULT=I2P_ERROR MESSAGE=TheMessageFromI2p",
"SESSION STATUS RESULT=I2P_ERROR MESSAGE=TheMessageFromI2p\n",
Reply{
Topic: "SESSION",
Type: "STATUS",
@ -45,6 +45,16 @@ var validCases = []struct {
},
},
},
{
"STREAM STATUS RESULT=CANT_REACH_PEER\n",
Reply{
Topic: "STREAM",
Type: "STATUS",
Pairs: map[string]string{
"RESULT": "CANT_REACH_PEER",
},
},
},
}
func TestParseReplyValidCases(t *testing.T) {
@ -64,7 +74,7 @@ func TestParseReplyValidCases(t *testing.T) {
for expK, expV := range tcase.Expected.Pairs {
if expV != parsed.Pairs[expK] {
t.Fatalf("Wrong %s. Got %s expected %s", expK, parsed.Pairs[expK], expV)
t.Fatalf("Wrong %s.\nGot '%s'\nExpected '%s'", expK, parsed.Pairs[expK], expV)
}
}
}

View File

@ -14,7 +14,7 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
var r *Reply
id = rand.Int31n(math.MaxInt32)
createCmd := fmt.Sprintf("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s\n", id, dest)
createCmd := fmt.Sprintf("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s", id, dest)
r, err = c.sendCmd(createCmd)
if err != nil {
return
@ -31,7 +31,6 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
return
}
fmt.Println("createStreamSession created")
newDest = r.Pairs["DESTINATION"]
return

View File

@ -7,7 +7,7 @@ import (
func (c *Client) StreamConnect(id int32, dest string) (err error) {
var r *Reply
r, err = c.sendCmd(fmt.Sprintf("STREAM CONNECT ID=%d DESTINATION=%s\n", id, dest))
r, err = c.sendCmd(fmt.Sprintf("STREAM CONNECT ID=%d DESTINATION=%s", id, dest))
if err != nil {
return err
}
@ -18,8 +18,7 @@ func (c *Client) StreamConnect(id int32, dest string) (err error) {
result := r.Pairs["RESULT"]
if result != "OK" {
err = ReplyError{result, r}
return
return ReplyError{result, r}
}
fmt.Println("StreamConnect OK")