2018-09-22 19:22:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jeffrey Walter <jeffreydwalter@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
|
|
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
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-12-11 23:48:51 +00:00
|
|
|
e.SSEClient.OnDisconnect(func(c *sse.Client) {
|
|
|
|
e.disconnect()
|
|
|
|
// fmt.Printf("\n\n\n\nClIENT DISCONNECTED!!!!!\n\n\n\n")
|
|
|
|
})
|
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:
|
2018-12-11 23:48:51 +00:00
|
|
|
//fmt.Println("Got event message.")
|
2018-09-19 21:35:05 +00:00
|
|
|
/*
|
2018-12-11 23:48:51 +00:00
|
|
|
fmt.Print(".")
|
2018-09-19 21:35:05 +00:00
|
|
|
fmt.Printf("EVENT: %s\n", event.Event)
|
2018-12-11 23:48:51 +00:00
|
|
|
fmt.Printf("DATA: %s\n", event.Data)
|
2018-09-19 21:35:05 +00:00
|
|
|
*/
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
if event != nil && event.Data != nil {
|
2018-09-19 21:35:05 +00:00
|
|
|
notifyResponse := &EventStreamResponse{}
|
|
|
|
b := bytes.NewBuffer(event.Data)
|
|
|
|
err := json.NewDecoder(b).Decode(notifyResponse)
|
|
|
|
if err != nil {
|
|
|
|
e.Error <- FAILED_TO_DECODE_JSON
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
// FIXME: This is a shitty way to handle this. It's potentially leaking a chan.
|
2018-09-19 21:35:05 +00:00
|
|
|
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()
|
|
|
|
}
|