-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscription.go
82 lines (68 loc) · 2.12 KB
/
subscription.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
package gocast
import (
"crypto/sha256"
"fmt"
"sync/atomic"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/stampzilla/gocast/api"
"github.com/stampzilla/gocast/responses"
)
type Subscription struct {
Urn string
SourceId string
DestinationId string
Handler Handler
Device *Device
requestId int64
inFlight map[int]chan *api.CastMessage
}
func (s *Subscription) Sha256() string {
data := s.Urn + s.SourceId + s.DestinationId
sum := sha256.Sum256([]byte(data))
return string(sum[:])
}
func (s *Subscription) Send(payload responses.Payload) error {
requestId := int(atomic.AddInt64(&s.requestId, 1))
payload.SetRequestId(requestId)
return s.Device.Send(s.Urn, s.SourceId, s.DestinationId, payload)
}
// Request works like send, but waits for resposne to requestId before returning.
func (s *Subscription) Request(payload responses.Payload) (*api.CastMessage, error) {
requestId := int(atomic.AddInt64(&s.requestId, 1))
payload.SetRequestId(requestId)
response := make(chan *api.CastMessage)
s.inFlight[requestId] = response
// err := s.Send(payload)
err := s.Device.Send(s.Urn, s.SourceId, s.DestinationId, payload)
if err != nil {
delete(s.inFlight, requestId)
return nil, err
}
delay := time.NewTimer(time.Second * 10)
select {
case reply := <-response:
if !delay.Stop() {
<-delay.C
}
return reply, nil
case <-delay.C:
delete(s.inFlight, requestId)
return nil, fmt.Errorf("Timeout sending: %s", spew.Sdump(payload))
}
}
func (s *Subscription) Receive(message *api.CastMessage, headers *responses.Headers) bool {
// Just skip the message if it isnt to this subscription
if *message.SourceId != s.DestinationId || (*message.DestinationId != s.SourceId && *message.DestinationId != "*") || *message.Namespace != s.Urn {
return false
}
s.Handler.Unmarshal(message.GetPayloadUtf8())
// if this is a request we must send the response back to the pending request
if headers.RequestId != nil && *headers.RequestId != 0 {
if listener, ok := s.inFlight[*headers.RequestId]; ok {
listener <- message
delete(s.inFlight, *headers.RequestId)
}
}
return true
}