mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* NOISSUEE - Create broker package for NATS Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Create funcs to return NATS connection Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * mv os.exit to main Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix Reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix tests and typos Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix CI Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Unify Publisher and Subscriber interfaces Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename Nats interface Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Mv message.pb.go, messsage.proto and topics.go to broker directory Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix go.mod Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use mainflux broker for writers and twins services Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix go.mod Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix twins tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix make proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix message.proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix golangcibot Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * regenerate message.pb.go Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix make proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add NATS errors Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package coap
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/broker"
|
|
)
|
|
|
|
// Observer is used to handle CoAP subscription.
|
|
type Observer struct {
|
|
// Expired flag is used to mark that ticker sent a
|
|
// CON message, but response is not received yet.
|
|
// The flag changes its value once ACK message is
|
|
// received from the client. If Expired is true
|
|
// when ticker is triggered, Observer should be canceled
|
|
// and removed from the Service map.
|
|
expired bool
|
|
|
|
// Message ID for notification messages.
|
|
msgID uint16
|
|
|
|
expiredLock, msgIDLock sync.Mutex
|
|
|
|
// Messages is used to receive messages from NATS.
|
|
Messages chan broker.Message
|
|
|
|
// Cancel channel is used to cancel observing resource.
|
|
// Cancel channel should not be used to send or receive any
|
|
// data, it's purpose is to be closed once Observer canceled.
|
|
Cancel chan bool
|
|
}
|
|
|
|
// NewObserver instantiates a new Observer.
|
|
func NewObserver() *Observer {
|
|
return &Observer{
|
|
Messages: make(chan broker.Message),
|
|
Cancel: make(chan bool),
|
|
}
|
|
}
|
|
|
|
// LoadExpired reads Expired flag in thread-safe way.
|
|
func (o *Observer) LoadExpired() bool {
|
|
o.expiredLock.Lock()
|
|
defer o.expiredLock.Unlock()
|
|
|
|
return o.expired
|
|
}
|
|
|
|
// StoreExpired stores Expired flag in thread-safe way.
|
|
func (o *Observer) StoreExpired(val bool) {
|
|
o.expiredLock.Lock()
|
|
defer o.expiredLock.Unlock()
|
|
|
|
o.expired = val
|
|
}
|
|
|
|
// LoadMessageID reads MessageID and increments
|
|
// its value in thread-safe way.
|
|
func (o *Observer) LoadMessageID() uint16 {
|
|
o.msgIDLock.Lock()
|
|
defer o.msgIDLock.Unlock()
|
|
|
|
o.msgID++
|
|
return o.msgID
|
|
}
|