Skip to content

Commit

Permalink
change API setIncomingDevPredicate to acceptDevIncoming callback
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterEB committed Mar 14, 2017
1 parent e65fc36 commit 5451b2e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 56 deletions.
24 changes: 11 additions & 13 deletions lib/coap-shepherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -30,10 +32,6 @@ function CoapShepherd() {

this.clientIdCount = 1;

this.incomingDevPredicate = function () {
return true;
};

this._net = {
intf: '',
ip: config.ip || '',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.');
Expand Down
56 changes: 37 additions & 19 deletions lib/components/reqHandler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

var _ = require('busyman'),
var Q = require('q'),
_ = require('busyman'),
debug = require('debug')('coap-shepherd:reqHdlr');

var CoapNode = require('./coap-node.js'),
cutils = require('./cutils');

/**** 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 *
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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');
Expand Down Expand Up @@ -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();
}
Expand Down
Empty file removed lib/database/coap.db
Empty file.
60 changes: 36 additions & 24 deletions test/coap-shepherd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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&lt=86400&lwm2m=1.0.0&mac=BB:BB:BB',
rsinfo: {
address: '127.0.0.1',
port: '5687'
},
payload: '</a/0>,</a/1>,</b/0>,</b/1>',
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',
Expand Down
6 changes: 6 additions & 0 deletions test/coapdb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Empty file removed test/database_test/coap.db
Empty file.

0 comments on commit 5451b2e

Please sign in to comment.