Refactored Send<>Parse reply into .sendCmd()

This commit is contained in:
Henry
2014-02-11 13:11:26 +01:00
parent 9de2fd3f5b
commit 3575c9897f
4 changed files with 42 additions and 95 deletions

View File

@ -51,7 +51,25 @@ func (c *Client) hello() (err error) {
}
return nil
}
// 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 {
return
}
if err = c.toSam.Flush(); err != nil {
return
}
line, err := c.fromSam.ReadString('\n')
if err != nil {
return
}
r, err = parseReply(line)
return
}
func (c *Client) Close() error {

View File

@ -26,26 +26,9 @@ func (r ReplyError) Error() string {
}
func (c *Client) Lookup(name string) (addr string, err error) {
msg := fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name)
if _, err = c.toSam.WriteString(msg); err != nil {
return
}
var r *Reply
if err = c.toSam.Flush(); err != nil {
return
}
var (
line string
r *Reply
)
line, err = c.fromSam.ReadString('\n')
if err != nil {
return
}
r, err = parseReply(line)
r, err = c.sendCmd(fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name))
if err != nil {
return
}
@ -55,18 +38,17 @@ func (c *Client) Lookup(name string) (addr string, err error) {
return
}
switch r.Pairs["RESULT"] {
case "OK":
addr = r.Pairs["VALUE"]
return
case "KEY_NOT_FOUND":
err = ReplyError{ResultKeyNotFound, r}
result := r.Pairs["RESULT"]
if result != "OK" {
err = ReplyError{result, r}
return
}
if r.Pairs["NAME"] != name {
err = fmt.Errorf("i2p Replyied with: %+v\n", r)
err = fmt.Errorf("i2p Replyed to another name.\nWanted:%s\nGot: %+v\n", name, r)
return
}
addr = r.Pairs["VALUE"]
return
}

View File

@ -11,29 +11,11 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
dest = "TRANSIENT"
}
var r *Reply
id = rand.Int31n(math.MaxInt32)
createCmd := fmt.Sprintf("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s\n", id, dest)
_, err = c.toSam.WriteString(createCmd)
if err != nil {
return
}
if err = c.toSam.Flush(); err != nil {
return
}
var (
line string
r *Reply
)
line, err = c.fromSam.ReadString('\n')
if err != nil {
return
}
fmt.Println("createStreamSession line:", line)
r, err = parseReply(line)
r, err = c.sendCmd(createCmd)
if err != nil {
return
}
@ -43,24 +25,14 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
return
}
switch r.Pairs["RESULT"] {
case ResultOk:
fmt.Println("createStreamSession created")
newDest = r.Pairs["DESTINATION"]
return
case ResultDuplicatedId:
err = ReplyError{ResultDuplicatedId, r}
return
case ResultDuplicatedDest:
err = ReplyError{ResultDuplicatedDest, r}
return
case ResultInvalidKey:
err = ReplyError{ResultInvalidKey, r}
return
case ResultI2PError:
result := r.Pairs["RESULT"]
if result != "OK" {
err = ReplyError{ResultKeyNotFound, r}
return
}
fmt.Println("createStreamSession created")
newDest = r.Pairs["DESTINATION"]
return
}

View File

@ -4,28 +4,10 @@ import (
"fmt"
)
func (c *Client) StreamConnect(id int32, dest string) error {
connectCmd := fmt.Sprintf("STREAM CONNECT ID=%d DESTINATION=%s\n", id, dest)
_, err := c.toSam.WriteString(connectCmd)
if err != nil {
return err
}
func (c *Client) StreamConnect(id int32, dest string) (err error) {
var r *Reply
if err = c.toSam.Flush(); err != nil {
return err
}
var (
line string
r *Reply
)
line, err = c.fromSam.ReadString('\n')
if err != nil {
return err
}
r, err = parseReply(line)
r, err = c.sendCmd(fmt.Sprintf("STREAM CONNECT ID=%d DESTINATION=%s\n", id, dest))
if err != nil {
return err
}
@ -34,19 +16,12 @@ func (c *Client) StreamConnect(id int32, dest string) error {
return fmt.Errorf("Unknown Reply: %+v\n", r)
}
switch r.Pairs["RESULT"] {
case ResultOk:
fmt.Println("StreamConnect OK")
return nil
case ResultDuplicatedId:
return ReplyError{ResultDuplicatedId, r}
case ResultDuplicatedDest:
return ReplyError{ResultDuplicatedDest, r}
case ResultInvalidKey:
return ReplyError{ResultInvalidKey, r}
case ResultI2PError:
return ReplyError{ResultKeyNotFound, r}
result := r.Pairs["RESULT"]
if result != "OK" {
err = ReplyError{result, r}
return
}
fmt.Println("StreamConnect OK")
return nil
}