initial commit
This commit is contained in:
commit
4b60ece582
327 changed files with 28286 additions and 0 deletions
7
libjs/wDispatcher/CMakeLists.txt
Normal file
7
libjs/wDispatcher/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
cmake_minimum_required(VERSION 2.8.12)
|
||||
|
||||
configure_file(dispatcher.js dispatcher.js)
|
||||
configure_file(defaulthandler.js defaulthandler.js)
|
||||
configure_file(handler.js handler.js)
|
||||
configure_file(logger.js logger.js)
|
||||
configure_file(parentreporter.js parentreporter.js)
|
35
libjs/wDispatcher/defaulthandler.js
Normal file
35
libjs/wDispatcher/defaulthandler.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
"use strict";
|
||||
var Class = require("../utils/class");
|
||||
var id = 0;
|
||||
|
||||
var DefaultHandler = Class.inherit({
|
||||
"className": "DefaultHandler",
|
||||
"constructor": function() {
|
||||
Class.fn.constructor.call(this);
|
||||
|
||||
this._id = id++;
|
||||
},
|
||||
"==": function(other) {
|
||||
if (!(other instanceof DefaultHandler)) {
|
||||
throw new Error("Can compare only DefaultHandler with DefaultHandler");
|
||||
}
|
||||
return this._id === other._id;
|
||||
},
|
||||
">": function(other) {
|
||||
if (!(other instanceof DefaultHandler)) {
|
||||
throw new Error("Can compare only DefaultHandler with DefaultHandler");
|
||||
}
|
||||
return this._id > other._id;
|
||||
},
|
||||
"<": function(other) {
|
||||
if (!(other instanceof DefaultHandler)) {
|
||||
throw new Error("Can compare only DefaultHandler with DefaultHandler");
|
||||
}
|
||||
return this._id < other._id;
|
||||
},
|
||||
"call": function(event) {
|
||||
throw new Error("Attempt to call pure abstract default handler");
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = DefaultHandler;
|
91
libjs/wDispatcher/dispatcher.js
Normal file
91
libjs/wDispatcher/dispatcher.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
"use strict";
|
||||
var Class = require("../utils/class");
|
||||
var Handler = require("./handler");
|
||||
var DefaultHandler = require("./defaulthandler");
|
||||
var Address = require("../wType/address");
|
||||
var AbstractMap = require("../wContainer/abstractmap");
|
||||
var AbstractOrder = require("../wContainer/abstractorder");
|
||||
|
||||
var HandlerOrder = AbstractOrder.template(Handler);
|
||||
var DefaultHandlerOrder = AbstractOrder.template(DefaultHandler);
|
||||
var HandlerMap = AbstractMap.template(Address, HandlerOrder);
|
||||
|
||||
var Dispatcher = Class.inherit({
|
||||
"className": "Dispatcher",
|
||||
"constructor": function() {
|
||||
Class.fn.constructor.call(this);
|
||||
|
||||
this._handlers = new HandlerMap();
|
||||
this._defautHandlers = new DefaultHandlerOrder(false);
|
||||
},
|
||||
"destructor": function() {
|
||||
this._handlers.destructor();
|
||||
|
||||
Class.fn.destructor.call(this);
|
||||
},
|
||||
"registerHandler": function(handler) {
|
||||
var itr = this._handlers.find(handler.address);
|
||||
var order;
|
||||
|
||||
|
||||
if (itr["=="](this._handlers.end())) {
|
||||
order = new HandlerOrder(false);
|
||||
this._handlers.insert(handler.address.clone(), order);
|
||||
} else {
|
||||
order = itr["*"]();
|
||||
}
|
||||
|
||||
order.push_back(handler);
|
||||
},
|
||||
"unregisterHandler": function(handler) {
|
||||
var itr = this._handlers.find(handler.address);
|
||||
|
||||
if (!itr["=="](this._handlers.end())) {
|
||||
|
||||
var ord = itr["*"]().second;
|
||||
ord.erase(handler);
|
||||
|
||||
if (ord.size() === 0) {
|
||||
this._handlers.erase(itr);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Error("Can't unregister hander");
|
||||
}
|
||||
},
|
||||
"registerDefaultHandler": function(dh) {
|
||||
this._defautHandlers.push_back(dh);
|
||||
},
|
||||
"unregisterDefaultHandler": function(dh) {
|
||||
this._defautHandlers.erase(dh);
|
||||
},
|
||||
"pass": function(event) {
|
||||
var itr = this._handlers.find(event.getDestination());
|
||||
|
||||
if (!itr["=="](this._handlers.end())) {
|
||||
var ord = itr["*"]().second;
|
||||
|
||||
var o_beg = ord.begin();
|
||||
var o_end = ord.end();
|
||||
var hands = [];
|
||||
|
||||
for (; !o_beg["=="](o_end); o_beg["++"]()) {
|
||||
hands.push(o_beg["*"]());
|
||||
}
|
||||
|
||||
for (var i = 0; i < hands.length; ++i) {
|
||||
hands[i].pass(event)
|
||||
}
|
||||
} else {
|
||||
var dhitr = this._defautHandlers.begin();
|
||||
var dhend = this._defautHandlers.end();
|
||||
for (; !dhitr["=="](dhend); dhitr["++"]()) {
|
||||
if (dhitr["*"]().call(event)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Dispatcher;
|
44
libjs/wDispatcher/handler.js
Normal file
44
libjs/wDispatcher/handler.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
"use strict";
|
||||
var Class = require("../utils/class");
|
||||
var id = 0;
|
||||
|
||||
var Handler = Class.inherit({
|
||||
"className": "Handler",
|
||||
"constructor": function(address, instance, method) {
|
||||
Class.fn.constructor.call(this);
|
||||
|
||||
this._id = id++;
|
||||
|
||||
this.address = address;
|
||||
this._ctx = instance;
|
||||
this._mth = method;
|
||||
},
|
||||
"destructor": function() {
|
||||
this.address.destructor();
|
||||
|
||||
Class.fn.destructor.call(this);
|
||||
},
|
||||
"pass": function(event) {
|
||||
this._mth.call(this._ctx, event);
|
||||
},
|
||||
"==": function(other) {
|
||||
if (!(other instanceof Handler)) {
|
||||
throw new Error("Can compare only Handler with Handler");
|
||||
}
|
||||
return this._id === other._id;
|
||||
},
|
||||
">": function(other) {
|
||||
if (!(other instanceof Handler)) {
|
||||
throw new Error("Can compare only Handler with Handler");
|
||||
}
|
||||
return this._id > other._id;
|
||||
},
|
||||
"<": function(other) {
|
||||
if (!(other instanceof Handler)) {
|
||||
throw new Error("Can compare only Handler with Handler");
|
||||
}
|
||||
return this._id < other._id;
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Handler;
|
18
libjs/wDispatcher/logger.js
Normal file
18
libjs/wDispatcher/logger.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
"use strict";
|
||||
var DefaultHandler = require("./defaulthandler");
|
||||
|
||||
var Logger = DefaultHandler.inherit({
|
||||
"className": "Logger",
|
||||
"constructor": function() {
|
||||
DefaultHandler.fn.constructor.call(this);
|
||||
},
|
||||
"call": function(event) {
|
||||
console.log("Event went to default handler");
|
||||
console.log("Destination: " + event.getDestination().toString());
|
||||
console.log("Data: " + event.getData().toString());
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Logger;
|
55
libjs/wDispatcher/parentreporter.js
Normal file
55
libjs/wDispatcher/parentreporter.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
"use strict";
|
||||
|
||||
var DefaultHandler = require("./defaulthandler");
|
||||
var Handler = require("./handler");
|
||||
|
||||
var Address = require("../wType/address");
|
||||
var AbstractMap = require("../wContainer/abstractmap");
|
||||
|
||||
var HandlersMap = AbstractMap.template(Address, Handler);
|
||||
|
||||
var ParentReporter = DefaultHandler.inherit({
|
||||
"className": "ParentReporter",
|
||||
"constructor": function() {
|
||||
DefaultHandler.fn.constructor.call(this);
|
||||
|
||||
this._handlers = new HandlersMap(false);
|
||||
},
|
||||
"destructor": function() {
|
||||
this._handlers.destructor();
|
||||
|
||||
DefaultHandler.fn.destructor.call(this);
|
||||
},
|
||||
"call": function(ev) {
|
||||
var addr = ev.getDestination();
|
||||
var result = [];
|
||||
|
||||
var itr = this._handlers.begin();
|
||||
var end = this._handlers.end();
|
||||
for (; !itr["=="](end); itr["++"]()) {
|
||||
var pair = itr["*"]();
|
||||
if (addr.begins(pair.first)) {
|
||||
result[pair.first.size()] = pair.second; //it's a dirty javascript trick
|
||||
} //I need the longest of matching, and that's how I'm gonna get it
|
||||
}
|
||||
if (result.length) {
|
||||
result[result.length - 1].pass(ev);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
"registerParent": function(parentAddr, handler) {
|
||||
this._handlers.insert(parentAddr, handler);
|
||||
},
|
||||
"unregisterParent": function(parentAddr) {
|
||||
var itr = this._handlers.find(parentAddr);
|
||||
if (!itr["=="](this._handlers.end())) {
|
||||
this._handlers.erase(itr);
|
||||
} else {
|
||||
throw new Error("An attempt to unregister unregistered parent in ParentReporter");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = ParentReporter;
|
Loading…
Add table
Add a link
Reference in a new issue