forked from amenzhinsky/iothub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux.go
256 lines (223 loc) · 4.42 KB
/
mux.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package iotdevice
import (
"encoding/json"
"fmt"
"sync"
"gitlab.com/michaeljohn/iothub/common"
)
// once wraps a function that can return an error and
// executes it only once, all sequential calls return nil.
func once(on *sync.Once, fn func() error) error {
var err error
on.Do(func() {
err = fn()
})
return err
}
func newEventsMux() *eventsMux {
return &eventsMux{done: make(chan struct{})}
}
type eventsMux struct {
on sync.Once
mu sync.RWMutex
subs []*EventSub
done chan struct{}
}
func (m *eventsMux) once(fn func() error) error {
return once(&m.on, fn)
}
func (m *eventsMux) Dispatch(msg *common.Message) {
m.mu.RLock()
for _, s := range m.subs {
select {
case <-s.done:
case <-m.done:
case s.ch <- msg:
}
}
m.mu.RUnlock()
}
func (m *eventsMux) sub() *EventSub {
s := newEventSub()
m.mu.Lock()
m.subs = append(m.subs, s)
m.mu.Unlock()
return s
}
func (m *eventsMux) unsub(s *EventSub) {
m.mu.Lock()
for i, ss := range m.subs {
if ss == s {
s.close(nil)
m.subs = append(m.subs[:i], m.subs[i+1:]...)
break
}
}
m.mu.Unlock()
}
func (m *eventsMux) close() {
m.mu.Lock()
select {
case <-m.done:
panic("already closed")
default:
}
close(m.done)
for _, s := range m.subs {
s.close(ErrClosed)
}
m.subs = m.subs[0:0]
m.mu.Unlock()
}
func newEventSub() *EventSub {
return &EventSub{
ch: make(chan *common.Message, 10), // TODO: configurable value
done: make(chan struct{}),
}
}
type EventSub struct {
ch chan *common.Message
err error
done chan struct{}
}
func (s *EventSub) C() <-chan *common.Message {
return s.ch
}
func (s *EventSub) Err() error {
return s.err
}
func (s *EventSub) close(err error) {
s.err = err
close(s.done)
close(s.ch)
}
func newTwinStateMux() *twinStateMux {
return &twinStateMux{done: make(chan struct{})}
}
type twinStateMux struct {
on sync.Once
mu sync.RWMutex
subs []*TwinStateSub
done chan struct{}
}
func (m *twinStateMux) once(fn func() error) error {
return once(&m.on, fn)
}
func (m *twinStateMux) Dispatch(b []byte) {
m.mu.RLock()
select {
case <-m.done:
panic("already closed")
default:
}
for _, sub := range m.subs {
go func(sub *TwinStateSub) {
select {
case sub.ch <- b:
case <-m.done:
}
}(sub)
}
m.mu.RUnlock()
}
func (m *twinStateMux) sub() *TwinStateSub {
s := &TwinStateSub{ch: make(chan TwinState, 10)}
m.mu.Lock()
m.subs = append(m.subs, s)
m.mu.Unlock()
return s
}
func (m *twinStateMux) unsub(s *TwinStateSub) {
m.mu.Lock()
for i, ss := range m.subs {
if ss == s {
m.subs = append(m.subs[:i], m.subs[i+1:]...)
break
}
}
m.mu.Unlock()
}
func (m *twinStateMux) close() {
m.mu.Lock()
for _, s := range m.subs {
s.err = ErrClosed
close(s.ch)
}
m.subs = m.subs[0:0]
m.mu.Unlock()
}
type TwinStateSub struct {
ch chan TwinState
err error
}
func (s *TwinStateSub) C() <-chan TwinState {
return s.ch
}
func (s *TwinStateSub) Err() error {
return s.err
}
func newMethodMux() *methodMux {
return &methodMux{}
}
// methodMux is direct-methods dispatcher.
type methodMux struct {
on sync.Once
mu sync.RWMutex
m map[string]DirectMethodHandler
}
func (m *methodMux) once(fn func() error) error {
return once(&m.on, fn)
}
// handle registers the given direct-method handler.
func (m *methodMux) handle(method string, fn DirectMethodHandler) error {
if fn == nil {
panic("fn is nil")
}
m.mu.Lock()
if m.m == nil {
m.m = map[string]DirectMethodHandler{}
}
if _, ok := m.m[method]; ok {
m.mu.Unlock()
return fmt.Errorf("method %q is already registered", method)
}
m.m[method] = fn
m.mu.Unlock()
return nil
}
// remove deregisters the named method.
func (m *methodMux) remove(method string) {
m.mu.Lock()
if m.m != nil {
delete(m.m, method)
}
m.mu.Unlock()
}
// Dispatch dispatches the named method, error is not nil only when dispatching fails.
func (m *methodMux) Dispatch(method string, b []byte) (int, []byte, error) {
m.mu.RLock()
f, ok := m.m[method]
m.mu.RUnlock()
if !ok {
return 0, nil, fmt.Errorf("method %q is not registered", method)
}
var v map[string]interface{}
if err := json.Unmarshal(b, &v); err != nil {
return jsonErr(err)
}
code, v, err := f(v)
if err != nil {
return jsonErr(err)
}
if v == nil {
v = map[string]interface{}{}
}
b, err = json.Marshal(v)
if err != nil {
return jsonErr(err)
}
return code, b, nil
}
func jsonErr(err error) (int, []byte, error) {
return 500, []byte(fmt.Sprintf(`{"error":%q}`, err.Error())), nil
}