-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
345 lines (294 loc) · 6.72 KB
/
message.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package coap
import (
"fmt"
"github.com/qwerty-iot/dtls/v2"
"reflect"
"strings"
"time"
"github.com/qwerty-iot/tox"
)
type Metadata struct {
ListenerName string
RemoteAddr string
DtlsPeer *dtls.Peer
ReceivedAt time.Time
BlockSize int
Server *Server
}
// Message is a CoAP message.
type Message struct {
Type COAPType
Code COAPCode
MessageID uint16
Token []byte
Payload []byte
packetSize int
opts options
queryVars map[string]string
PathVars map[string]string
Meta Metadata
}
func NewMessage() *Message {
return &Message{}
}
func (m *Message) GetBlock1() *BlockMetadata {
if oi := m.Option(OptBlock1); oi != nil {
bm, _ := blockDecode(oi)
return bm
}
return nil
}
func (m *Message) GetBlock2() *BlockMetadata {
if oi := m.Option(OptBlock2); oi != nil {
bm, _ := blockDecode(oi)
return bm
}
return nil
}
func (m *Message) getBlockKey() string {
return m.Code.String() + m.PathString() + m.QueryString()
}
func (m *Message) RequiresBlockwise() bool {
if m.Meta.BlockSize != 0 {
if m.Payload != nil && len(m.Payload) > m.Meta.BlockSize {
return true
} else {
return false
}
} else {
return false
}
}
// IsConfirmable returns true if this message is confirmable.
func (m *Message) IsConfirmable() bool {
return m.Type == TypeConfirmable
}
func (m *Message) IsRequest() bool {
return m.Code < 10
}
func (m *Message) PacketSize() int {
if m.packetSize != 0 {
return m.packetSize
} else {
return m.headerSize() + len(m.Payload)
}
}
func (m *Message) OptionsMap() map[string]interface{} {
ret := map[string]interface{}{}
for _, v := range m.opts {
def := optionDefs[v.ID]
if ro, found := ret[def.name]; found {
if ra, oka := ro.([]interface{}); oka {
ra = append(ra, v.Value)
} else {
ra = []interface{}{ro, v.Value}
ret[def.name] = ra
}
} else {
ret[def.name] = v.Value
}
}
if len(ret) == 0 {
return nil
}
return ret
}
// Options gets all the values for the given option.
func (m *Message) Options(o OptionID) []interface{} {
var rv []interface{}
for _, v := range m.opts {
if o == v.ID {
rv = append(rv, v.Value)
}
}
return rv
}
// Option gets the first value for the given option ID.
func (m *Message) Option(o OptionID) interface{} {
for _, v := range m.opts {
if o == v.ID {
return v.Value
}
}
return nil
}
func (m *Message) optionStrings(o OptionID) []string {
var rv []string
for _, o := range m.Options(o) {
rv = append(rv, o.(string))
}
return rv
}
// AddOption adds an option.
func (m *Message) WithOption(opID OptionID, val interface{}, replace bool) *Message {
if replace {
m.RemoveOption(opID)
}
iv := reflect.ValueOf(val)
if (iv.Kind() == reflect.Slice || iv.Kind() == reflect.Array) &&
iv.Type().Elem().Kind() == reflect.String {
for i := 0; i < iv.Len(); i++ {
m.opts = append(m.opts, option{opID, iv.Index(i).Interface()})
}
return m
}
m.opts = append(m.opts, option{opID, val})
return m
}
// RemoveOption removes all references to an option
func (m *Message) RemoveOption(opID OptionID) {
m.opts = m.opts.Minus(opID)
}
func (m *Message) ParseQuery() map[string]string {
if m.queryVars != nil {
return m.queryVars
}
m.queryVars = map[string]string{}
qa := m.Options(OptURIQuery)
for _, q := range qa {
if qs, ok := q.(string); ok {
ss := strings.Split(qs, "=")
if len(ss) == 2 {
m.queryVars[ss[0]] = ss[1]
} else {
m.queryVars[qs] = ""
}
}
}
return m.queryVars
}
func (m *Message) QueryString() string {
qi := m.Options(OptURIQuery)
qa := tox.ToStringArray(qi)
return strings.Join(qa, "&")
}
func (m *Message) WithQuery(q map[string]string) *Message {
for k, v := range q {
val := k
if len(v) != 0 {
val = fmt.Sprintf("%s=%s", k, v)
}
m.WithOption(OptURIQuery, val, false)
}
return m
}
// Path gets the Path set on this message if any.
func (m *Message) Path() []string {
return m.optionStrings(OptURIPath)
}
// PathString gets a path as a / separated string.
func (m *Message) PathString() string {
return strings.Join(m.Path(), "/")
}
// WithPathString sets a path by a / separated string.
func (m *Message) WithPathString(s string) *Message {
if len(s) == 0 {
return m
}
for s[0] == '/' && len(s) > 1 {
s = s[1:]
}
m.WithPath(strings.Split(s, "/"))
return m
}
// WithPath updates or adds a URIPath attribute on this message.
func (m *Message) WithPath(s []string) *Message {
m.WithOption(OptURIPath, s, true)
return m
}
func (m *Message) WithToken(token []byte) *Message {
m.Token = token
return m
}
func (m *Message) WithPayload(payload []byte) *Message {
m.Payload = payload
return m
}
func (m *Message) WithBlock1(bm *BlockMetadata) *Message {
m.WithOption(OptBlock1, bm.Encode(), true)
return m
}
func (m *Message) WithBlock2(bm *BlockMetadata) *Message {
m.WithOption(OptBlock2, bm.Encode(), true)
return m
}
func (m *Message) WithSize1(sz int) *Message {
m.WithOption(OptSize1, sz, true)
return m
}
func (m *Message) WithSize2(sz int) *Message {
m.WithOption(OptSize2, sz, true)
return m
}
func (m *Message) Accept() MediaType {
opt := m.Option(OptAccept)
if opt != nil {
return opt.(MediaType)
} else {
return None
}
}
func (m *Message) WithAccept(mt MediaType) *Message {
if mt == None {
return m
}
m.WithOption(OptAccept, mt, true)
return m
}
func (m *Message) ContentFormat() MediaType {
opt := m.Option(OptContentFormat)
if opt != nil {
return opt.(MediaType)
} else {
return None
}
}
func (m *Message) WithContentFormat(mt MediaType) *Message {
if mt == None {
return m
}
m.WithOption(OptContentFormat, mt, true)
return m
}
func (m *Message) WithType(ty COAPType) *Message {
m.Type = ty
return m
}
func (m *Message) WithCode(code COAPCode) *Message {
m.Code = code
return m
}
func (m *Message) WithLocationPath(s []string) *Message {
m.WithOption(OptLocationPath, s, true)
return m
}
func (m *Message) WithLocationPathString(path string) *Message {
for path[0] == '/' {
path = path[1:]
}
m.WithLocationPath(strings.Split(path, "/"))
return m
}
// Path gets the Path set on this message if any.
func (m *Message) LocationPath() []string {
return m.optionStrings(OptLocationPath)
}
// PathString gets a path as a / separated string.
func (m *Message) LocationPathString() string {
return strings.Join(m.LocationPath(), "/")
}
func (m *Message) MakeReply(code COAPCode, payload []byte) *Message {
rm := Message{}
if code != CodeEmpty {
rm.Token = m.Token
}
rm.MessageID = m.MessageID
rm.Type = TypeAcknowledgement
rm.Payload = payload
rm.Code = code
rm.Meta.BlockSize = m.Meta.BlockSize
return &rm
}