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 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 { 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) { func (c *Client) Lookup(name string) (addr string, err error) {
msg := fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name) var r *Reply
if _, err = c.toSam.WriteString(msg); err != nil {
return
}
if err = c.toSam.Flush(); err != nil { r, err = c.sendCmd(fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name))
return
}
var (
line string
r *Reply
)
line, err = c.fromSam.ReadString('\n')
if err != nil {
return
}
r, err = parseReply(line)
if err != nil { if err != nil {
return return
} }
@ -55,18 +38,17 @@ func (c *Client) Lookup(name string) (addr string, err error) {
return return
} }
switch r.Pairs["RESULT"] { result := r.Pairs["RESULT"]
case "OK": if result != "OK" {
addr = r.Pairs["VALUE"] err = ReplyError{result, r}
return
case "KEY_NOT_FOUND":
err = ReplyError{ResultKeyNotFound, r}
return return
} }
if r.Pairs["NAME"] != name { 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 return
} }

View File

@ -11,29 +11,11 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
dest = "TRANSIENT" dest = "TRANSIENT"
} }
var r *Reply
id = rand.Int31n(math.MaxInt32) 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\n", id, dest)
_, err = c.toSam.WriteString(createCmd) r, err = c.sendCmd(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)
if err != nil { if err != nil {
return return
} }
@ -43,24 +25,14 @@ func (c *Client) createStreamSession(dest string) (id int32, newDest string, err
return return
} }
switch r.Pairs["RESULT"] { result := r.Pairs["RESULT"]
case ResultOk: if result != "OK" {
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:
err = ReplyError{ResultKeyNotFound, r} err = ReplyError{ResultKeyNotFound, r}
return return
} }
fmt.Println("createStreamSession created")
newDest = r.Pairs["DESTINATION"]
return return
} }

View File

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