2013-08-12 02:23:24 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Unification */
|
|
|
|
|
|
|
|
var __ = new Object(); /* wildcard marker */
|
|
|
|
__.__ = "__";
|
|
|
|
|
|
|
|
function unificationFailed() {
|
|
|
|
throw {unificationFailed: true};
|
|
|
|
}
|
|
|
|
|
|
|
|
function unify1(a, b) {
|
|
|
|
var i;
|
|
|
|
|
|
|
|
if (a === __) return b;
|
|
|
|
if (b === __) return a;
|
|
|
|
|
|
|
|
if (a === b) return a;
|
|
|
|
|
|
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
|
|
if (a.length !== b.length) unificationFailed();
|
2013-08-13 17:44:39 +00:00
|
|
|
var result = new Array(a.length);
|
2013-08-12 02:23:24 +00:00
|
|
|
for (i = 0; i < a.length; i++) {
|
2013-08-13 17:44:39 +00:00
|
|
|
result[i] = unify1(a[i], b[i]);
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof a === "object" && typeof b === "object") {
|
|
|
|
/* TODO: consider other kinds of matching. I've chosen to
|
|
|
|
require any field mentioned by either side to be present in
|
|
|
|
both. Does that make sense? */
|
|
|
|
var result = ({});
|
2013-08-13 17:44:24 +00:00
|
|
|
for (i in a) { if (a.hasOwnProperty(i)) result[i] = true; }
|
|
|
|
for (i in b) { if (b.hasOwnProperty(i)) result[i] = true; }
|
2013-08-12 02:23:24 +00:00
|
|
|
for (i in result) {
|
|
|
|
if (result.hasOwnProperty(i)) {
|
|
|
|
result[i] = unify1(a[i], b[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
unificationFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
function unify(a, b) {
|
|
|
|
try {
|
2013-10-17 15:38:47 +00:00
|
|
|
// console.log("unify", JSON.stringify(a), JSON.stringify(b));
|
2013-08-12 02:23:24 +00:00
|
|
|
return {result: unify1(a, b)};
|
|
|
|
} catch (e) {
|
|
|
|
if (e.unificationFailed) return undefined;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function anyUnify(aa, bb) {
|
|
|
|
for (var i = 0; i < aa.length; i++) {
|
|
|
|
for (var j = 0; j < bb.length; j++) {
|
|
|
|
if (unify(aa[i], bb[j])) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-09-27 11:14:27 +00:00
|
|
|
/* Events and Actions */
|
2013-08-12 02:23:24 +00:00
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function Route(isSubscription, pattern, metaLevel, level) {
|
2013-08-12 12:51:45 +00:00
|
|
|
this.isSubscription = isSubscription;
|
2013-08-12 02:23:24 +00:00
|
|
|
this.pattern = pattern;
|
2013-09-27 11:14:27 +00:00
|
|
|
this.metaLevel = (metaLevel === undefined) ? 0 : metaLevel;
|
2013-08-12 12:51:45 +00:00
|
|
|
this.level = (level === undefined) ? 0 : level;
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
Route.prototype.drop = function () {
|
|
|
|
if (this.metaLevel === 0) { return null; }
|
|
|
|
return new Route(this.isSubscription, this.pattern, this.metaLevel - 1, this.level);
|
|
|
|
};
|
|
|
|
|
|
|
|
Route.prototype.lift = function () {
|
|
|
|
return new Route(this.isSubscription, this.pattern, this.metaLevel + 1, this.level);
|
|
|
|
};
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
Route.prototype.toJSON = function () {
|
|
|
|
return [this.isSubscription ? "sub" : "pub", this.pattern, this.metaLevel, this.level];
|
|
|
|
};
|
|
|
|
|
2013-10-31 12:10:53 +00:00
|
|
|
Route.fromJSON = function (j) {
|
|
|
|
switch (j[0]) {
|
|
|
|
case "sub": return new Route(true, j[1], j[2], j[3]);
|
|
|
|
case "pub": return new Route(false, j[1], j[2], j[3]);
|
|
|
|
default: throw { message: "Invalid JSON-encoded route: " + JSON.stringify(j) };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function sub(pattern, metaLevel, level) {
|
|
|
|
return new Route(true, pattern, metaLevel, level);
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function pub(pattern, metaLevel, level) {
|
|
|
|
return new Route(false, pattern, metaLevel, level);
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function spawn(behavior, initialRoutes) {
|
|
|
|
return { type: "spawn",
|
|
|
|
behavior: behavior,
|
|
|
|
initialRoutes: (initialRoutes === undefined) ? [] : initialRoutes };
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateRoutes(routes) {
|
|
|
|
return { type: "routes", routes: routes };
|
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function sendMessage(m, metaLevel, isFeedback) {
|
2013-10-30 15:50:05 +00:00
|
|
|
return { type: "message",
|
2013-09-27 11:14:27 +00:00
|
|
|
metaLevel: (metaLevel === undefined) ? 0 : metaLevel,
|
|
|
|
message: m,
|
|
|
|
isFeedback: (isFeedback === undefined) ? false : isFeedback };
|
|
|
|
}
|
|
|
|
|
2014-02-28 14:46:59 +00:00
|
|
|
function shutdownWorld() {
|
|
|
|
return { type: "shutdownWorld" };
|
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Metafunctions */
|
|
|
|
|
|
|
|
function dropRoutes(routes) {
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < routes.length; i++) {
|
|
|
|
var r = routes[i].drop();
|
|
|
|
if (r) { result.push(r); }
|
|
|
|
}
|
|
|
|
return result;
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
function liftRoutes(routes) {
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < routes.length; i++) {
|
|
|
|
result.push(routes[i].lift());
|
|
|
|
}
|
|
|
|
return result;
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
function intersectRoutes(rs1, rs2, ignoreLevels) {
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < rs1.length; i++) {
|
|
|
|
for (var j = 0; j < rs2.length; j++) {
|
|
|
|
var ri = rs1[i];
|
|
|
|
var rj = rs2[j];
|
|
|
|
if (ri.isSubscription === !rj.isSubscription
|
|
|
|
&& ri.metaLevel === rj.metaLevel
|
|
|
|
&& (ignoreLevels || (ri.level < rj.level)))
|
|
|
|
{
|
|
|
|
var u = unify(ri.pattern, rj.pattern);
|
|
|
|
if (u) {
|
|
|
|
var rk = new Route(ri.isSubscription, u.result, ri.metaLevel, ri.level);
|
|
|
|
result.push(rk);
|
2013-09-27 11:14:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 18:14:51 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterEvent(e, routes) {
|
|
|
|
switch (e.type) {
|
|
|
|
case "routes":
|
|
|
|
return updateRoutes(intersectRoutes(e.routes, routes));
|
2013-10-30 15:50:05 +00:00
|
|
|
case "message":
|
2013-09-27 11:14:27 +00:00
|
|
|
for (var i = 0; i < routes.length; i++) {
|
|
|
|
var r = routes[i];
|
|
|
|
if (e.metaLevel === r.metaLevel
|
|
|
|
&& e.isFeedback === !r.isSubscription
|
|
|
|
&& unify(e.message, r.pattern))
|
|
|
|
{
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
throw { message: "Event type " + e.type + " not filterable",
|
|
|
|
event: e };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Configurations */
|
|
|
|
|
2013-10-17 14:50:51 +00:00
|
|
|
function World(bootFn) {
|
2013-08-12 02:23:24 +00:00
|
|
|
this.nextPid = 0;
|
|
|
|
this.eventQueue = [];
|
|
|
|
this.processTable = {};
|
|
|
|
this.downwardRoutes = [];
|
2013-09-27 11:14:27 +00:00
|
|
|
this.processActions = [];
|
|
|
|
this.activePid = null;
|
2013-10-17 14:50:51 +00:00
|
|
|
this.stepperId = null;
|
2013-11-03 17:31:09 +00:00
|
|
|
this.asChild(-1, bootFn, true);
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
/* Class state / methods */
|
|
|
|
|
2013-10-18 08:58:08 +00:00
|
|
|
World.stack = [];
|
|
|
|
|
|
|
|
World.current = function () {
|
|
|
|
return World.stack[World.stack.length - 1];
|
|
|
|
};
|
2013-09-27 11:14:27 +00:00
|
|
|
|
|
|
|
World.send = function (m, metaLevel, isFeedback) {
|
2013-10-18 08:58:08 +00:00
|
|
|
World.current().enqueueAction(sendMessage(m, metaLevel, isFeedback));
|
2013-09-27 11:14:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
World.updateRoutes = function (routes) {
|
2013-10-18 08:58:08 +00:00
|
|
|
World.current().enqueueAction(updateRoutes(routes));
|
2013-09-27 11:14:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
World.spawn = function (behavior, initialRoutes) {
|
2013-10-18 08:58:08 +00:00
|
|
|
World.current().enqueueAction(spawn(behavior, initialRoutes));
|
|
|
|
};
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
World.exit = function (exn) {
|
|
|
|
World.current().killActive(exn);
|
|
|
|
};
|
|
|
|
|
2014-02-28 14:46:59 +00:00
|
|
|
World.shutdownWorld = function () {
|
|
|
|
World.current().enqueueAction(shutdownWorld());
|
|
|
|
};
|
|
|
|
|
2013-10-18 08:58:08 +00:00
|
|
|
World.withWorldStack = function (stack, f) {
|
|
|
|
var oldStack = World.stack;
|
|
|
|
World.stack = stack;
|
|
|
|
var result = null;
|
|
|
|
try {
|
|
|
|
result = f();
|
|
|
|
} catch (e) {
|
|
|
|
World.stack = oldStack;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
World.stack = oldStack;
|
|
|
|
return result;
|
2013-09-27 11:14:27 +00:00
|
|
|
};
|
|
|
|
|
2013-10-17 14:50:51 +00:00
|
|
|
World.wrap = function (f) {
|
2013-10-18 08:58:08 +00:00
|
|
|
var savedStack = World.stack.slice();
|
|
|
|
var savedPid = World.current().activePid;
|
|
|
|
return function () {
|
|
|
|
var actuals = arguments;
|
|
|
|
return World.withWorldStack(savedStack, function () {
|
|
|
|
var result = World.current().asChild(savedPid, function () {
|
|
|
|
return f.apply(null, actuals);
|
|
|
|
});
|
|
|
|
World.stack[0].startStepping();
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
};
|
2013-10-17 14:50:51 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
/* Instance methods */
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
World.prototype.killActive = function (exn) {
|
|
|
|
this.kill(this.activePid, exn);
|
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
World.prototype.enqueueAction = function (action) {
|
|
|
|
this.processActions.push([this.activePid, action]);
|
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.isQuiescent = function () {
|
|
|
|
return this.eventQueue.length === 0 && this.processActions.length === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.step = function () {
|
|
|
|
this.dispatchEvents();
|
|
|
|
this.performActions();
|
|
|
|
return this.stepChildren() || !this.isQuiescent();
|
|
|
|
};
|
|
|
|
|
2013-10-17 14:50:51 +00:00
|
|
|
World.prototype.startStepping = function () {
|
|
|
|
var self = this;
|
|
|
|
if (this.stepperId) return;
|
|
|
|
if (this.step()) {
|
|
|
|
this.stepperId = setTimeout(function () {
|
|
|
|
self.stepperId = null;
|
|
|
|
self.startStepping();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.stopStepping = function () {
|
|
|
|
if (this.stepperId) {
|
|
|
|
clearTimeout(this.stepperId);
|
|
|
|
this.stepperId = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-03 17:31:09 +00:00
|
|
|
World.prototype.asChild = function (pid, f, omitLivenessCheck) {
|
|
|
|
if (!(pid in this.processTable) && !omitLivenessCheck) {
|
|
|
|
console.warn("World.asChild eliding invocation of dead process", pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 08:58:08 +00:00
|
|
|
World.stack.push(this);
|
2013-09-27 11:14:27 +00:00
|
|
|
var result = null;
|
|
|
|
this.activePid = pid;
|
|
|
|
try {
|
|
|
|
result = f();
|
|
|
|
} catch (e) {
|
|
|
|
this.kill(pid, e);
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
2013-09-27 11:14:27 +00:00
|
|
|
this.activePid = null;
|
2013-10-18 08:58:08 +00:00
|
|
|
if (World.stack.pop() !== this) {
|
|
|
|
throw { message: "Internal error: World stack imbalance" };
|
|
|
|
}
|
2013-09-27 11:14:27 +00:00
|
|
|
return result;
|
2013-08-12 02:23:24 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
World.prototype.kill = function (pid, exn) {
|
2013-10-17 15:38:47 +00:00
|
|
|
if (exn && exn.stack) {
|
2013-10-22 18:14:51 +00:00
|
|
|
console.log("Process exited", pid, exn, exn.stack);
|
2013-10-17 15:38:47 +00:00
|
|
|
} else {
|
2013-10-22 18:14:51 +00:00
|
|
|
console.log("Process exited", pid, exn);
|
2013-10-17 15:38:47 +00:00
|
|
|
}
|
2013-11-03 20:56:12 +00:00
|
|
|
var p = this.processTable[pid];
|
|
|
|
if (p && p.behavior.trapexit) {
|
|
|
|
this.asChild(pid, function () { return p.behavior.trapexit(exn); });
|
|
|
|
}
|
2013-09-27 11:14:27 +00:00
|
|
|
delete this.processTable[pid];
|
|
|
|
this.issueRoutingUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.stepChildren = function () {
|
|
|
|
var someChildBusy = false;
|
|
|
|
for (var pid in this.processTable) {
|
|
|
|
var p = this.processTable[pid];
|
|
|
|
if (p.behavior.step /* exists, haven't called it yet */) {
|
2013-10-17 14:50:51 +00:00
|
|
|
var childBusy = this.asChild(pid, function () { return p.behavior.step() });
|
2013-09-27 11:14:27 +00:00
|
|
|
someChildBusy = someChildBusy || childBusy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return someChildBusy;
|
2013-08-12 02:23:24 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
World.prototype.performActions = function () {
|
|
|
|
var queue = this.processActions;
|
|
|
|
this.processActions = [];
|
2013-08-12 02:23:24 +00:00
|
|
|
var item;
|
|
|
|
while ((item = queue.shift())) {
|
|
|
|
this.performAction(item[0], item[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
World.prototype.dispatchEvents = function () {
|
|
|
|
var queue = this.eventQueue;
|
|
|
|
this.eventQueue = [];
|
|
|
|
var item;
|
|
|
|
while ((item = queue.shift())) {
|
|
|
|
this.dispatchEvent(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-12 02:23:24 +00:00
|
|
|
World.prototype.performAction = function (pid, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case "spawn":
|
2013-09-27 11:14:27 +00:00
|
|
|
var pid = this.nextPid++;
|
|
|
|
this.processTable[pid] = { routes: action.initialRoutes, behavior: action.behavior };
|
2013-09-27 14:36:51 +00:00
|
|
|
if (action.behavior.boot) { this.asChild(pid, function () { action.behavior.boot() }); }
|
2013-09-27 11:14:27 +00:00
|
|
|
this.issueRoutingUpdate();
|
2013-08-12 02:23:24 +00:00
|
|
|
break;
|
|
|
|
case "routes":
|
2013-10-18 08:58:08 +00:00
|
|
|
if (pid in this.processTable) {
|
|
|
|
// it may not be: this might be the routing update from a
|
|
|
|
// kill of the process
|
|
|
|
this.processTable[pid].routes = action.routes;
|
|
|
|
}
|
2013-09-27 11:14:27 +00:00
|
|
|
this.issueRoutingUpdate();
|
2013-08-12 02:23:24 +00:00
|
|
|
break;
|
2013-10-30 15:50:05 +00:00
|
|
|
case "message":
|
2013-09-27 11:14:27 +00:00
|
|
|
if (action.metaLevel === 0) {
|
|
|
|
this.eventQueue.push(action);
|
|
|
|
} else {
|
|
|
|
World.send(action.message, action.metaLevel - 1, action.isFeedback);
|
|
|
|
}
|
2013-08-12 02:23:24 +00:00
|
|
|
break;
|
2014-02-28 14:46:59 +00:00
|
|
|
case "shutdownWorld":
|
|
|
|
World.exit();
|
|
|
|
break;
|
2013-08-12 02:23:24 +00:00
|
|
|
default:
|
|
|
|
throw { message: "Action type " + action.type + " not understood",
|
|
|
|
action: action };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 11:14:27 +00:00
|
|
|
World.prototype.aggregateRoutes = function (base) {
|
|
|
|
var acc = base.slice();
|
|
|
|
for (var pid in this.processTable) {
|
|
|
|
var p = this.processTable[pid];
|
|
|
|
for (var i = 0; i < p.routes.length; i++) {
|
|
|
|
acc.push(p.routes[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-10-17 14:50:51 +00:00
|
|
|
return acc;
|
2013-08-12 02:23:24 +00:00
|
|
|
};
|
|
|
|
|
2013-10-26 17:39:30 +00:00
|
|
|
World.prototype.issueLocalRoutingUpdate = function () {
|
2013-09-27 11:14:27 +00:00
|
|
|
this.eventQueue.push(updateRoutes(this.aggregateRoutes(this.downwardRoutes)));
|
2013-10-26 17:39:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.issueRoutingUpdate = function () {
|
|
|
|
this.issueLocalRoutingUpdate();
|
2013-09-27 11:14:27 +00:00
|
|
|
World.updateRoutes(dropRoutes(this.aggregateRoutes([])));
|
2013-08-12 02:23:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.dispatchEvent = function (e) {
|
2013-09-27 11:14:27 +00:00
|
|
|
for (var pid in this.processTable) {
|
|
|
|
var p = this.processTable[pid];
|
|
|
|
var e1 = filterEvent(e, p.routes);
|
2013-10-17 15:38:47 +00:00
|
|
|
// console.log("filtering", e, p.routes, e1);
|
2013-09-27 11:14:27 +00:00
|
|
|
if (e1) { this.asChild(pid, function () { p.behavior.handleEvent(e1) }); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
World.prototype.handleEvent = function (e) {
|
2013-08-12 02:23:24 +00:00
|
|
|
switch (e.type) {
|
|
|
|
case "routes":
|
2013-09-27 11:14:27 +00:00
|
|
|
this.downwardRoutes = liftRoutes(e.routes);
|
2013-10-26 17:39:30 +00:00
|
|
|
this.issueLocalRoutingUpdate();
|
2013-08-12 02:23:24 +00:00
|
|
|
break;
|
2013-10-30 15:50:05 +00:00
|
|
|
case "message":
|
2013-09-27 11:14:27 +00:00
|
|
|
this.eventQueue.push(sendMessage(e.message, e.metaLevel + 1, e.isFeedback));
|
2013-08-12 02:23:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-09-27 11:14:27 +00:00
|
|
|
throw { message: "Event type " + e.type + " not understood",
|
|
|
|
event: e };
|
2013-08-12 02:23:24 +00:00
|
|
|
}
|
|
|
|
};
|
2013-09-27 14:36:51 +00:00
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Utilities: detecting presence/absence events via routing events */
|
|
|
|
|
|
|
|
function PresenceDetector(initialRoutes) {
|
|
|
|
this.state = this._digestRoutes(initialRoutes === undefined ? [] : initialRoutes);
|
|
|
|
}
|
|
|
|
|
|
|
|
PresenceDetector.prototype._digestRoutes = function (routes) {
|
|
|
|
var newState = {};
|
|
|
|
for (var i = 0; i < routes.length; i++) {
|
|
|
|
newState[JSON.stringify(routes[i].toJSON())] = routes[i];
|
|
|
|
}
|
|
|
|
return newState;
|
|
|
|
};
|
|
|
|
|
2013-10-30 13:13:03 +00:00
|
|
|
PresenceDetector.prototype.getRouteList = function () {
|
|
|
|
var rs = [];
|
|
|
|
for (var k in this.state) { rs.push(this.state[k]); }
|
|
|
|
return rs;
|
|
|
|
};
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
PresenceDetector.prototype.handleRoutes = function (routes) {
|
|
|
|
var added = [];
|
|
|
|
var removed = [];
|
|
|
|
var newState = this._digestRoutes(routes);
|
|
|
|
for (var k in newState) {
|
|
|
|
if (!(k in this.state)) {
|
|
|
|
added.push(newState[k]);
|
|
|
|
} else {
|
|
|
|
delete this.state[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var k in this.state) {
|
|
|
|
removed.push(this.state[k]);
|
|
|
|
}
|
|
|
|
this.state = newState;
|
|
|
|
return { added: added, removed: removed };
|
|
|
|
};
|
|
|
|
|
|
|
|
PresenceDetector.prototype.presenceExistsFor = function (probeRoute) {
|
|
|
|
for (var k in this.state) {
|
|
|
|
var existingRoute = this.state[k];
|
|
|
|
if (probeRoute.isSubscription === !existingRoute.isSubscription
|
|
|
|
&& probeRoute.metaLevel === existingRoute.metaLevel
|
2013-10-28 14:02:39 +00:00
|
|
|
&& probeRoute.level === existingRoute.level
|
2013-10-22 18:14:51 +00:00
|
|
|
&& unify(probeRoute.pattern, existingRoute.pattern))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Utilities: matching demand for some service */
|
|
|
|
|
2013-10-30 13:13:03 +00:00
|
|
|
function DemandMatcher(pattern, metaLevel, options) {
|
2014-02-28 14:47:34 +00:00
|
|
|
options = $.extend({
|
2013-10-30 13:13:03 +00:00
|
|
|
demandLevel: 0,
|
|
|
|
supplyLevel: 0,
|
|
|
|
demandSideIsSubscription: true
|
2014-02-28 14:47:34 +00:00
|
|
|
}, options);
|
2013-10-30 13:13:03 +00:00
|
|
|
this.pattern = pattern;
|
|
|
|
this.metaLevel = metaLevel;
|
|
|
|
this.demandLevel = options.demandLevel;
|
|
|
|
this.supplyLevel = options.supplyLevel;
|
|
|
|
this.demandSideIsSubscription = options.demandSideIsSubscription;
|
|
|
|
this.onDemandIncrease = function (r) {
|
|
|
|
console.error("Unhandled increase in demand for route", r);
|
|
|
|
};
|
|
|
|
this.onSupplyDecrease = function (r) {
|
|
|
|
console.error("Unhandled decrease in supply for route", r);
|
2013-10-22 18:14:51 +00:00
|
|
|
};
|
|
|
|
this.state = new PresenceDetector();
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:13:03 +00:00
|
|
|
DemandMatcher.prototype.boot = function () {
|
|
|
|
World.updateRoutes([this.computeDetector(true),
|
|
|
|
this.computeDetector(false)]);
|
|
|
|
};
|
|
|
|
|
|
|
|
DemandMatcher.prototype.handleEvent = function (e) {
|
|
|
|
if (e.type === "routes") {
|
|
|
|
this.handleRoutes(e.routes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DemandMatcher.prototype.computeDetector = function (demandSide) {
|
|
|
|
var maxLevel = (this.demandLevel > this.supplyLevel ? this.demandLevel : this.supplyLevel);
|
|
|
|
return new Route(this.demandSideIsSubscription ? !demandSide : demandSide,
|
|
|
|
this.pattern,
|
|
|
|
this.metaLevel,
|
|
|
|
maxLevel + 1);
|
|
|
|
};
|
|
|
|
|
2013-10-22 18:14:51 +00:00
|
|
|
DemandMatcher.prototype.handleRoutes = function (routes) {
|
|
|
|
var changes = this.state.handleRoutes(routes);
|
2013-10-30 13:13:03 +00:00
|
|
|
this.incorporateChanges(true, changes.added);
|
|
|
|
this.incorporateChanges(false, changes.removed);
|
|
|
|
};
|
|
|
|
|
|
|
|
DemandMatcher.prototype.incorporateChanges = function (isArrivals, routeList) {
|
|
|
|
var relevantChangeDetector = this.computeDetector(isArrivals);
|
|
|
|
var expectedChangeLevel = isArrivals ? this.demandLevel : this.supplyLevel;
|
|
|
|
var expectedPeerLevel = isArrivals ? this.supplyLevel : this.demandLevel;
|
|
|
|
for (var i = 0; i < routeList.length; i++) {
|
|
|
|
var changed = routeList[i];
|
|
|
|
if (changed.level != expectedChangeLevel) continue;
|
|
|
|
var relevantChangedN = intersectRoutes([changed], [relevantChangeDetector]);
|
|
|
|
if (relevantChangedN.length === 0) continue;
|
|
|
|
var relevantChanged = relevantChangedN[0]; /* there can be only one */
|
|
|
|
var peerDetector = new Route(relevantChanged.isSubscription,
|
|
|
|
relevantChanged.pattern,
|
|
|
|
relevantChanged.metaLevel,
|
|
|
|
expectedPeerLevel + 1);
|
|
|
|
var peerRoutes = intersectRoutes(this.state.getRouteList(), [peerDetector]);
|
|
|
|
var peerExists = false;
|
|
|
|
for (var j = 0; j < peerRoutes.length; j++) {
|
|
|
|
if (peerRoutes[j].level == expectedPeerLevel) {
|
|
|
|
peerExists = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-22 18:14:51 +00:00
|
|
|
}
|
2013-10-30 13:13:03 +00:00
|
|
|
if (isArrivals && !peerExists) { this.onDemandIncrease(relevantChanged); }
|
|
|
|
if (!isArrivals && peerExists) { this.onSupplyDecrease(relevantChanged); }
|
2013-10-22 18:14:51 +00:00
|
|
|
}
|
2013-10-30 13:13:03 +00:00
|
|
|
};
|
2013-10-22 18:14:51 +00:00
|
|
|
|
2013-10-31 12:10:53 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Utilities: deduplicator */
|
|
|
|
|
|
|
|
function Deduplicator(ttl_ms) {
|
|
|
|
this.ttl_ms = ttl_ms || 10000;
|
|
|
|
this.queue = [];
|
|
|
|
this.map = {};
|
|
|
|
this.timerId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Deduplicator.prototype.accept = function (m) {
|
|
|
|
var s = JSON.stringify(m);
|
|
|
|
if (s in this.map) return false;
|
|
|
|
var entry = [(+new Date()) + this.ttl_ms, s, m];
|
|
|
|
this.map[s] = entry;
|
|
|
|
this.queue.push(entry);
|
|
|
|
|
|
|
|
if (this.timerId === null) {
|
|
|
|
var self = this;
|
|
|
|
this.timerId = setInterval(function () { self.expireMessages(); },
|
|
|
|
this.ttl_ms > 1000 ? 1000 : this.ttl_ms);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Deduplicator.prototype.expireMessages = function () {
|
|
|
|
var now = +new Date();
|
|
|
|
while (this.queue.length > 0 && this.queue[0][0] <= now) {
|
|
|
|
var entry = this.queue.shift();
|
|
|
|
delete this.map[entry[1]];
|
|
|
|
}
|
|
|
|
if (this.queue.length === 0) {
|
|
|
|
clearInterval(this.timerId);
|
|
|
|
this.timerId = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 14:36:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Ground interface */
|
|
|
|
|
2013-10-17 14:50:51 +00:00
|
|
|
function Ground(bootFn) {
|
|
|
|
var self = this;
|
|
|
|
this.stepperId = null;
|
2013-10-22 18:14:51 +00:00
|
|
|
this.state = new PresenceDetector();
|
2013-10-18 08:58:08 +00:00
|
|
|
World.withWorldStack([this], function () {
|
2013-10-17 14:50:51 +00:00
|
|
|
self.world = new World(bootFn);
|
2013-10-18 08:58:08 +00:00
|
|
|
});
|
2013-09-27 14:36:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:50:51 +00:00
|
|
|
Ground.prototype.step = function () {
|
|
|
|
var self = this;
|
2013-10-18 08:58:08 +00:00
|
|
|
return World.withWorldStack([this], function () {
|
2013-10-17 14:50:51 +00:00
|
|
|
return self.world.step();
|
2013-10-18 08:58:08 +00:00
|
|
|
});
|
2013-10-17 14:50:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ground.prototype.startStepping = World.prototype.startStepping;
|
|
|
|
Ground.prototype.stopStepping = World.prototype.stopStepping;
|
|
|
|
|
|
|
|
Ground.prototype.enqueueAction = function (action) {
|
|
|
|
if (action.type === 'routes') {
|
2013-10-22 18:14:51 +00:00
|
|
|
var added = this.state.handleRoutes(action.routes).added;
|
|
|
|
if (added.length > 0) {
|
|
|
|
console.error("You have subscribed to a nonexistent event source.", added);
|
2013-10-17 15:38:47 +00:00
|
|
|
}
|
2013-10-17 14:50:51 +00:00
|
|
|
} else {
|
|
|
|
console.error("You have sent a message into the outer void.", action);
|
|
|
|
}
|
|
|
|
};
|