-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandsubscription.go
88 lines (71 loc) · 1.89 KB
/
commandsubscription.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2018 DataArt. All rights reserved.
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package devicehive_go
import (
"encoding/json"
"sync"
"github.com/devicehive/devicehive-go/internal/transport"
)
var commandSubsMutex sync.RWMutex
var commandSubscriptions = make(map[*CommandSubscription]string)
type CommandSubscription struct {
CommandsChan chan *Command
ErrorChan chan *Error
done chan struct{}
client *Client
}
// Sends request to unsubscribe from current commands subscription
func (cs *CommandSubscription) Remove() *Error {
commandSubsMutex.RLock()
subsId := commandSubscriptions[cs]
commandSubsMutex.RUnlock()
err := cs.client.unsubscribe("command/unsubscribe", subsId)
commandSubsMutex.Lock()
close(cs.done)
delete(commandSubscriptions, cs)
commandSubsMutex.Unlock()
return err
}
func (cs *CommandSubscription) sendError(err *Error) {
cs.ErrorChan <- err
}
func newCommandSubscription(subsId string, tspSubs *transport.Subscription, client *Client) *CommandSubscription {
subs := &CommandSubscription{
CommandsChan: make(chan *Command),
ErrorChan: make(chan *Error),
done: make(chan struct{}),
client: client,
}
commandSubsMutex.Lock()
commandSubscriptions[subs] = subsId
commandSubsMutex.Unlock()
go func() {
loop:
for {
select {
case rawComm, ok := <-tspSubs.DataChan:
if !ok {
break loop
}
comm := client.NewCommand()
err := json.Unmarshal(rawComm, comm)
if err != nil {
subs.ErrorChan <- &Error{name: InvalidSubscriptionEventData, reason: err.Error()}
continue
}
subs.CommandsChan <- comm
case err, ok := <-tspSubs.ErrChan:
if !ok {
break loop
}
subs.sendError(newError(err))
case <-subs.done:
break loop
}
}
close(subs.CommandsChan)
close(subs.ErrorChan)
}()
return subs
}