forked from absmach/supermq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.go
172 lines (145 loc) · 4.22 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Package coap contains the domain concept definitions needed to support
// Mainflux CoAP adapter service functionality. All constant values are taken
// from RFC, and could be adjusted based on specific use case.
package coap
import (
"context"
"fmt"
"sync"
"github.com/gogo/protobuf/proto"
"github.com/mainflux/mainflux/pkg/errors"
broker "github.com/nats-io/nats.go"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/pkg/messaging"
)
const chansPrefix = "channels"
// Exported errors
var (
ErrUnauthorized = errors.New("unauthorized access")
ErrUnsubscribe = errors.New("unable to unsubscribe")
)
// Service specifies CoAP service API.
type Service interface {
// Publish Messssage
Publish(ctx context.Context, key string, msg messaging.Message) error
// Subscribes to channel with specified id, subtopic and adds subscription to
// service map of subscriptions under given ID.
Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error
// Unsubscribe method is used to stop observing resource.
Unsubscribe(ctx context.Context, key, chanID, subptopic, token string) error
}
var _ Service = (*adapterService)(nil)
// Observers is a map of maps,
type adapterService struct {
auth mainflux.ThingsServiceClient
conn *broker.Conn
observers map[string]observers
obsLock sync.Mutex
}
// New instantiates the CoAP adapter implementation.
func New(auth mainflux.ThingsServiceClient, nc *broker.Conn) Service {
as := &adapterService{
auth: auth,
conn: nc,
observers: make(map[string]observers),
obsLock: sync.Mutex{},
}
return as
}
func (svc *adapterService) Publish(ctx context.Context, key string, msg messaging.Message) error {
ar := &mainflux.AccessByKeyReq{
Token: key,
ChanID: msg.Channel,
}
thid, err := svc.auth.CanAccessByKey(ctx, ar)
if err != nil {
return errors.Wrap(ErrUnauthorized, err)
}
msg.Publisher = thid.GetValue()
data, err := proto.Marshal(&msg)
if err != nil {
return err
}
subject := fmt.Sprintf("%s.%s", chansPrefix, msg.Channel)
if msg.Subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, msg.Subtopic)
}
return svc.conn.Publish(subject, data)
}
func (svc *adapterService) Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error {
ar := &mainflux.AccessByKeyReq{
Token: key,
ChanID: chanID,
}
if _, err := svc.auth.CanAccessByKey(ctx, ar); err != nil {
return errors.Wrap(ErrUnauthorized, err)
}
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
go func() {
<-c.Done()
svc.remove(subject, c.Token())
}()
obs, err := NewObserver(subject, c, svc.conn)
if err != nil {
c.Cancel()
return err
}
return svc.put(subject, c.Token(), obs)
}
func (svc *adapterService) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) error {
ar := &mainflux.AccessByKeyReq{
Token: key,
ChanID: chanID,
}
if _, err := svc.auth.CanAccessByKey(ctx, ar); err != nil {
return errors.Wrap(ErrUnauthorized, err)
}
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
return svc.remove(subject, token)
}
func (svc *adapterService) put(endpoint, token string, o Observer) error {
svc.obsLock.Lock()
defer svc.obsLock.Unlock()
obs, ok := svc.observers[endpoint]
// If there are no observers, create map and assign it to the endpoint.
if !ok {
obs = observers{token: o}
svc.observers[endpoint] = obs
return nil
}
// If observer exists, cancel subscription and replace it.
if sub, ok := obs[token]; ok {
if err := sub.Cancel(); err != nil {
return errors.Wrap(ErrUnsubscribe, err)
}
}
obs[token] = o
return nil
}
func (svc *adapterService) remove(endpoint, token string) error {
svc.obsLock.Lock()
defer svc.obsLock.Unlock()
obs, ok := svc.observers[endpoint]
if !ok {
return nil
}
if current, ok := obs[token]; ok {
if err := current.Cancel(); err != nil {
return errors.Wrap(ErrUnsubscribe, err)
}
}
delete(obs, token)
// If there are no observers left for the endpint, remove the map.
if len(obs) == 0 {
delete(svc.observers, endpoint)
}
return nil
}