1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/ws/adapter.go
Nikola Marčetić 42b3682352
MF-415 - Merge mProxy support (#1045)
* NOISSUE - Add mProxy support (#1017)

* Add mproxy

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Fix docker and add EMQ compose

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Fix EMQX name

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Add nats, auth and es

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Removed unucessary vendoring

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Update vendoring

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix mproxy interface implementation

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

 NOISSUE - Aligned Event interface method signatures with new spec (#1025)

* Aligned Event interface method signatures with new spec

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Updated deps

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

NOISSUE - Update mproxy dependency (#1038)

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Update Vendor with new mProxy (#1043)

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

Twins merge conflict reverted

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Twins merge conflict reverted

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Twins fixed nats import

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Update deps

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Fixed Event interface Unsubscribe() typo

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Update vendors

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

* Upgrade CI script

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2020-02-26 17:14:16 +01:00

105 lines
2.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Package ws contains the domain concept definitions needed to support
// Mainflux ws adapter service functionality.
package ws
import (
"context"
"errors"
"sync"
"github.com/mainflux/mainflux"
broker "github.com/nats-io/nats.go"
)
var (
// ErrFailedMessagePublish indicates that message publishing failed.
ErrFailedMessagePublish = errors.New("failed to publish message")
// ErrFailedSubscription indicates that client couldn't subscribe to specified channel.
ErrFailedSubscription = errors.New("failed to subscribe to a channel")
// ErrFailedConnection indicates that service couldn't connect to message broker.
ErrFailedConnection = errors.New("failed to connect to message broker")
)
// Service specifies web socket service API.
type Service interface {
mainflux.MessagePublisher
// Subscribes to channel with specified id.
Subscribe(string, string, *Channel) error
}
// Channel is used for receiving and sending messages.
type Channel struct {
Messages chan mainflux.Message
Closed chan bool
closed bool
mutex sync.Mutex
}
// NewChannel instantiates empty channel.
func NewChannel() *Channel {
return &Channel{
Messages: make(chan mainflux.Message),
Closed: make(chan bool),
closed: false,
mutex: sync.Mutex{},
}
}
// Send method send message over Messages channel.
func (channel *Channel) Send(msg mainflux.Message) {
channel.mutex.Lock()
defer channel.mutex.Unlock()
if !channel.closed {
channel.Messages <- msg
}
}
// Close channel and stop message transfer.
func (channel *Channel) Close() {
channel.mutex.Lock()
defer channel.mutex.Unlock()
channel.closed = true
channel.Closed <- true
close(channel.Messages)
close(channel.Closed)
}
var _ Service = (*adapterService)(nil)
type adapterService struct {
pubsub Service
}
// New instantiates the WS adapter implementation.
func New(pubsub Service) Service {
return &adapterService{pubsub: pubsub}
}
func (as *adapterService) Publish(ctx context.Context, token string, msg mainflux.Message) error {
if err := as.pubsub.Publish(ctx, token, msg); err != nil {
switch err {
case broker.ErrConnectionClosed, broker.ErrInvalidConnection:
return ErrFailedConnection
default:
return ErrFailedMessagePublish
}
}
return nil
}
func (as *adapterService) Subscribe(chanID, subtopic string, channel *Channel) error {
if err := as.pubsub.Subscribe(chanID, subtopic, channel); err != nil {
return ErrFailedSubscription
}
return nil
}