From 5451b2ec35403a851e64df9b89a846047b77b78e Mon Sep 17 00:00:00 2001 From: PeterEB Date: Tue, 14 Mar 2017 18:36:39 +0800 Subject: [PATCH] change API setIncomingDevPredicate to acceptDevIncoming callback --- lib/coap-shepherd.js | 24 +++++++-------- lib/components/reqHandler.js | 56 +++++++++++++++++++++------------ lib/database/coap.db | 0 test/coap-shepherd.test.js | 60 +++++++++++++++++++++--------------- test/coapdb.test.js | 6 ++++ test/database_test/coap.db | 0 6 files changed, 90 insertions(+), 56 deletions(-) delete mode 100644 lib/database/coap.db delete mode 100644 test/database_test/coap.db diff --git a/lib/coap-shepherd.js b/lib/coap-shepherd.js index 38aceb0..d7dd91c 100644 --- a/lib/coap-shepherd.js +++ b/lib/coap-shepherd.js @@ -2,11 +2,13 @@ var Q = require('q'), fs = require('fs'), + path = require('path'), util = require('util'), EventEmitter = require('events').EventEmitter, Readable = require('stream').Readable, - network = require('network'), - proving = require('proving'), + network = require('network'); + +var proving = require('proving'), _ = require('busyman'), coap = require('coap'), debug = require('debug')('coap-shepherd'), @@ -30,10 +32,6 @@ function CoapShepherd() { this.clientIdCount = 1; - this.incomingDevPredicate = function () { - return true; - }; - this._net = { intf: '', ip: config.ip || '', @@ -64,6 +62,13 @@ function CoapShepherd() { coap.updateTiming({ maxLatency: (reqTimeout - 47) / 2 }); + + this.acceptDevIncoming = function (devInfo, callback) { // Override at will. + setImmediate(function () { + var accepted = true; + callback(null, accepted); + }); + }; } util.inherits(CoapShepherd, EventEmitter); @@ -310,13 +315,6 @@ CoapShepherd.prototype.remove = function (clientName, callback) { return deferred.promise.nodeify(callback); }; -CoapShepherd.prototype.setIncomingDevPredicate = function (predicate) { - proving.fn(predicate, 'predicate must be a function'); - - this.incomingDevPredicate = predicate; - return true; -}; - CoapShepherd.prototype._newClientId = function (id) { if (!_.isUndefined(id)) proving.number(id, 'id should be a number.'); diff --git a/lib/components/reqHandler.js b/lib/components/reqHandler.js index fb85627..84643d6 100644 --- a/lib/components/reqHandler.js +++ b/lib/components/reqHandler.js @@ -1,6 +1,7 @@ 'use strict'; -var _ = require('busyman'), +var Q = require('q'), + _ = require('busyman'), debug = require('debug')('coap-shepherd:reqHdlr'); var CoapNode = require('./coap-node.js'), @@ -8,7 +9,7 @@ var CoapNode = require('./coap-node.js'), /**** Code Enumerations ****/ var RSP = { ok: '2.00', created: '2.01', deleted: '2.02', changed: '2.04', content: '2.05', badreq: '4.00', - unauth: '4.01', forbid: '4.03', notfound: '4.04', notallowed: '4.05', timeout: '4.08', dberror: '5.00' }; + unauth: '4.01', forbid: '4.03', notfound: '4.04', notallowed: '4.05', timeout: '4.08', serverError: '5.00' }; /********************************************************* * Handler function * @@ -57,8 +58,6 @@ function clientRegisterHandler (shepherd, req, rsp) { return sendRsp(rsp, RSP.badreq, '', 'register'); else if (shepherd._joinable === false) return sendRsp(rsp, RSP.notallowed, '', 'register'); - else if (!cnode && !shepherd.incomingDevPredicate(devAttrs)) - return sendRsp(rsp, RSP.notallowed, '', 'register'); function clientRealRegistered(firstRegistered) { _.delay(function() { @@ -78,25 +77,44 @@ function clientRegisterHandler (shepherd, req, rsp) { }).fail(function (err) { if (errCount < 2) { errCount += 1; - clientRealRegistered(); + return clientRealRegistered(); } else { errCount = 0; - shepherd.emit('error', err); + return shepherd.emit('error', err); } }).done(); }, 50); } if (!cnode) { - cnode = new CoapNode(shepherd, devAttrs); - shepherd._registry[devAttrs.clientName] = cnode; - cnode._registered = true; - cnode._heartbeat = cutils.getTime(); - cnode.lifeCheck(true); - - rsp.setOption('Location-Path', [new Buffer('rd'), new Buffer(cnode.clientId.toString())]); - sendRsp(rsp, RSP.created, '', 'register'); - return clientRealRegistered(true); + Q.fcall(function () { + var allowDevIncoming; + + if (_.isFunction(shepherd.acceptDevIncoming)) { + allowDevIncoming = Q.nbind(shepherd.acceptDevIncoming, shepherd); + return allowDevIncoming(devAttrs); + } else { + return true; + } + }).then(function (accepted) { + if (accepted) { + cnode = new CoapNode(shepherd, devAttrs); + shepherd._registry[devAttrs.clientName] = cnode; + cnode._registered = true; + cnode._heartbeat = cutils.getTime(); + cnode.lifeCheck(true); + + rsp.setOption('Location-Path', [new Buffer('rd'), new Buffer(cnode.clientId.toString())]); + sendRsp(rsp, RSP.created, '', 'register'); + return clientRealRegistered(true); + } else { + sendRsp(rsp, RSP.notallowed, '', 'register'); + return accepted; + } + }, function (err) { + sendRsp(rsp, RSP.serverError, '', 'register'); + shepherd.emit('error', err); + }).done(); } else { cnode._updateAttrs(devAttrs).then(function (diff) { cnode._registered = true; @@ -107,7 +125,7 @@ function clientRegisterHandler (shepherd, req, rsp) { sendRsp(rsp, RSP.changed, '', 'register'); return clientRealRegistered(false); }, function (err) { - sendRsp(rsp, RSP.dberror, '', 'register'); + sendRsp(rsp, RSP.serverError, '', 'register'); shepherd.emit('error', err); }).done(); } @@ -152,7 +170,7 @@ function clientUpdateHandler (shepherd, req, rsp) { data: msg }); }, function (err) { - sendRsp(rsp, RSP.dberror, '', 'update'); + sendRsp(rsp, RSP.serverError, '', 'update'); shepherd.emit('error', err); }).done(); } else { @@ -172,7 +190,7 @@ function clientDeregisterHandler (shepherd, req, rsp) { shepherd.remove(clientName).then(function () { sendRsp(rsp, RSP.deleted, '', 'deregister'); }, function (err) { - sendRsp(rsp, RSP.dberror, '', 'deregister'); + sendRsp(rsp, RSP.serverError, '', 'deregister'); }).done(); } else { sendRsp(rsp, RSP.notfound, '', 'deregister'); @@ -223,7 +241,7 @@ function clientCheckHandler (shepherd, req, rsp) { sendRsp(rsp, RSP.changed, '', 'check'); startHeartbeat(); }, function (err) { - sendRsp(rsp, RSP.dberror, '', 'check'); + sendRsp(rsp, RSP.serverError, '', 'check'); shepherd.emit('error', err); }).done(); } diff --git a/lib/database/coap.db b/lib/database/coap.db deleted file mode 100644 index e69de29..0000000 diff --git a/test/coap-shepherd.test.js b/test/coap-shepherd.test.js index 01facb0..4211531 100644 --- a/test/coap-shepherd.test.js +++ b/test/coap-shepherd.test.js @@ -165,23 +165,6 @@ describe('coap-shepherd', function () { }); }); - describe('#.setIncomingDevPredicate()', function () { - it('should throw TypeError if clientName is not a string', function () { - expect(function () { return shepherd.setIncomingDevPredicate(); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(undefined); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(null); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(NaN); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(10); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate('xx'); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate({}); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate([]); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(true); }).to.throw(TypeError); - expect(function () { return shepherd.setIncomingDevPredicate(new Date()); }).to.throw(TypeError); - - expect(function () { return shepherd.setIncomingDevPredicate(function () { return true; }); }).not.to.throw(TypeError); - }); - }); - describe('#._newClientId()', function () { it('should throw TypeError if id is not a number', function () { expect(function () { return shepherd._newClientId(null); }).to.throw(TypeError); @@ -623,8 +606,8 @@ describe('coap-shepherd', function () { }); }); - describe('#.setIncomingDevPredicate()', function () { - it('should set incomingDevPredicate and get not allow rsp', function (done) { + describe('#.acceptDevIncoming()', function () { + it('should implement acceptDevIncoming and get not allow rsp', function (done) { var rsp = {}; rsp.end = function (msg) { @@ -635,12 +618,41 @@ describe('coap-shepherd', function () { } }; - shepherd.setIncomingDevPredicate(function (devInfo) { - if (devInfo.clientName === 'cnode03') - return false; + shepherd.acceptDevIncoming = function (devInfo, callback) { + if (devInfo.clientName === 'cnode03') { + callback(null, false); + } else { + callback(null, true); + } + }; - return true; - }); + emitClintReqMessage(shepherd, { + code: '0.01', + method: 'POST', + url: '/rd?ep=cnode03<=86400&lwm2m=1.0.0&mac=BB:BB:BB', + rsinfo: { + address: '127.0.0.1', + port: '5687' + }, + payload: ',,,', + headers: {} + }, rsp); + }); + + it('should implement acceptDevIncoming and create dev', function (done) { + var rsp = {}; + + rsp.setOption = sinon.spy(); + rsp.end = function (msg) { + expect(rsp.code).to.be.eql('2.01'); + if (shepherd.find('cnode03')) { + done(); + } + }; + + shepherd.acceptDevIncoming = function (devInfo, callback) { + callback(null, true); + }; emitClintReqMessage(shepherd, { code: '0.01', diff --git a/test/coapdb.test.js b/test/coapdb.test.js index f72bf89..e15aad0 100644 --- a/test/coapdb.test.js +++ b/test/coapdb.test.js @@ -8,6 +8,12 @@ var Coapdb = require('../lib/components/coapdb.js'); var dbPath = path.resolve('./test/database_test/coap.db'), coapdb; +try { + fs.statSync('./test/database_test'); +} catch (e) { + fs.mkdirSync('./test/database_test'); +} + var nodeMock1 = { clientName: 'mock01', locationPath: '1', diff --git a/test/database_test/coap.db b/test/database_test/coap.db deleted file mode 100644 index e69de29..0000000