-
Notifications
You must be signed in to change notification settings - Fork 11
/
observe-015-blockwise-notification.js
349 lines (312 loc) · 12.7 KB
/
observe-015-blockwise-notification.js
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
346
347
348
349
/*
Handle a blockwise notification:
1. Client sends a request:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 1 0 | 0 0 0 0 0 0 0 1 | 0x0001
1 | CON | 2 bytes | GET | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 0
Uri-Path: observer
Uri-Path: blocks
==============================================================================
2. Server sends a piggy-backed response with a Block2 option:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x0001
1 | ACK | 0 bytes | 2.05 Content | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 1
Content-Format: text/plain;charset=utf-8
Max-Age : 10
Block2 : NUM: 0, M: true, SZX: 0 (16 bytes)
------------------------------------------------------------------------------
Payload (16 bytes)
1111111111111111
==============================================================================
3. Client receives the #2 response. Request emits the `acknowledged` event and
the `block received` event.
4. Server sends the second block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x4321
1 | CON | 0 bytes | 2.05 Content | 17185
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 1
Content-Format: text/plain;charset=utf-8
Max-Age : 10
Block2 : NUM: 1, M: false, SZX: 0 (16 bytes)
------------------------------------------------------------------------------
Payload (16 bytes)
2222222222222222
==============================================================================
5. Client receives the #4 block and sends the acknowledgement:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0x4321
1 | ACK | 0 bytes | Empty | 17185
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
==============================================================================
6. The #1 request emits the `block received` event and the `response` event.
7. After 2s, server sends the first block of the first notification:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x4322
1 | CON | 0 bytes | 2.05 Content | 17186
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 2
Content-Format: text/plain;charset=utf-8
Max-Age : 10
Block2 : NUM: 0, M: true, SZX: 0 (16 bytes)
------------------------------------------------------------------------------
Payload (16 bytes)
3333333333333333
==============================================================================
8. Client receives the #7 message and sends an acknowledgement:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0x4322
1 | ACK | 0 bytes | Empty | 17186
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
==============================================================================
9. The #1 request emits the `block received` event.
10. Server receives the #8 acknowledgement and sends the next block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x4323
1 | CON | 0 bytes | 2.05 Content | 17187
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 2
Content-Format: text/plain;charset=utf-8
Max-Age : 10
Block2 : NUM: 1, M: false, SZX: 0 (16 bytes)
------------------------------------------------------------------------------
Payload (16 bytes)
4444444444444444
==============================================================================
11. Client receives the #10 message and acknowledges it:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0x4323
1 | ACK | 0 bytes | Empty | 17187
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
==============================================================================
12. The #1 request emits the `block received` event and the `response` event.
13. Client cancels the #1 observer. The #1 request emits the `cancelled` event.
14. After 2s, server sends the first block of the second notification:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x4324
1 | CON | 0 bytes | 2.05 Content | 17188
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 3
Content-Format: text/plain;charset=utf-8
Max-Age : 10
Block2 : NUM: 0, M: true, SZX: 0 (16 bytes)
------------------------------------------------------------------------------
Payload (16 bytes)
5555555555555555
==============================================================================
15. Client receives the #14 message but resets it:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 1 | 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0x4324
1 | RST | 0 bytes | Empty | 17188
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
==============================================================================
16. Server receives the #15 reset and removes the #1 observer. No more
notifications are sent.
*/
'use strict';
require('should');
var sinon = require('sinon');
var helpers = require('../helpers');
var Message = require(helpers.LIB_DIR).Message;
helpers.test(__filename, function(ctx)
{
/*jshint maxstatements:999*/
var request = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0001,
token: new Buffer([]),
uri: '/observer/blocks',
observe: 0
};
var responseBlock0 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: request.id,
token: request.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 10,
observe: 1,
block2: {num: 0, m: true, szx: 0},
payload: new Buffer('1111111111111111')
};
var responseBlock1 = {
type: Message.Type.CON,
code: Message.Code.CONTENT,
id: 0x4321,
token: request.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 10,
observe: 1,
block2: {num: 1, m: false, szx: 0},
payload: new Buffer('2222222222222222')
};
var responseBlock1Ack = {
type: Message.Type.ACK,
code: Message.Code.EMPTY,
id: responseBlock1.id
};
var response = {
type: responseBlock1.type,
code: responseBlock1.code,
id: responseBlock1.id,
token: responseBlock1.token,
contentFormat: responseBlock1.contentFormat,
maxAge: responseBlock1.maxAge,
observe: responseBlock1.observe,
block2: responseBlock1.block2,
payload: Buffer.concat([responseBlock0.payload, responseBlock1.payload])
};
var notification1Block0 = {
type: Message.Type.CON,
code: Message.Code.CONTENT,
id: 0x4322,
token: request.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 10,
observe: 2,
block2: {num: 0, m: true, szx: 0},
payload: new Buffer('3333333333333333')
};
var notification1Block0Ack = {
type: Message.Type.ACK,
code: Message.Type.EMPTY,
id: notification1Block0.id
};
var notification1Block1 = {
type: Message.Type.CON,
code: Message.Code.CONTENT,
id: 0x4323,
token: request.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 10,
observe: 2,
block2: {num: 1, m: false, szx: 0},
payload: new Buffer('4444444444444444')
};
var notification1Block1Ack = {
type: Message.Type.ACK,
code: Message.Type.EMPTY,
id: notification1Block1.id
};
var notification1Response = {
type: notification1Block1.type,
code: notification1Block1.code,
id: notification1Block1.id,
token: notification1Block1.token,
contentFormat: notification1Block1.contentFormat,
maxAge: notification1Block1.maxAge,
observe: notification1Block1.observe,
block2: notification1Block1.block2,
payload: Buffer.concat([
notification1Block0.payload,
notification1Block1.payload
])
};
var notification2Block0 = {
type: Message.Type.CON,
code: Message.Code.CONTENT,
id: 0x4324,
token: request.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 10,
observe: 3,
block2: {num: 0, m: true, szx: 0},
payload: new Buffer('5555555555555555')
};
var notification2Block0Rst = {
type: Message.Type.RST,
code: Message.Type.EMPTY,
id: notification2Block0.id
};
ctx.socket.expectRequest(request);
ctx.socket.scheduleResponse(50, responseBlock0);
ctx.socket.scheduleResponse(75, responseBlock1);
ctx.socket.expectRequest(75, responseBlock1Ack);
ctx.socket.scheduleResponse(2050, notification1Block0);
ctx.socket.expectRequest(2050, notification1Block0Ack);
ctx.socket.scheduleResponse(2075, notification1Block1);
ctx.socket.expectRequest(2075, notification1Block1Ack);
ctx.socket.scheduleResponse(4050, notification2Block0);
ctx.socket.expectRequest(4050, notification2Block0Rst);
var req = ctx.client.request(Message.fromObject(request));
setTimeout(function() { ctx.client.cancel(req); }, 3000);
var eventSpy = sinon.spy(req, 'emit');
ctx.clock.tick(3600000);
return function assert()
{
ctx.socket.assert();
sinon.assert.callCount(eventSpy, 8);
sinon.assert.calledWith(
eventSpy, 'acknowledged', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'block received', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'response', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'cancelled'
);
eventSpy.args[0][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[0][1], responseBlock0, "Invalid ACK."
);
eventSpy.args[1][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[1][1], responseBlock0, "Invalid `block received` #1."
);
eventSpy.args[2][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[2][1], responseBlock1, "Invalid `block received` #2."
);
eventSpy.args[3][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[3][1], response, "Invalid `response` #1."
);
eventSpy.args[4][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[4][1], notification1Block0, "Invalid `block received` #3."
);
eventSpy.args[5][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[5][1], notification1Block1, "Invalid `block received` #4."
);
eventSpy.args[6][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[6][1], notification1Response, "Invalid `response` #2."
);
eventSpy.args[7][0].should.be.equal('cancelled');
};
});