2014-02-10 21:01:55 +01:00
|
|
|
package goSam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
2015-02-05 11:52:28 +01:00
|
|
|
"time"
|
2014-02-10 21:01:55 +01:00
|
|
|
)
|
|
|
|
|
2015-02-05 11:52:28 +01:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2014-02-14 12:05:13 +01:00
|
|
|
// CreateStreamSession creates a new STREAM Session.
|
|
|
|
// Returns the Id for the new Client.
|
2014-02-11 14:48:13 +01:00
|
|
|
func (c *Client) CreateStreamSession(dest string) (id int32, newDest string, err error) {
|
2014-02-10 21:01:55 +01:00
|
|
|
if dest == "" {
|
|
|
|
dest = "TRANSIENT"
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:11:26 +01:00
|
|
|
var r *Reply
|
|
|
|
|
2014-02-10 21:01:55 +01:00
|
|
|
id = rand.Int31n(math.MaxInt32)
|
2014-02-11 14:08:52 +01:00
|
|
|
createCmd := fmt.Sprintf("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s", id, dest)
|
2014-02-11 13:11:26 +01:00
|
|
|
r, err = c.sendCmd(createCmd)
|
2014-02-10 21:01:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Topic != "SESSION" || r.Type != "STATUS" {
|
|
|
|
err = fmt.Errorf("Unknown Reply: %+v\n", r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:11:26 +01:00
|
|
|
result := r.Pairs["RESULT"]
|
|
|
|
if result != "OK" {
|
2014-02-10 21:01:55 +01:00
|
|
|
err = ReplyError{ResultKeyNotFound, r}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:11:26 +01:00
|
|
|
newDest = r.Pairs["DESTINATION"]
|
|
|
|
|
2014-02-10 21:01:55 +01:00
|
|
|
return
|
|
|
|
}
|