Skip to content

Commit

Permalink
Merge pull request #18 from mophy/refactoring-storage-layer
Browse files Browse the repository at this point in the history
refactor a customizable storage layer
  • Loading branch information
PeterEB authored Mar 14, 2019
2 parents fade34a + f86f941 commit 7b58e85
Show file tree
Hide file tree
Showing 13 changed files with 1,290 additions and 1,226 deletions.
77 changes: 37 additions & 40 deletions lib/coap-shepherd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

var Q = require('q'),
fs = require('fs'),
path = require('path'),
util = require('util'),
EventEmitter = require('events').EventEmitter,
Readable = require('stream').Readable,
Expand All @@ -11,11 +9,10 @@ var Q = require('q'),
var proving = require('proving'),
_ = require('busyman'),
coap = require('coap'),
debug = require('debug')('coap-shepherd'),
logReq = require('debug')('coap-shepherd:request');
debug = require('debug')('coap-shepherd');

var CoapNode = require('./components/coap-node'),
Coapdb = require('./components/coapdb'),
var StorageInterface = require('./components/storage-interface'),
NedbStorage = require('./components/nedb-storage'),
cutils = require('./components/cutils'),
CNST = require('./components/constants'),
defaultConfig = require('./config'),
Expand All @@ -25,17 +22,11 @@ var CoapNode = require('./components/coap-node'),
var RSP = CNST.RSP;

function CoapShepherd(config) {
if (undefined !== config)
proving.object(config, 'config should be an object if given.');

EventEmitter.call(this);

this.clientIdCount = 1;
this._config = config ? Object.assign({}, defaultConfig, config) : defaultConfig;
this._config.ip = this._config.ip || '';
this._config.port = this._config.port || 5683;
this._config.reqTimeout = this._config.reqTimeout || 60;
this._config.hbTimeout = this._config.hbTimeout || 60;

this._setConfig(config);

this._net = {
intf: '',
Expand All @@ -55,14 +46,6 @@ function CoapShepherd(config) {
this._permitJoinTime = 0;
this._permitJoinTimer = null;

try {
fs.statSync(this._config.defaultDbFolder);
} catch (e) {
fs.mkdirSync(this._config.defaultDbFolder);
}

this._coapdb = new Coapdb(this._config.defaultDbPath);

coap.updateTiming({
maxLatency: (this._config.reqTimeout - 47) / 2
});
Expand All @@ -77,8 +60,6 @@ function CoapShepherd(config) {

util.inherits(CoapShepherd, EventEmitter);

var coapShepherd = new CoapShepherd();

CoapShepherd.prototype.start = function (callback) {
var deferred = Q.defer(),
shepherd = this;
Expand Down Expand Up @@ -134,23 +115,15 @@ CoapShepherd.prototype.reset = function (mode, callback) {

mode = !!mode;

if (mode === true) {
if (shepherd._coapdb) {
shepherd._coapdb.db.remove({}, { multi: true }, function (err, num) {
shepherd._coapdb.db.loadDatabase(function (err) {
if (!err)
debug('Database cleared.');
else
debug(err);
});
this.stop().then(function () {
if (mode === true) {
return shepherd._storage.reset().then(function () {
debug('Database cleared.');
return shepherd.start();
});
} else {
shepherd._coapdb = new Coapdb(shepherd._config.defaultDbPath);
}
}

this.stop().then(function () {
return shepherd.start();
else
return shepherd.start();
}).done(deferred.resolve, deferred.reject);

return deferred.promise.nodeify(callback);
Expand Down Expand Up @@ -337,7 +310,7 @@ CoapShepherd.prototype.remove = function (clientName, callback) {
mac = cnode.mac;
cnode._setStatus('offline');
cnode.lifeCheck(false);
cnode.dbRemove().done(function () {
this._storage.remove(cnode).done(function () {
cnode._registered = false;
cnode.so = null;
cnode._cancelAllObservers();
Expand Down Expand Up @@ -391,6 +364,28 @@ CoapShepherd.prototype._fire = function (msg, data) {
});
};

CoapShepherd.prototype._setConfig = function (config) {
if (undefined !== config)
proving.object(config, 'config should be an object if given.');

this._config = config ? Object.assign({}, defaultConfig, config) : defaultConfig;
this._config.ip = this._config.ip || '';
this._config.port = this._config.port || 5683;
this._config.reqTimeout = this._config.reqTimeout || 60;
this._config.hbTimeout = this._config.hbTimeout || 60;

if ((this._config.storage !== undefined) && (this._config.storage !== null) && !(this._config.storage instanceof StorageInterface))
throw new TypeError('config.storage should be an StorageInterface if given.');
if (this._config.storage)
this._storage = this._config.storage;
else
this._storage = this._createDefaultStorage(this._config.defaultDbPath);
};

CoapShepherd.prototype._createDefaultStorage = function (dbPath) {
return new NedbStorage(dbPath);
};

/*********************************************************
* Private function *
*********************************************************/
Expand Down Expand Up @@ -457,4 +452,6 @@ function hbCheck (shepherd, enabled) {
}
}

var coapShepherd = new CoapShepherd();

module.exports = coapShepherd;
Loading

0 comments on commit 7b58e85

Please sign in to comment.