Skip to content

Commit

Permalink
server can return in block2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen Quoc Dinh committed Jun 14, 2014
1 parent 450d40c commit 55c4658
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
63 changes: 57 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,20 @@ CoAPServer.prototype._handle = function(packet, rsinfo) {
request.rsinfo = rsinfo
response.statusCode = '2.05'

this.emit('request', request, response)
response._request = request

// if this kindof request answser is cached
// todo, how to make cache
if (cachedPayload) {
console.log('went to send directly')
response.end(cachedPayload);
}
// else, when this request got a buffered blockwise already
// dont bother uper layer
else {
console.log('get from uper layer')
this.emit('request', request, response)
}
}

function toKey(address, port, packet, appendToken) {
Expand All @@ -180,8 +193,8 @@ function toKey(address, port, packet, appendToken) {
}

// new out message
// inherit from Outgoing message
// to handler blockwise (2)
// inherit from OutgoingMessage
// to handle cached answer and blockwise (2)
function OutMessage() {
OutgoingMessage.apply(this, Array.prototype.slice.call(arguments));
}
Expand All @@ -191,9 +204,19 @@ var maxBlock2 = 1024 //16, 32, 64, must <= 2**(6+4)
var totalBlock
var isLastBlock

// todo: define a new cached buffer
var cachedPayload

OutMessage.prototype.end= function(payload) {
var that = this
var requestedBlockOption = _parseBlock2(that._packet.options)
var requestedBlockOption = _parseBlock2(that._request.options)

// todo: new cached buffer
cachedPayload = payload

// if payload is suitable for ONE message, shoot it out
if ((!requestedBlockOption) && (payload.length < maxBlock2))
return OutgoingMessage.prototype.end.call(this, payload)

// for the first request, block2 option may be missed
if (!requestedBlockOption)
Expand All @@ -206,6 +229,9 @@ OutMessage.prototype.end= function(payload) {
if (requestedBlockOption.size > maxBlock2)
requestedBlockOption.size = maxBlock2


// otherwise, utilize blockwise (block2)

// block number should have limit
totalBlock = Math.ceil(payload.length/requestedBlockOption.size)
if (requestedBlockOption.num < totalBlock)
Expand All @@ -221,8 +247,15 @@ OutMessage.prototype.end= function(payload) {

var block2 = _createBlock(requestedBlockOption, isLastBlock)
console.log('return block2 = ', block2)
that._packet.setOption('Block2', block2)
that._packet.setOption('ETag', '123456')
this._packet = _packetSetBlock2(this._packet, block2)
// that._packet.setOption('Block2', block2)
// that._packet.setOption('ETag', '123456')

// console.log('that _packet')
// console.log(that._packet)
// console.log('--------------this')
// console.log(that)

OutgoingMessage.prototype.end.call(this, payload.slice((requestedBlockOption.num)*requestedBlockOption.size, (requestedBlockOption.num+1)*requestedBlockOption.size))
// OutgoingMessage.prototype.end.call(this, a, b);
};
Expand Down Expand Up @@ -292,4 +325,22 @@ function _createBlock(requestedBlock, isLastBlock) {
return (extraNum)? Buffer.concat([extraNum, new Buffer([byte])]):new Buffer([byte])
}

// need to rewrite this function
function _packetSetBlock2(packet, block2) {
var hasBlock = false;
for (i in packet.options) {
if (packet.options[i] && packet.options[i].name == 'Block2') {
packet.options[i].value = block2
hasBlock = true;
}
}
if (!hasBlock) {
packet.options.push({
name: 'Block2',
value: block2
})
}
return packet
}

module.exports = CoAPServer
11 changes: 3 additions & 8 deletions nqd/test-block2-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

const coap = require('./..')
, server = coap.createServer()
, payload = new Buffer(10000)
, payload = new Buffer(10)

var maxBlock2 = 1024 //16, 32, 64, must <= 2**(6+4)
var totalBlock
Expand All @@ -20,22 +20,17 @@ var isLastBlock
// server
// plays split up for blockwise
server.on('request', function(req, res) {
res.setOption('Content-Format', 'application/json')
res.end(payload)
})

// the default CoAP port is 5683
server.listen(function() {
var req = coap.request('coap://localhost/Matteo')
// test early negotiation
req.setOption('Block2', new Buffer([0x06])) // block size of 1024
req.setOption('Block2', new Buffer([0x0])) // block size of 1024
// test later negotiation (?)
req.on('response', function(res) {
// console.log(res)
// console.log('receive')
// res.pipe(process.stdout)
// console.log('--------------')
// console.log(payload.toString())
// console.log('--------------')
console.log('result: ', res.payload.toString() == payload.toString())
res.on('end', function() {
process.exit(0)
Expand Down

0 comments on commit 55c4658

Please sign in to comment.