diff --git a/auth.go b/auth.go index 8728dc9..d9688ff 100644 --- a/auth.go +++ b/auth.go @@ -1,47 +1,33 @@ package gosam -import "fmt" - // SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and // Password from the arguments func (c *Client) SetupAuth(user, password string) error { - r, err := c.sendCmd("AUTH ENABLE\n") + _, err := c.sendCmd("AUTH ENABLE\n") if err != nil { return err } - if r.Topic != "AUTH" { - return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r) - } - r, err = c.sendCmd("AUTH %s %s\n", user, password) + _, err = c.sendCmd("AUTH %s %s\n", user, password) if err != nil { return err } - if r.Topic != "AUTH" { - return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r) - } return nil } // TeardownAuth sends the AUTH DISABLE command but does not remove the Username and // Password from the client PasswordManager func (c *Client) TeardownAuth() error { - r, err := c.sendCmd("AUTH DISABLE\n") + _, err := c.sendCmd("AUTH DISABLE\n") if err != nil { return err } - if r.Topic != "AUTH" { - return fmt.Errorf("TeardownAuth Unknown Reply: %+v\n", r) - } return nil } func (c *Client) RemoveAuthUser(user string) error { - r, err := c.sendCmd("AUTH REMOVE %s\n", user) + _, err := c.sendCmd("AUTH REMOVE %s\n", user) if err != nil { return err } - if r.Topic != "AUTH" { - return fmt.Errorf("RemoveAuthUser Unknown Reply: %+v\n", r) - } return nil } diff --git a/client.go b/client.go index c32b130..d14f556 100644 --- a/client.go +++ b/client.go @@ -226,11 +226,7 @@ func (c *Client) hello() error { return err } - if r.Topic != "HELLO" { - return fmt.Errorf("Client Hello Unknown Reply: %+v\n", r) - } - - if r.Pairs["RESULT"] != "OK" { + if !r.IsOk() { return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r) } @@ -248,7 +244,37 @@ func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) { return nil, err } - return parseReply(line) + r, err := parseReply(line) + if err != nil { + return nil, err + } + + if err := c.validateResponse(str, r); err != nil { + return nil, fmt.Errorf("unrecogized reply: %+v\n%v", r, err) + } + + return r, nil +} + +func (c *Client) validateResponse(command string, reply *Reply) error { + expectedTypesMap := map[string]string{ + "HELLO": "REPLY", + "DEST": "REPLY", + "NAMING": "REPLY", + "SESSION": "STATUS", + "STREAM": "STATUS", + } + commandTopic := strings.SplitN(command, "", 1)[0] + + if commandTopic != reply.Topic { + return fmt.Errorf("unrecogized reply topic. expecting: %v, got: %v", commandTopic, reply.Topic) + } + + if expectedTypesMap[commandTopic] != reply.Type { + return fmt.Errorf("unrecogized reply type. expecting: %v, got: %v", expectedTypesMap[commandTopic], reply.Type) + } + + return nil } // Close the underlying socket to SAM diff --git a/dest.go b/dest.go index 893dc45..baffac4 100644 --- a/dest.go +++ b/dest.go @@ -46,9 +46,6 @@ func (c *Client) NewDestination(kind ...string) (string, string, error) { if err != nil { return "", "", err } - if r.Topic != "DEST" { - return "", "", fmt.Errorf("NewDestination Unknown Reply: %+v\n", r) - } return r.Pairs["PRIV"], r.Pairs["PUB"], nil } diff --git a/naming.go b/naming.go index aaa7e14..9fd3d89 100644 --- a/naming.go +++ b/naming.go @@ -15,14 +15,8 @@ func (c *Client) Lookup(name string) (string, error) { return "", nil } - // TODO: move check into sendCmd() - if r.Topic != "NAMING" || r.Type != "REPLY" { - return "", fmt.Errorf("Naming Unknown Reply: %s, %s\n", r.Topic, r.Type) - } - - result := r.Pairs["RESULT"] - if result != "OK" { - return "", ReplyError{result, r} + if !r.IsOk() { + return "", ReplyError{r.GetResult(), r} } if r.Pairs["NAME"] != name { diff --git a/replyParser.go b/replyParser.go index 8035105..98a9f4e 100644 --- a/replyParser.go +++ b/replyParser.go @@ -38,6 +38,21 @@ type Reply struct { Pairs map[string]string } +func (r *Reply) IsOk() bool { + return r.Pairs["RESULT"] == ResultOk +} + +func (r *Reply) GetResult() string { + result, ok := r.Pairs["RESULT"] + + if !ok { + // TODO Add some debug output + return "" + } + + return result +} + func parseReply(line string) (*Reply, error) { line = strings.TrimSpace(line) parts := strings.Split(line, " ") diff --git a/sessions.go b/sessions.go index 1628975..907d4c6 100644 --- a/sessions.go +++ b/sessions.go @@ -1,7 +1,7 @@ package gosam import ( - "fmt" + // "math" "math/rand" "time" @@ -33,13 +33,7 @@ func (c *Client) CreateSession(style, dest string) (string, error) { return "", err } - // TODO: move check into sendCmd() - if r.Topic != "SESSION" || r.Type != "STATUS" { - return "", fmt.Errorf("Session Unknown Reply: %+v\n", r) - } - - result := r.Pairs["RESULT"] - if result != "OK" { + if !r.IsOk() { return "", ReplyError{ResultKeyNotFound, r} } c.destination = r.Pairs["DESTINATION"] diff --git a/stream.go b/stream.go index 81231a9..384d3c1 100644 --- a/stream.go +++ b/stream.go @@ -1,9 +1,5 @@ package gosam -import ( - "fmt" -) - // StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client func (c *Client) StreamConnect(dest string) error { if dest == "" { @@ -14,14 +10,8 @@ func (c *Client) StreamConnect(dest string) error { return err } - // TODO: move check into sendCmd() - if r.Topic != "STREAM" || r.Type != "STATUS" { - return fmt.Errorf("Stream Connect Unknown Reply: %+v\n", r) - } - - result := r.Pairs["RESULT"] - if result != "OK" { - return ReplyError{result, r} + if !r.IsOk() { + return ReplyError{r.GetResult(), r} } return nil @@ -34,14 +24,8 @@ func (c *Client) StreamAccept() (*Reply, error) { return nil, err } - // TODO: move check into sendCmd() - if r.Topic != "STREAM" || r.Type != "STATUS" { - return nil, fmt.Errorf("Stream Accept Unknown Reply: %+v\n", r) - } - - result := r.Pairs["RESULT"] - if result != "OK" { - return nil, ReplyError{result, r} + if !r.IsOk() { + return nil, ReplyError{r.GetResult(), r} } return r, nil