forked from cryptix/goSam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
38 lines (31 loc) · 773 Bytes
/
sessions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package goSam
import (
"fmt"
"math"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// CreateStreamSession creates a new STREAM Session.
// Returns the Id for the new Client.
func (c *Client) CreateStreamSession(dest string) (int32, string, error) {
if dest == "" {
dest = "TRANSIENT"
}
id := rand.Int31n(math.MaxInt32)
r, err := c.sendCmd("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s\n", id, dest)
if err != nil {
return -1, "", err
}
// TODO: move check into sendCmd()
if r.Topic != "SESSION" || r.Type != "STATUS" {
return -1, "", fmt.Errorf("Unknown Reply: %+v\n", r)
}
result := r.Pairs["RESULT"]
if result != "OK" {
return -1, "", ReplyError{ResultKeyNotFound, r}
}
return id, r.Pairs["DESTINATION"], nil
}