nmos-ledger
Version:
NMOS discovery and registration APIs
227 lines (185 loc) • 7.58 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: api/NodeRAMStore.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: api/NodeRAMStore.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/* Copyright 2015 Christine S. MacNeill
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by appli cable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var immutable = require('seamless-immutable');
var NodeStore = require('./NodeStore');
var statusError = NodeStore.prototype.statusError;
// Check that the skip parameter is withing range, or set defaults
function checkSkip (skip, keys) {
if (skip && typeof skip === 'string') skip = +skip;
if (!skip || Number(skip) !== skip || skip % 1 !== skip ||
skip < 0)
skip = 0;
if (skip > keys.length) skip = keys.length;
return skip;
}
// Check that the limit parameter is withing range, or set defaults
function checkLimit (limit, keys) {
if (limit && typeof limit === 'string') limit = +limit;
if (!limit || Number(limit) !== limit || limit % 1 !== limit ||
limit > keys.length)
limit = keys.length;
if (limit < 0) limit = 0;
return limit;
}
// Generic get collection methods that returns an ordered sequence of items
function getCollection(items, skip, limit, cb, argsLength) {
if (argsLength === 1) {
cb = skip;
skip = null;
limit = null;
}
if (argsLength !== 3) {
cb(statusError(400, "Both skip and limit parameters must be provided."));
return;
}
var sortedKeys = Object.keys(items);
skip = checkSkip(skip, sortedKeys);
limit = checkLimit(limit, sortedKeys);
if (sortedKeys.length === 0 || limit === 0 || skip >= sortedKeys.length) {
cb(null, [], sortedKeys.length, 1, 1, 0);
return;
}
var pages = Math.ceil(sortedKeys.length / limit);
var pageOf = Math.ceil(skip / limit) + 1;
var itemArray = new Array();
for ( var x = skip ; x < Math.max(skip + limit, sortedKeys.length) ; x++ ) {
deviceArray.push(devices[sortedKeys[x]]);
}
cb(null, deviceArray, sortedKeys.length, pageOf, pages, deviceArray.length);
}
function getItem(items, id, cb, argsLength, name) {
if (argsLength !== 2) {
cb(statusError(400, "Identifier and callback function must be provided."));
} else if (!id || typeof id !== 'string'){
cb(statusError(400, "Identifier must be a string value."));
} else if (id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/) == null) {
cb(statusError(400, "Identifier must be a valid UUID."));
} else {
var item = items[id];
if (item) cb(null, device);
else cb(statusError(404, "A " + name + " with identifier ''" + id +
"' could not be found."));
}
}
/**
* In RAM store representing the state of a [node]{@link Node}. Immutable value.
* @constructor
* @implements {NodeStore}
* @param {Node} self
*/
function NodeRAMStore(self) {
this.self = self;
/**
* Map of devices available at this [node]{@link Node}. Keys are UUIDs.
* @type {Object.<string, Device>}
* @readonly
*/
this.devices = {};
/**
* Map of sources available at this [node]{@link Node}. Keys are UUIDs.
* @type {Object.<string, Source>}
* @readonly
*/
this.sources = {};
/**
* Map of flows associated with this [node]{@link Node}. Keys are UUIDs.
* @type {Object.<string, Flow>}
* @readonly
*/
this.flows = {};
/**
* Map of senders associated with this [node]{@link Node}. Keys are UUIDs.
* @type {Object.<string, Sender>}
* @readonly
*/
this.senders = {};
/**
* Map of receivers associated with this [node]{@link Node}. Keys are UUIDs.
* @type {Object.<string, Receiver>}
* @readonly
*/
this.receivers = {};
return immutable(this, { prototype : NodeState.prototype });
}
NodeRAMStore.prototype.getSelf = function (cb) {
cb(null, self);
}
NodeRAMStore.prototype.getDevices = function (skip, limit, cb) {
getCollection(this.devices, skip, limit, cb, arguments.length);
}
NodeRAMStore.prototype.getDevice = function (id, cb) {
getItem(this.deivces, id, cb, argsLength, 'device');
}
NodeRAMStore.prototype.getSources = function (skip, limit, cb) {
getCollection(this.sources, skip, limit, cb, arguments.length);
}
NodeRAMStore.prototype.getSource = function (id, cb) {
getItem(this.sources, id, cb, arguments.length, 'source');
}
NodeRAMStore.prototype.getSenders = function (skip, limit, cb) {
getCollection(this.senders, skip, limit, cb, arguments.length);
}
NodeRAMStore.prototype.getSender = function (id, cb) {
getItem(this.senders, id, cb, arguments.length, 'sender');
}
NodeRAMStore.prototype.getReceivers = function (skip, limit, cb) {
getCollection(this.receivers, skip, limit, cb, arguments.length);
}
NodeRAMStore.prototype.getReceiver = function (id, cb) {
getItem(this.receivers, id, cb, arguments.length, 'receiver');
}
NodeRAMStore.prototype.getFlows = function (skip, limit, cb) {
getCollection(this.flows, skip, limit, cb, arguments.length);
}
NodeRAMStore.prototype.getFlow = function (id, cb) {
getItem(this.flows, id, cb, arguments.length, 'flow');
}
NodeState.prototype.addDevice = function (device) {
return this.merge({
devices : this.devices.concat([device]),
self : this.self.merge({ services : this.self.services.concat([device.id]) })
});
}
module.exports = NodeState;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Device.html">Device</a></li><li><a href="Flow.html">Flow</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeAPI.html">NodeAPI</a></li><li><a href="NodeRAMStore.html">NodeRAMStore</a></li><li><a href="Versionned.html">Versionned</a></li></ul><h3>Interfaces</h3><ul><li><a href="NodeStore.html">NodeStore</a></li></ul><h3>Global</h3><ul><li><a href="global.html#capabilities">capabilities</a></li><li><a href="global.html#deviceTypes">deviceTypes</a></li><li><a href="global.html#formats">formats</a></li><li><a href="global.html#statusError">statusError</a></li><li><a href="global.html#transports">transports</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Dec 21 2015 17:19:49 GMT+0000 (GMT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>