forked from morkai/h5.coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock-008-put-negotiation.js
360 lines (323 loc) · 13.8 KB
/
block-008-put-negotiation.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
350
351
352
353
354
355
356
357
358
359
360
/*
Handle an atomic response with a different block size to a confirmable,
blockwise PUT request:
1. User sends a request with the `blockSize` option set to `128`:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0001
1 | CON | 0 bytes | PUT | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
------------------------------------------------------------------------------
Payload (128 bytes)
|-------------------------------------------------------------|
| BLOCK 1 |
|-------------------------------------------------------------|
| BLOCK 2 |
==============================================================================
2. Client recognizes that the payload of the #1 request is bigger than
the specified block size, so it constructs and sends the first Block1 request
instead:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0002
1 | CON | 0 bytes | PUT | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 0, M: true, SZX: 3 (128 bytes)
------------------------------------------------------------------------------
Payload (128 bytes)
|-------------------------------------------------------------|
| BLOCK 1 |
==============================================================================
3. Server confirms the first block, but returns a lesser block size (32 instead
of 128):
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0002
1 | ACK | 0 bytes | 2.04 Changed | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 0, M: true, SZX: 1 (32 bytes)
==============================================================================
4. Client receives the #3 confirmation. Request emits the `acknowledged` event
and the `block sent` event.
5. Client sends a request with the 5th block (128 bytes were already sent, then
the server changed the block size to 32 bytes: 128/32=4):
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0003
1 | CON | 0 bytes | PUT | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 4, M: true, SZX: 1 (32 bytes)
------------------------------------------------------------------------------
Payload (32 bytes)
|-------------------------------
==============================================================================
6. Servers confirms the 5th block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0003
1 | ACK | 0 bytes | 2.04 Changed | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 4, M: true, SZX: 1 (32 bytes)
==============================================================================
7. Client receives the #6 confirmation. Request emits the `block sent`.
8. Client sends a request with the 6th block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0004
1 | CON | 0 bytes | PUT | 4
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 5, M: true, SZX: 1 (32 bytes)
------------------------------------------------------------------------------
Payload (32 bytes)
------------------------------|
==============================================================================
9. Server confirms the 6th block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0004
1 | ACK | 0 bytes | 2.04 Changed | 4
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 5, M: true, SZX: 1 (32 bytes)
==============================================================================
10. Client receives the #9 confirmation. Request emits the `block sent` event.
11. Client sends a request with the 7th block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0005
1 | CON | 0 bytes | PUT | 5
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 6, M: true, SZX: 1 (32 bytes)
------------------------------------------------------------------------------
Payload (32 bytes)
| BLOC
==============================================================================
12. Server confirms the 7th block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0005
1 | ACK | 0 bytes | 2.04 Changed | 5
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 6, M: true, SZX: 1 (32 bytes)
==============================================================================
13. Client receives the #12 confirmation. Request emits the `block sent` event.
14. Client sends a request with the 8th, last block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0006
1 | CON | 0 bytes | PUT | 6
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : 32
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 7, M: false, SZX: 1 (32 bytes)
------------------------------------------------------------------------------
Payload (31 bytes)
K 2 |
==============================================================================
15. Server confirms the last block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0006
1 | ACK | 0 bytes | 2.04 Changed | 6
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 7, M: false, SZX: 1 (32 bytes)
==============================================================================
16. Client receives the #15 confirmation. Request emits the `block sent` event
and the `response` event.
*/
'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:99*/
var request = {
type: Message.Type.CON,
code: Message.Code.PUT,
uri: '/blocks/put/32',
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'|-------------------------------------------------------------|\n' +
'| BLOCK 1 |\n' +
'|-------------------------------------------------------------|\n' +
'| BLOCK 2 |'
)
};
var reqWithBlock0 = {
type: request.type,
code: request.code,
id: 0x0002,
uri: request.uri,
block1: {num: 0, m: true, szx: 3},
contentFormat: request.contentFormat,
payload: request.payload.slice(0 * 128, 1 * 128)
};
var resToBlock0 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock0.id,
block1: {num: 0, m: true, szx: 1}
};
var reqWithBlock4 = {
type: request.type,
code: request.code,
id: 0x0003,
uri: request.uri,
block1: {num: 4, m: true, szx: 1},
contentFormat: request.contentFormat,
payload: request.payload.slice(4 * 32, 5 * 32)
};
var resToBlock4 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock4.id,
block1: {num: 4, m: true, szx: 1}
};
var reqWithBlock5 = {
type: request.type,
code: request.code,
id: 0x0004,
uri: request.uri,
block1: {num: 5, m: true, szx: 1},
contentFormat: request.contentFormat,
payload: request.payload.slice(5 * 32, 6 * 32)
};
var resToBlock5 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock5.id,
block1: {num: 5, m: true, szx: 1}
};
var reqWithBlock6 = {
type: request.type,
code: request.code,
id: 0x0005,
uri: request.uri,
block1: {num: 6, m: true, szx: 1},
contentFormat: request.contentFormat,
payload: request.payload.slice(6 * 32, 7 * 32)
};
var resToBlock6 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock6.id,
block1: {num: 6, m: true, szx: 1}
};
var reqWithBlock7 = {
type: request.type,
code: request.code,
id: 0x0006,
uri: request.uri,
block1: {num: 7, m: false, szx: 1},
contentFormat: request.contentFormat,
payload: request.payload.slice(7 * 32, 8 * 32)
};
var resToBlock7 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock7.id,
block1: {num: 7, m: false, szx: 1}
};
ctx.socket.expectRequest(reqWithBlock0);
ctx.socket.scheduleResponse(50, resToBlock0);
ctx.socket.expectRequest(50, reqWithBlock4);
ctx.socket.scheduleResponse(100, resToBlock4);
ctx.socket.expectRequest(100, reqWithBlock5);
ctx.socket.scheduleResponse(150, resToBlock5);
ctx.socket.expectRequest(150, reqWithBlock6);
ctx.socket.scheduleResponse(200, resToBlock6);
ctx.socket.expectRequest(200, reqWithBlock7);
ctx.socket.scheduleResponse(250, resToBlock7);
var req = ctx.client.request(Message.fromObject(request), {
blockSize: 128
});
var eventSpy = sinon.spy(req, 'emit');
ctx.clock.tick(3600000);
return function assert()
{
ctx.socket.assert();
sinon.assert.callCount(eventSpy, 7);
sinon.assert.calledWith(
eventSpy, 'acknowledged', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'block sent', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'response', sinon.match.instanceOf(Message)
);
eventSpy.args[0][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[0][1], resToBlock0, "Invalid ACK."
);
eventSpy.args[1][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[1][1], resToBlock0, "Invalid `block sent` (#1)."
);
eventSpy.args[2][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[2][1], resToBlock4, "Invalid `block sent` (#2)."
);
eventSpy.args[3][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[3][1], resToBlock5, "Invalid `block sent` (#3)."
);
eventSpy.args[4][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[4][1], resToBlock6, "Invalid `block sent` (#4)."
);
eventSpy.args[5][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[5][1], resToBlock7, "Invalid `block sent` (#5)."
);
eventSpy.args[6][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[6][1], resToBlock7, "Invalid `response`."
);
};
});