Start cameradar server to communicate with GUI
This commit is contained in:
committed by
Brendan Le Glaunec
parent
3ef48a97cf
commit
4e922a2a48
@@ -0,0 +1,48 @@
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package actor
|
||||
|
||||
// Sub/Unsub event type
|
||||
const (
|
||||
SubscribeEvent = "subscribe"
|
||||
UnsubscribeEvent = "unsubscribe"
|
||||
)
|
||||
|
||||
// Subscription contains a sub/unsub event
|
||||
type Subscription struct {
|
||||
Command string
|
||||
Channel string
|
||||
SubscribersCount uint
|
||||
}
|
||||
|
||||
// Publication contains a publish event
|
||||
type Publication struct {
|
||||
Channel string
|
||||
Data string
|
||||
}
|
||||
|
||||
// PubSub is a generic interface for publishing data to subscribers using channels
|
||||
// It exposes subscriptions events so the controller can create/delete
|
||||
// data sources depending on the channels users subscribe to.
|
||||
// ex: launch a camera stream only when users subscribe to it
|
||||
type PubSub interface {
|
||||
Run()
|
||||
Sub() <-chan *Subscription
|
||||
Pub() chan<- *Publication
|
||||
}
|
||||
|
||||
// ChannelAccessChecker allows to check for accesses on a given channel
|
||||
type ChannelAccessChecker interface {
|
||||
CheckAccess(channel, accessToken string) bool
|
||||
ClearCache(accessToken string)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/EtixLabs/cameradar/server/actor"
|
||||
)
|
||||
|
||||
// Mock mocks a pubsub actor
|
||||
type Mock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Sub mock
|
||||
func (m *Mock) Sub() <-chan *actor.Subscription {
|
||||
args := m.Called()
|
||||
return args.Get(0).(<-chan *actor.Subscription)
|
||||
}
|
||||
|
||||
// Pub mock
|
||||
func (m *Mock) Pub() chan<- *actor.Publication {
|
||||
args := m.Called()
|
||||
return args.Get(0).(chan<- *actor.Publication)
|
||||
}
|
||||
|
||||
// Run mock
|
||||
func (m *Mock) Run() {
|
||||
m.Called()
|
||||
}
|
||||
|
||||
// AccessCheckerMock mocks a channel access checker
|
||||
type AccessCheckerMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// CheckAccess mocks an access check
|
||||
func (m *AccessCheckerMock) CheckAccess(channel, accessToken string) bool {
|
||||
args := m.Called(channel, accessToken)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
// ClearCache mocks a cache clear
|
||||
func (m *AccessCheckerMock) ClearCache(accessToken string) {
|
||||
m.Called(accessToken)
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/EtixLabs/cameradar/server/actor"
|
||||
"github.com/EtixLabs/cameradar/server/adaptor"
|
||||
)
|
||||
|
||||
// events allow to serialize in one go routine all events from the subscribers
|
||||
const (
|
||||
eventSubscribe = "subscribe"
|
||||
eventUnsubscribe = "unsubscribe"
|
||||
eventDisconnect = "disconnect"
|
||||
)
|
||||
|
||||
type pubSubEvent struct {
|
||||
name string
|
||||
channel string
|
||||
client adaptor.WebSocket
|
||||
}
|
||||
|
||||
// WebSocket manage pubsub communication using a websocket adaptor
|
||||
type WebSocket struct {
|
||||
wsf adaptor.WebSocketFactory
|
||||
|
||||
subscriptions map[string][]adaptor.WebSocket
|
||||
sub chan *actor.Subscription
|
||||
pub chan *actor.Publication
|
||||
events chan *pubSubEvent
|
||||
}
|
||||
|
||||
// NewWebSocket creates a PubSub actor that uses a websockets factory
|
||||
func NewWebSocket(
|
||||
wsf adaptor.WebSocketFactory,
|
||||
) *WebSocket {
|
||||
wsPubSub := &WebSocket{
|
||||
wsf: wsf,
|
||||
|
||||
subscriptions: make(map[string][]adaptor.WebSocket),
|
||||
sub: make(chan *actor.Subscription),
|
||||
pub: make(chan *actor.Publication),
|
||||
events: make(chan *pubSubEvent),
|
||||
}
|
||||
go wsPubSub.Run()
|
||||
return wsPubSub
|
||||
}
|
||||
|
||||
// Sub return the chan where websocket event gonna be pushed
|
||||
func (b *WebSocket) Sub() <-chan *actor.Subscription {
|
||||
return b.sub
|
||||
}
|
||||
|
||||
// Pub return the chan where we consider publishement will be asked
|
||||
func (b *WebSocket) Pub() chan<- *actor.Publication {
|
||||
return b.pub
|
||||
}
|
||||
|
||||
// Run start to listen on pubsub events
|
||||
func (b *WebSocket) Run() {
|
||||
for {
|
||||
select {
|
||||
case event := <-b.events:
|
||||
|
||||
client := event.client
|
||||
channel := event.channel
|
||||
|
||||
switch event.name {
|
||||
case eventSubscribe:
|
||||
b.handleSubscribe(client, channel)
|
||||
case eventUnsubscribe:
|
||||
b.handleUnsubscribe(client, channel)
|
||||
case eventDisconnect:
|
||||
b.handleDisconnect(client)
|
||||
}
|
||||
case publication := <-b.pub:
|
||||
|
||||
subscribers := b.subscriptions[publication.Channel]
|
||||
|
||||
// prepend channel name to message, so client knows from which channel
|
||||
// the message comes from
|
||||
message := fmt.Sprintf("%s/%s", publication.Channel, publication.Data)
|
||||
|
||||
// broadcast message to subscribers
|
||||
for _, client := range subscribers {
|
||||
select {
|
||||
case client.Write() <- message:
|
||||
default:
|
||||
// drop frame
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Accept a new incoming connection and create a websocket using the factory
|
||||
func (b *WebSocket) Accept(w http.ResponseWriter, req *http.Request) {
|
||||
client, err := b.wsf.NewIncomingWebSocket(w, req)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot accept incoming connection: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
go b.readClient(client)
|
||||
}
|
||||
|
||||
func (b *WebSocket) readClient(client adaptor.WebSocket) {
|
||||
for {
|
||||
message, ok := <-client.Read()
|
||||
if !ok {
|
||||
// connection channel closed, disconnect client (in the main routine)
|
||||
b.events <- &pubSubEvent{
|
||||
name: eventDisconnect,
|
||||
client: client,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// expect text message
|
||||
command, ok := message.(string)
|
||||
if !ok {
|
||||
fmt.Printf("invalid non-text message: %v\n", message)
|
||||
return
|
||||
}
|
||||
|
||||
// process command
|
||||
// NOTE: if another protocol is needed, extract this behavior
|
||||
if strings.HasPrefix(command, "s/") {
|
||||
channel := strings.TrimPrefix(command, "s/")
|
||||
|
||||
// process in main routine
|
||||
b.events <- &pubSubEvent{
|
||||
name: eventSubscribe,
|
||||
client: client,
|
||||
channel: channel,
|
||||
}
|
||||
} else if strings.HasPrefix(command, "u/") {
|
||||
channel := strings.TrimPrefix(command, "u/")
|
||||
|
||||
// process in main routine
|
||||
b.events <- &pubSubEvent{
|
||||
name: eventUnsubscribe,
|
||||
client: client,
|
||||
channel: channel,
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("invalid message '%s', should be [s|u]/{channel}\n", command)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *WebSocket) handleSubscribe(client adaptor.WebSocket, channel string) {
|
||||
// if client is already subscribed, ignore
|
||||
if b.alreadySubscribed(client, channel) {
|
||||
return
|
||||
}
|
||||
|
||||
// add to subscribers map
|
||||
subscribersCount := b.addSubscription(client, channel)
|
||||
|
||||
// notify external world
|
||||
b.sub <- &actor.Subscription{
|
||||
Command: actor.SubscribeEvent,
|
||||
Channel: channel,
|
||||
SubscribersCount: subscribersCount,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *WebSocket) handleUnsubscribe(client adaptor.WebSocket, channel string) {
|
||||
// if client didn't subscribe, ignore
|
||||
if !b.alreadySubscribed(client, channel) {
|
||||
return
|
||||
}
|
||||
|
||||
// remove from map
|
||||
subscribersCount := b.removeSubscription(client, channel)
|
||||
|
||||
// notify external world
|
||||
b.sub <- &actor.Subscription{
|
||||
Command: actor.UnsubscribeEvent,
|
||||
Channel: channel,
|
||||
SubscribersCount: subscribersCount,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *WebSocket) handleDisconnect(client adaptor.WebSocket) {
|
||||
|
||||
// unsubscribe client from all its channels
|
||||
for channel := range b.subscriptions {
|
||||
b.handleUnsubscribe(client, channel)
|
||||
}
|
||||
|
||||
// close client write channel
|
||||
close(client.Write())
|
||||
}
|
||||
|
||||
func (b *WebSocket) alreadySubscribed(client adaptor.WebSocket, channel string) bool {
|
||||
clients := b.subscriptions[channel]
|
||||
for _, c := range clients {
|
||||
if c == client {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// addSubscription adds a subscription of a client to a channel and returns
|
||||
// the new number of subscriptions on the given channel
|
||||
func (b *WebSocket) addSubscription(client adaptor.WebSocket, channel string) uint {
|
||||
b.subscriptions[channel] = append(b.subscriptions[channel], client)
|
||||
return uint(len(b.subscriptions[channel]))
|
||||
}
|
||||
|
||||
func (b *WebSocket) removeSubscription(client adaptor.WebSocket, channel string) uint {
|
||||
clients := b.subscriptions[channel]
|
||||
for i, current := range clients {
|
||||
if current == client {
|
||||
clients = append(clients[:i], clients[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
b.subscriptions[channel] = clients
|
||||
return uint(len(clients))
|
||||
}
|
||||
Reference in New Issue
Block a user