2018-09-19 07:07:32 +00:00
|
|
|
package arlo
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-09-18 18:02:21 +00:00
|
|
|
"net/http"
|
2018-09-17 04:44:41 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/r3labs/sse"
|
|
|
|
)
|
|
|
|
|
2018-09-18 18:02:21 +00:00
|
|
|
var (
|
2018-09-19 21:35:05 +00:00
|
|
|
FAILED_TO_PUBLISH = errors.New("failed to publish")
|
|
|
|
FAILED_TO_DECODE_JSON = errors.New("failed to decode json")
|
|
|
|
FAILED_TO_SUBSCRIBE = errors.New("failed to subscribe to seeclient")
|
2018-09-18 18:02:21 +00:00
|
|
|
)
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
type subscriber chan *EventStreamResponse
|
|
|
|
|
|
|
|
type subscribers map[string]subscriber
|
|
|
|
|
|
|
|
type subscriptions struct {
|
|
|
|
subscribers
|
|
|
|
rwmutex sync.RWMutex
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
type eventStream struct {
|
|
|
|
SSEClient *sse.Client
|
|
|
|
Events chan *sse.Event
|
|
|
|
Error chan error
|
|
|
|
Verbose bool
|
|
|
|
Disconnected chan interface{}
|
|
|
|
once *sync.Once
|
|
|
|
|
|
|
|
subscriptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEventStream(url string, client *http.Client) *eventStream {
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
SSEClient := sse.NewClient(url)
|
2018-09-18 18:02:21 +00:00
|
|
|
SSEClient.Connection = client
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
return &eventStream{
|
2018-09-17 04:44:41 +00:00
|
|
|
SSEClient: SSEClient,
|
|
|
|
Events: make(chan *sse.Event),
|
2018-09-22 14:15:22 +00:00
|
|
|
subscriptions: subscriptions{make(map[string]subscriber), sync.RWMutex{}},
|
2018-09-19 21:35:05 +00:00
|
|
|
Error: make(chan error),
|
2018-09-22 14:15:22 +00:00
|
|
|
Disconnected: make(chan interface{}),
|
|
|
|
once: new(sync.Once),
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
func (e *eventStream) disconnect() {
|
|
|
|
e.once.Do(func() {
|
|
|
|
close(e.Disconnected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventStream) listen() (connected chan bool) {
|
2018-09-19 21:35:05 +00:00
|
|
|
|
|
|
|
connected = make(chan bool)
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-09-18 18:02:21 +00:00
|
|
|
err := e.SSEClient.SubscribeChanRaw(e.Events)
|
2018-09-17 04:44:41 +00:00
|
|
|
if err != nil {
|
2018-09-19 21:35:05 +00:00
|
|
|
e.Error <- FAILED_TO_SUBSCRIBE
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-e.Events:
|
|
|
|
/*
|
|
|
|
fmt.Println("Got event message.")
|
|
|
|
fmt.Printf("EVENT: %s\n", event.Event)
|
|
|
|
fmt.Printf("DATA: %s\n", event.Data)
|
|
|
|
*/
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
if event.Data != nil {
|
|
|
|
notifyResponse := &EventStreamResponse{}
|
|
|
|
b := bytes.NewBuffer(event.Data)
|
|
|
|
err := json.NewDecoder(b).Decode(notifyResponse)
|
|
|
|
if err != nil {
|
|
|
|
e.Error <- FAILED_TO_DECODE_JSON
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if notifyResponse.Status == "connected" {
|
|
|
|
connected <- true
|
|
|
|
} else if notifyResponse.Status == "disconnected" {
|
2018-09-22 14:15:22 +00:00
|
|
|
e.disconnect()
|
2018-09-19 21:35:05 +00:00
|
|
|
} else {
|
2018-09-22 14:15:22 +00:00
|
|
|
e.subscriptions.rwmutex.RLock()
|
|
|
|
subscriber, ok := e.subscribers[notifyResponse.TransId]
|
|
|
|
e.subscriptions.rwmutex.RUnlock()
|
|
|
|
if ok {
|
2018-09-19 21:35:05 +00:00
|
|
|
subscriber <- notifyResponse
|
|
|
|
}
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 14:15:22 +00:00
|
|
|
case <-e.Disconnected:
|
2018-09-19 21:35:05 +00:00
|
|
|
connected <- false
|
|
|
|
return
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-09-19 21:35:05 +00:00
|
|
|
|
|
|
|
return connected
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
2018-09-22 14:15:22 +00:00
|
|
|
|
|
|
|
func (s *subscriptions) unsubscribe(transId string) {
|
|
|
|
s.rwmutex.Lock()
|
|
|
|
defer s.rwmutex.Unlock()
|
|
|
|
if _, ok := s.subscribers[transId]; ok {
|
|
|
|
close(s.subscribers[transId])
|
|
|
|
delete(s.subscribers, transId)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriptions) subscribe(transId string, subscriber subscriber) {
|
|
|
|
s.rwmutex.Lock()
|
|
|
|
s.subscribers[transId] = subscriber
|
|
|
|
s.rwmutex.Unlock()
|
|
|
|
}
|