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.
|
2015-03-25 22:03:05 +01:00
|
|
|
func (c *Client) CreateStreamSession(dest string) (int32, string, error) {
|
2014-02-10 21:01:55 +01:00
|
|
|
if dest == "" {
|
|
|
|
dest = "TRANSIENT"
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:03:05 +01:00
|
|
|
id := rand.Int31n(math.MaxInt32)
|
2019-02-24 23:08:01 -05:00
|
|
|
r, err := c.sendCmd(
|
|
|
|
"SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s %s\n",
|
|
|
|
id,
|
|
|
|
dest,
|
|
|
|
c.sigtype(),
|
|
|
|
c.allOptions(),
|
|
|
|
)
|
2014-02-10 21:01:55 +01:00
|
|
|
if err != nil {
|
2015-03-25 22:03:05 +01:00
|
|
|
return -1, "", err
|
2014-02-10 21:01:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 22:03:05 +01:00
|
|
|
// TODO: move check into sendCmd()
|
2014-02-10 21:01:55 +01:00
|
|
|
if r.Topic != "SESSION" || r.Type != "STATUS" {
|
2015-03-25 22:03:05 +01:00
|
|
|
return -1, "", fmt.Errorf("Unknown Reply: %+v\n", r)
|
2014-02-10 21:01:55 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 13:11:26 +01:00
|
|
|
result := r.Pairs["RESULT"]
|
|
|
|
if result != "OK" {
|
2015-03-25 22:03:05 +01:00
|
|
|
return -1, "", ReplyError{ResultKeyNotFound, r}
|
2014-02-10 21:01:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 22:03:05 +01:00
|
|
|
return id, r.Pairs["DESTINATION"], nil
|
2014-02-10 21:01:55 +01:00
|
|
|
}
|