Procházet zdrojové kódy

Created Eventual event manager

Luis Figueiredo před 8 roky
rodič
revize
aeac43e5b2

+ 0 - 1
bundles/core-config/index.js

@@ -1,6 +1,5 @@
 var log = require('hlogger').createLogger('config');
 
-
 var configActivator = {
 	start(context) {
 	},

binární
test/eventual/.index.js.swp


+ 6 - 4
bundles/core-installer/index.js

@@ -14,15 +14,16 @@ var installerActivator = {
 		// With config
 		this.deployPath = context.manager.config.deployPath; 
 		this.runPath = context.manager.config.runPath;
-
 		if(this.deployPath == undefined) {
 			log.warn("There is no deploy path configured");
-			context.toggleOn('installed');	
+			context.state("installed");
+			//context.emit('installed');
 			return;
 		}
 		if(this.runPath == undefined) {
 			log.warn("There is no bundlePath configured");
-			context.toggleOn('installed');	
+			this.installed = true;
+			context.state("installed");
 			return;
 		}
 
@@ -33,7 +34,8 @@ var installerActivator = {
 
 		var hitCounter = new HitCounter(mods.length, () => {
 			log.info("Emitting installed");
-			context.toggleOn('installed',context);	
+			this.installed = true;
+			context.state('installed');	
 		});
 
 		for(var v of mods) {

+ 15 - 14
bundles/core-loader/index.js

@@ -9,20 +9,21 @@ var loaderActivator = {
 	start(context) {
 		this.context = context;
 		this.manager = context.manager;
-
 		this.runPath = context.manager.config.runPath;
+
+		this.registerShell(context);
+
+
 		if(!this.runPath ) {
 			log.warn("There is no runPath configured in manager");
 			return;
 		}
-
-		// Check this out its wrong!?
 		context.on('core-installer:installed',() => {
-			this.loadBundles();	
+				this.loadBundles();	
 		});
-
-		this.registerShell(context);
-
+		//this.loadBundles();	
+		// Check this out its wrong!?
+	
 	},
 	loadBundles() {
 		var mods = fs.readdirSync(this.runPath);
@@ -41,8 +42,8 @@ var loaderActivator = {
 	},
 
 	registerShell(context) {
-		var prefix = context.prefix(".*:cmd"); // Any cmd sender
-		prefix.on('lb',(req,res) => {
+		//var prefix = context.prefix(".*:cmd"); // Any cmd sender
+		context.on('core-shell:cmd:lb',(req,res) => {
 			var tbl = [];
 			for(var k in context.manager.registry) {
 				var v = context.manager.registry[k];
@@ -79,32 +80,32 @@ var loaderActivator = {
 			}
 			return bcontext;
 		}
-		prefix.on('stop',(req,res) => {
+		context.on('core-shell:cmd:stop',(req,res) => {
 			var bcontext = commonGetBundle(req,res,1);
 			if(bcontext == null) { return; }
 			bcontext.stop();
 
 		});
-		prefix.on('start',(req,res) => {
+		context.on('core-shell:cmd:start',(req,res) => {
 			var bcontext = commonGetBundle(req,res,1);
 			if(bcontext == null) { return; }
 			bcontext.start();
 		});
 
-		prefix.on('reload',(req,res) => {
+		context.on('core-shell:cmd:reload',(req,res) => {
 			var bcontext = commonGetBundle(req,res,1);
 			if(bcontext == null) { return; }
 			var modulePath = bcontext.modulePath;
 			context.manager.unregister(bcontext);
 			context.manager.register({id:bcontext.id, name:bcontext.name, bundle:bcontext.modulePath});
 		});
-		prefix.on('unload',(req,res) => {
+		context.on('core-shell:cmd:unload',(req,res) => {
 			var bcontext = commonGetBundle(req,res,1);
 			if(bcontext == null) { return; }
 			context.manager.unregister(bcontext);
 		});
 		
-		prefix.on('load',(req,res) => {
+		context.on('core-shell:cmd:load',(req,res) => {
 			if(req.args.length < 1) {
 				res.write("Not enough parameters\n");
 				return;

+ 13 - 8
bundles/core-monitor/index.js

@@ -26,13 +26,19 @@ var monitor = {
 		});
 	},
 	registerCommands(context) {
+		context
+			.channel('core-shell:cmd')
+			.on('stat',(req,res,e) => {
+				this.showStat(res); // Should pass stdout
+			});
 		// listen from command from ourselves
-		context.on('.*:cmd:stat',(req,res,e) => {
-			this.showStat(res); // Should pass stdout
-		});
-		context.on('.*:cmd:gc',(req,res,e) => {
+		/*context.on('core-shell:cmd:stat',(req,res,e) => {
+				this.showStat(res); // Should pass stdout
+		}).on('core-shell:cmd:gc',(req,res,e) => {
 			gc();
-		});
+		})
+		context.on('core-shell:cmd:test',(req,res,e) => {
+		});/**/
 	},
 	showStat(res) {
 		var context = this.context;
@@ -43,13 +49,12 @@ var monitor = {
 				 +" Total: " + ntokb(obj.heapTotal)
 				 +" Used: " + ntokb(obj.heapUsed) +"\n");
 
-		res.write("Events: " + context.manager.events.listeners.length + "\n");
 		// Weird?
-		res.write("Modules registered: " + context.manager.stat.modulesRegistered
+		/*res.write("Modules registered: " + context.manager.stat.modulesRegistered
 						+" Modules UnRegistered: " + context.manager.stat.modulesUnRegistered
 						+" events emitted: " + context.manager.events.stat.emitted
 						+" events called: " + context.manager.events.stat.called
-						+" events tried: " + context.manager.events.stat.tried + "\n");
+						+" events tried: " + context.manager.events.stat.tried + "\n");*/
 	},
 	// Human readable stat
 	getStat() {

+ 4 - 1
bundles/core-shell/index.js

@@ -22,7 +22,10 @@ var shellActivator = {
 			var res = this.stdout;
 			context
 				.emit("cmd:" + cmd,req,res)
-				.done(()=> {
+				.done((evt)=> {
+					if(evt.count == 0) {
+						context.emit("cmd",req,res).done(() => { this.cli.prompt() });
+					}
 					this.cli.prompt();	
 				});
 		});

binární
test/eventual/lib/.eventual.js.swp


+ 18 - 0
doc/ideas.js

@@ -0,0 +1,18 @@
+// This JS doesn't do anything, just want to have syntax highlight on my thoughts
+//
+//
+
+
+
+commChannel with two points
+
+	obj1 <- channel -> obj2
+
+
+as obj2:  channel.send('hello');
+
+obj1:
+
+	// Seems like a 2 way event system
+	channel.on('hello');
+

+ 10 - 0
doc/performance.js

@@ -1,3 +1,13 @@
+
+
+
+
+
+Done,with 1000000 events, it was taking aroudn 200~400 ms
+
+for 1000000 events
+with new EventManager is taking 0.02 0.11ms altough we lost 
+ 
 Event manager - revamp
 
 	//Current status:

+ 15 - 0
doc/spec.md

@@ -1,5 +1,20 @@
+
+
+
 # Request response idea:
 
+
+### State changes
+	Get a state controller , on state change it will inform the listeners trough eventual
+
+context
+	.channel('state')
+	.on('core-installer:installed',() => {
+
+})
+
+
+
 Module gets loaded, and request 
 
 Whats the time?

+ 38 - 14
lib/bundle-context.js

@@ -56,8 +56,15 @@ class BundleContext /*extends XEventEmitter*/ {
 		var dummyFunc = () => {};
 		this._bundleContext.instanceStart = solveMethod(this.instance,"start") || dummyFunc;
 		this._bundleContext.instanceStop = solveMethod(this.instance,"stop") || dummyFunc;
-		
+
+		// New contextual events
 	}	
+	get events() {
+		if(this._events == undefined) {
+			this._events = this.manager.eventual.createContext();
+		}
+		return this._events;
+	}
 
 	get id() {
 		return this.info.id;
@@ -77,6 +84,7 @@ class BundleContext /*extends XEventEmitter*/ {
 	get version() {
 		return this.info.version;
 	}
+
 	start() {
 		if(this.info.state == 1) {
 			log.warn("Instance is turned on trying to turn off first");
@@ -88,7 +96,7 @@ class BundleContext /*extends XEventEmitter*/ {
 			} catch(e) {
 				log.error("Bundle start error: " + this.name + "\n",e);
 			}
-			this.manager.events.toggleOn(this.name,this.instance);
+			//this.manager.events.toggleOn(this.name,this.instance);
 			this.info.state = 1;
 			this.info.startDate = new Date();
 		//});
@@ -96,7 +104,7 @@ class BundleContext /*extends XEventEmitter*/ {
 
 	stop() {
 		if(this.info.state == 0) return;
-		this.toggleOff("start");
+		//this.toggleOff("start");
 		this.emit("stop");
 		try {
 			this._bundleContext.instanceStop(this);
@@ -106,34 +114,50 @@ class BundleContext /*extends XEventEmitter*/ {
 
 		this.clearIntervals();
 		this.clearTimeouts();
+		this.events.destroy();
 
-		this.manager.events.removeListener(this);
+		//this.manager.events.removeListener(this);
 		this.info.state = 0;
 	}
 
 	/* Event wrappers */
-	removeListener(ename) {
+	/*removeListener(ename) {
 		this.manager.events.removeListener(this,ename);
+	}*/
+	
+	emit(name,...args) {
+		return this.events.broadcast( this.name + ":" +name,...args);
 	}
-	emit(ename,...args) {
-		return this.manager.events.emit(this.name+":"+ename,...args);
+	state(name,...args) {
+		this.events.state( this.name + ":"+ name,...args);
+		return this;
 	}
-	toggleOn(ename,arg) {
+	/*emit(ename,...args) {
+		return this.manager.events.emit(this.name+":"+ename,...args);
+	}*/
+	/*toggleOn(ename,arg) {
 		this.manager.events.toggleOn(this.name+":"+ename,arg);
 	}
 	toggleOff(ename) {
 		this.manager.events.toggleOff(this.name+":"+ename);
+	}*/
+	channel(name) {
+		return this.events.channel(name);
+	}
+	on(name,cb) {
+		// Only create events if does not exists, maybe with a getter
+		this.events.on(name,cb);
+		return this;
 	}
-	on(...args) {
+
+	/*on(...args) {
 		var cb = args.pop();
 		var evtName = args.join(":");
 		this.manager.events.on(this,evtName ,cb);
 		return this;
-	}
-	after(...args) {
-		var cb = args.pop();
-		var evtName = args.join(":")
-		this.manager.events.after(this,evtName ,cb);
+	}*/
+	after(name,cb) { // Priorities
+		this.events.on(name,cb,1000);
 		return this;
 	}
 	// Channel

+ 9 - 1
lib/bundle-manager.js

@@ -4,6 +4,10 @@ var path 						= require('path'),
 		clearRequire 		= require('./utils/clear-require'),
 		readPackageJson = require('./utils/read-packagejson');
 
+
+/* NEW */
+var Eventual = require('./eventual');
+
 var	BundleContext = require('./bundle-context');
 var log = require('hlogger').createLogger('bundle-manager');
 
@@ -24,8 +28,11 @@ class BundleManager {
 	constructor(opts) {
 		// Control events	
 		this.config = opts || {};
+
+		this.eventual = new Eventual();
+		
+		//this.events = new XEventEmitter();   // Force for now
 		
-		this.events = new XEventEmitter();
 		// Global channel
 		this.globalEvents = new XEventEmitter();
 
@@ -187,6 +194,7 @@ class BundleManager {
 		return fretList;
 	}
 
+
 	with(args,cb) {
 		var mods = args;
 		/*if(typeof(args[args.length-1]) != "function") {

+ 171 - 0
lib/eventual/eventual-channel.js

@@ -0,0 +1,171 @@
+/** Concept each channel can have child channels ??**/
+const chs = ':'; // Channel separator
+
+class XEvent {
+	constructor(donecb,args) {
+		this.count = 0;
+		this.dowait = 0;
+		
+		this.done= donecb; // Callback
+		this.errors = [];
+		this.params = args;
+	}
+	wait() {
+		this.dowait = 1;
+	}
+}
+
+class EventualChannel {
+
+	constructor(eventual, name,parent) {
+		this.eventual = eventual;
+		this.name = name;
+		this.parent = parent; //?? we need
+		
+		this.childs = {}
+		this.listeners = [];
+	}
+
+	// createChannel based on name
+	//
+	fullName() {
+		var name = this.name;
+		for(var cur = this;cur = cur.parent;cur.parent) {
+			if(this.parent.parent) { // if not root
+				name = this.parent.name +":" + name;
+			}
+		}
+		return  name;
+	}
+
+	get2(name,create) {
+		if(name == undefined) return this;
+
+		var cur = this;	
+		var [childName,...sub] = name.split(chs);
+		
+		if(cur.child[childName] == undefined) {
+			if(!create) return null;
+			cur.child[name] = new EventualChannel(this.eventual,name,this);
+		}
+		return this.get2(sub.join(chs),create);
+	};
+
+	// Should be private function
+	get(name,create) {
+		// How it should work?
+		// Recursivelly get a listener why? because of match
+		var nodes = name.split(chs);
+		var cur = this;
+		var ok = 1;
+
+		nodes.forEach((v) => {
+			if( cur.childs[v] == undefined) {
+				if(create == undefined) { ok = false; return null; }
+				cur.childs[v] = new EventualChannel(this.eventual,name,this);
+			}
+			cur = cur.childs[v]; // next;
+		});
+
+		if(!ok) return null;
+		return cur;
+	}
+	channel(name) { // Will create channel and return new child channel
+		return this.get(name,true);
+	}
+	// Get listeners
+	search(name,create) {
+		var channel	= this.get(name,create);
+		if(channel) return channel.listeners;
+		return [];
+	}
+	// Registers 
+	// new listener
+	on (name,cb,order) {
+		var channel = this.channel(name);
+		var listener = { name:name, callback:cb, order: order || 0 };
+		channel.listeners.push(listener)
+
+		// Check for state call
+		var cname = channel.fullName();
+		if(this.eventual._events.state[cname]!=undefined) {
+			this.process([listener],this.eventual._events.state[cname]);
+		}
+		return this;
+	}
+	emit(name,args) {
+		var tocall = this.search(name);
+		return this.process(tocall,args);
+	}
+
+	//TODO: CLEAN THIS
+	/*
+	 * Basically process a list of functions and calls done in end
+	 * */
+	process(tocall,nargs) {
+		var self=this;
+	
+		var args = nargs || [];	 // or empty
+		// Common for all for now	
+		var evt = new XEvent(next,args);
+
+		var callback,dotrigger = 0;
+		//Inner trigger	
+		// Finish	
+		function triggerDone() {
+			if(callback) callback(evt);
+			dotrigger = 1;
+		}
+		// Mem leak???
+		var ret = {
+			done: function(cb) {
+				if(dotrigger) { cb(evt); }
+				callback = cb;
+			}
+		}
+
+		//this.stat.emitted++;
+		// Sort
+		tocall = tocall.sort((a,b) => {
+			return (a.order - b.order) 
+		});	
+		// Get iter and setup evt
+		var iter = tocall[Symbol.iterator]();
+		
+		function next() {
+			process.nextTick(() => chain(iter));
+		}
+
+		// Internal thing
+		function chain(iter) { 
+			// Stack overflow risk? maybe put this in process.tick();
+			var v = iter.next();
+			if(v.done) {
+				triggerDone(); // Done callback with evt only
+				return;
+			}
+			// Create evt here with some global event chaining iteraction	
+			try {
+				var entry = v.value;
+				evt.args = args;
+				evt.name = entry.name;
+				//self.stat.called++;
+				entry.callback(...args,evt);
+				evt.count++;
+			}catch(e) {
+				console.log(e);
+				evt.errors.push(e);
+				// Should we stop??
+			}
+			if(!evt.dowait) next();
+			evt.dowait=0; // reset wait?
+		}
+		next();
+
+		return ret;
+	}
+
+
+}
+
+module.exports = EventualChannel;

+ 95 - 0
lib/eventual/eventual-context.js

@@ -0,0 +1,95 @@
+
+/** Concept each channel can have child channels ??**/
+var EventualChannel = require('./eventual-channel');
+
+class EventualContext2 extends EventualChannel {
+	constructor(eventual) {
+		super(eventual);
+	}
+	broadcast(name,...args) {
+		// Special send to all eventual contexts
+		var tocall = [];		
+		this.eventual._events.ctx.forEach((c) => {
+			tocall.push(...c.search(name));
+		});
+		return this.process(tocall,args)
+	}
+	state(name,...args) {
+		this.eventual.state(name,args);
+	}
+	destroy() {
+		this.childs = null;
+		this.listeners = null;
+		var i = this.eventual._events.ctx.indexOf(this);
+		if(i!=-1) this.eventual._events.ctx.splice(i,1);
+	}
+	toString() {
+		var out = [];
+		this.eventual._events.ctx.forEach((c) => {	
+			var cur = c;
+			// First level
+			function listChild(cur,prefix) {
+				for( var c in cur.childs) {
+					var name = (prefix)?prefix + ":" + c:c;
+				out.push(name);
+				listChild(cur.childs[c], name);
+				}
+			}
+			listChild(cur,"");
+		});
+
+		return out.join("\n");
+	}
+
+
+}
+
+
+
+/** Old and deprecated **/
+class EventualContext extends EventualChannel {
+	constructor(eventual) {
+		super();
+		this.eventual = eventual;
+		// Contextual listeners, it will invalidate regexp?
+		this.listeners = [];
+	}
+	process(tocall,args,donecb) {
+		tocall.forEach((l) => {
+			l.callback(...args);
+		});
+	}
+	broadcast(name,...args) {
+		this.eventual._events.ctx.forEach((c) => {
+			c.emit(name,...args);
+		});
+	}
+	/** flat model **/
+	on(name,cb) {
+		// Any listener will be placed here
+		this.listeners.push({
+			name: name,
+			callback:cb
+		});
+		return this;
+	}
+	emit(name,...args) {
+		// We need to go trought all contexts in Eventual
+		var tocall = [];
+		this.eventual._events.ctx.forEach((c) => {
+			c.listeners.forEach((l) =>  {
+				if(l.name == name) {
+					tocall.push(l);
+				}
+			})
+		});
+		this.process(tocall,name,args);
+	}/**/
+	destroy() {
+		var i = this.eventual._events.ctx.indexOf(this);
+		if(i!=-1) this.eventual._events.ctx.splice(i,1);
+		this.listeners = null;
+	}
+}
+
+module.exports = EventualContext2;

+ 31 - 0
lib/eventual/eventual.js

@@ -0,0 +1,31 @@
+/** 
+ * Substitute for xeventx
+ * */
+
+const EventualContext = require('./eventual-context');
+
+
+class Eventual {
+	constructor() {
+		this._events = {
+			ctx: [],
+			state: {} 
+		};
+	}
+	createContext() {
+		var ret = new EventualContext(this);
+		this._events.ctx.push(ret);
+		return ret;
+	}
+	destroyContext(ctx) {
+		var i = this._events.ctx.indexOf(ctx);
+		if(i!=-1) this._events.ctx.splice(i,1);
+	}
+	state(name,args) {
+		// Should do this per context
+		this._events.state[name] = args;
+	}
+}
+
+module.exports = Eventual;
+

+ 1 - 0
lib/eventual/index.js

@@ -0,0 +1 @@
+module.exports = require('./eventual');

+ 1 - 1
package.json

@@ -8,7 +8,7 @@
     "email": "luisf@hexasoftware.com"
   },
   "scripts": {
-    "test": "cd test/hci-usecase; node app"
+    "test": "cd test; node --expose-gc hci-usecase"
   },
   "keywords": [
     "osgi",

binární
test/eventual/lib/.eventual-context.js.swp


binární
test/eventual/doc/.README.md.swp


test/eventual/doc/README.md → test/eventual-test/doc/README.md


+ 128 - 0
test/eventual-test/index.js

@@ -0,0 +1,128 @@
+const Eventual = require('../../lib/eventual');
+const Benchmark = require('../../lib/utils/benchmark');
+
+
+
+function makeid()
+{
+    var text = "";
+    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+
+    for( var i=0; i < 5; i++ )
+        text += possible.charAt(Math.floor(Math.random() * possible.length));
+
+    return text;
+}
+
+
+var events = new Eventual();
+var cevt= events.createContext();
+var ctx2 = events.createContext();
+
+function stressEvents() {
+	// 1 million
+	var ncount = 1000000;
+	console.log("Adding",ncount,"events");
+	for(var i = 0;i<ncount;i++) {
+		// Create random contexts too
+		var evtName = makeid() + ":test";
+		cevt.on(evtName,(a,b,c,evt) => {
+			console.log("evt: " , evt);
+			console.log("Args: " + a,b,c);	
+		});
+	}
+	// Second context
+	console.log("Adding events to second context");
+	for(var i = 0;i<ncount;i++) {
+		ctx2.on('test:test',(a,b,c) => { });
+	};
+}
+
+
+
+
+
+function pinger() {
+
+	ctx2.on('c1:ping',() => {
+		console.log("Pong from ctx2");
+	});
+
+	var pingbm = new Benchmark();
+	var pctx = events.createContext();
+	pctx .on('c1:ping',(e) => {
+		console.log("Pong: " + pingbm.mark(), "Contexts:",events._events.ctx.length);
+	})
+
+	var test = 0;
+	setInterval(() => {
+		pingbm.start();
+		console.log("Sending ping");
+		pctx.emit('c1:ping').done((e) => {
+			console.log("executed");	
+		});
+		if( test > 10) { // 10 second
+			cevt.destroy();
+			ctx2.destroy();
+		}
+		test++;
+	},1000);
+}
+function sequential() {
+	// Testing done
+	var cc = events.createContext();	
+	var cc2 = events.createContext();
+
+	cc.on('hello',(e) => {
+		e.wait();
+		console.log("CC1 Hello will wait 1s");
+		setTimeout(() => {
+
+			e.done()
+		},1000);
+	});
+	cc2.on('hello',(e) => {
+		e.wait();
+		console.log("cc2 Hello");
+		setTimeout(() => {
+			e.done();
+		},2000);
+	});
+
+console.log("Broadcasting hello");
+	cc.broadcast('hello').done(() => {
+		console.log("Hello called");
+	});
+}
+
+
+//stressEvents();
+//pinger();
+//sequential();
+//
+
+// State changes
+
+var cc = events.createContext();
+var cc2 = events.createContext();
+//cc.emit('test');
+//cc.state('test',1);
+
+// call this
+cc2.on('test:test2',() => { // Should be calling it self
+	console.log("Ok2");
+});
+cc.on('test:test2:test3',() => { // Should be calling it self
+	console.log("ok3");
+});
+cc.onMatch('test.*',() => {
+	console.log("Event matched");
+});
+
+cc2.broadcast('test:test2:test3');
+
+
+
+
+
+

test/eventual/lib/eventual-context.js → test/eventual-test/lib/eventual-context.js


test/eventual/lib/eventual.js → test/eventual-test/lib/eventual.js


+ 0 - 63
test/eventual/index.js

@@ -1,63 +0,0 @@
-const Eventual = require('./lib/eventual.js');
-const Benchmark = require('../../lib/utils/benchmark');
-
-function makeid()
-{
-    var text = "";
-    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
-
-    for( var i=0; i < 5; i++ )
-        text += possible.charAt(Math.floor(Math.random() * possible.length));
-
-    return text;
-}
-
-
-
-
-var events = new Eventual();
-var cevt= events.createContext();
-var ctx2 = events.createContext();
-
-function stressEvents() {
-	// 1 million
-	var ncount = 1000000;
-	console.log("Adding",ncount,"events");
-	for(var i = 0;i<ncount;i++) {
-		// Create random contexts too
-		var evtName = makeid() + ":test";
-		cevt.on(evtName,(a,b,c,evt) => {
-			console.log("evt: " , evt);
-			console.log("Args: " + a,b,c);	
-		});
-	}
-	// Second context
-	console.log("Adding events to second context");
-	for(var i = 0;i<ncount;i++) {
-		ctx2.on('test:test',(a,b,c) => { });
-	};
-}
-
-stressEvents();
-
-ctx2.on('c1:ping',() => {
-	console.log("Pong from ctx2");
-});
-
-var pingbm = new Benchmark();
-var pctx = events.createContext();
-pctx .on('c1:ping',() => {
-		console.log("Pong: " + pingbm.mark(), "Contexts:",events._events.ctx.length);
-	})
-
-var test = 0;
-setInterval(() => {
-	pingbm.start();
-	console.log("Sending ping");
-	pctx.broadcast('c1:ping');
-	if( test > 5) {
-		cevt.destroy();
-		ctx2.destroy();
-	}
-	test++;
-},1000);

+ 10 - 0
test/hci-test/index.js

@@ -0,0 +1,10 @@
+var BundleManager = require('../../');
+
+
+var manager = new BundleManager();
+
+manager.loadDefaultBundles();
+
+
+
+

+ 0 - 5
test/hci-usecase/app/index.js

@@ -1,5 +0,0 @@
-var BundleManager = require('../../../');
-
-var manager = new BundleManager({runPath: __dirname + "/../bundles"});
-
-manager.loadDefaultBundles();

+ 27 - 8
test/hci-usecase/bundles/hci-http-monitor/index.js

@@ -26,14 +26,34 @@ function bundleStart(context) {
 		context.stop();
 	});
 
-	var channel = context.prefix('hci-http:req');
-	channel.on('/monitor/api/(.*)',(req,res,e)=> {
-		var [conName,conMethod = 'index',...params] = e.match[1].split(/\//);	
+	var channel = context
+		.channel('hci-http')
+		.on('req',(req,res,e) => {
+			// Manual router
+			var re = new RegExp("/monitor/api/(.*)");
+			var match = req.url.match(re);
+			if(match != null) {
+				api(match,req,res,e);
+				return;
+			}
+			re = new RegExp("/monitor/?(.*)");
+			match =	req.url.match(re);
+			if(match!=null) {
+				staticHandler(match,req,res,e);
+				return;
+			}
+			res.statusCode = 404;
+	});
+	//router?!
+
+	function api(match,req,res,e) {
+		var [conName,conMethod = 'index',...params] = match[1].split(/\//);	
 		var con = controllers[conName];
 		if(con == undefined || con[conMethod] == undefined) {
 			res.statusCode = 404;
 			return;
 		}
+
 		var to = setTimeout(function() {
 			res.writeHead(408);
 			res.end("Controller timeout");
@@ -45,16 +65,15 @@ function bundleStart(context) {
 		
 		con[conMethod](context,cb,...params);
 		// Setting a timeout would be good
-	});
+	};
 
-	// Static server for path / monitor
-	channel.on('/monitor/?(.*)',(req,res,e) => {
+	function staticHandler(match,req,res,e) {
 		if(e.count != 0) { // Something else executed
 			return;
 		}
 		// Not executed
 		//
-		var reqroute = e.match[1];
+		var reqroute = match[1];
 		if(reqroute.length == 0) {
 			reqroute = "index.html";
 		}
@@ -75,7 +94,7 @@ function bundleStart(context) {
 		res.writeHead(200,{'Content-type':mimeType});
 		var s = fs.createReadStream(fullPath);
 		s.pipe(res);
-	});
+	};
 
 }
 

+ 3 - 2
test/hci-usecase/bundles/hci-http/index.js

@@ -5,7 +5,8 @@ var activator = {
 
 	start(context) {
 		this.server = http.createServer((req,res) => {
-			context.emit('req:'+req.url,req,res) //??
+			// Global emmit
+			context.emit('req',req,res) //??
 						 .done((evt) => {
 							 // Array of results
 							 if(res.statusCode == 404 || evt.count == 0) {
@@ -35,7 +36,7 @@ var activator = {
 				this.clients[k].destroy();
 			};
 		});
-		context.toggleOn('serverStart',this);
+		context.state('serverStart',this);
 	},
 
 	stop(context) {

+ 1 - 1
test/hci-usecase/bundles/memory-monitor/index.js

@@ -3,7 +3,7 @@ var log = require('hlogger').createLogger('memory-monitor');
 var activator = {
 	start(context) {
 		context
-			.prefix('core-shell:cmd')
+			.channel('core-shell:cmd')
 			.on('memory',(req,res) => {
 				if(req.cmd == "start") {
 					var itime = time || itime;

+ 1 - 5
test/hci-usecase/bundles/shell-test/index.js

@@ -6,20 +6,16 @@ function bundleStart(context) {
 	log.info("Command provider installed");
 
 	// Create a bash and transport commands
-	context.after('.*:cmd:.*',(req,res,e) => {
-		if(e.count >0) return;
-
+	context.after('core-shell:cmd',(req,res,e) => {
 		e.wait();	
 		var shcmd = req.cmd + " " + req.args.join(" ");
 		var proc = child.exec(shcmd);
 		res.write("\n");
 		proc.stdout.pipe(res);
 		proc.stderr.pipe(process.stderr);
-
 		proc.on('close',function() {
 			e.done();
 		});			
-		e.done();
 	});
 }
 

+ 17 - 16
test/hci-usecase/bundles/stress-test/index.js

@@ -18,13 +18,13 @@ module.exports.bundleActivator = {
 	start(context) {
 		context.manager.load(__dirname + "/http-test");
 		this.context = context;
-		context.prefix('core-shell:cmd')
+		context.channel('core-shell:cmd')
 				.on('test',(req,res,e) => {
 					var cmds = ["events","cpu","mem","ping","siege"]; 
 					var [subcmd,...args] = req.args;
 					if(cmds.indexOf(subcmd) == -1) {
 						res.write("Wrong\n");	
-						res.write("Available tests:" , cmds + "\n");
+						res.write("Available tests:" + cmds + "\n");
 						return;
 					};
 
@@ -32,17 +32,19 @@ module.exports.bundleActivator = {
 				});
 
 		var lastPing; 
-		context.on('stress-test:ping',() => {
+		context.on('stress-test:ping',(e) => {
 			lastPing = process.hrtime();
 			context.emit('pong')
+			e.done();
 		});
-		context.on('stress-test:pong',() => {
+		context.on('stress-test:pong',(e) => {
 			var now = hrToStr(process.hrtime(lastPing));
 			console.log("Time taken to poing: " + now);
 		});
 	},
-	ping() {
-		this.context.emit('ping');
+	ping(req,res,e) {
+		e.wait();
+		this.context.emit('ping',e);
 	},
 	events(req,res,e) {
 		var count = 1000000;
@@ -69,16 +71,15 @@ module.exports.bundleActivator = {
 
 		// Loop with tick
 		//
-		if(arg == "start") {
-			var toggle = false;
-			this.ci = context.setInterval(() => {
-				if(toggle) {
-					this.context.manager.load(__dirname + "/memory-test-unit");
-				}else {
-					this.context.manager.unregister("memory-test-unit");
-				}
-			},5000);
-		}
+		var toggle = false;
+		this.ci = context.setInterval(() => {
+			if(toggle) {
+				this.context.manager.load(__dirname + "/memory-test-unit");
+			}else {
+				this.context.manager.unregister("memory-test-unit");
+			}
+			toggle = !toggle;
+		},5000);
 	},
 
 	siege(req,res,e) {

+ 3 - 3
test/hci-usecase/bundles/stress-test/memory-test-unit/index.js

@@ -2,7 +2,7 @@ var log = require('hlogger').createLogger('jspak.Config');
 
 class BigObject {
 	constructor() {
-		this.temp = new Array(1e3);
+		this.temp = new Array(1e5);
 	}
 }
 
@@ -48,10 +48,10 @@ var activator = {
 				e.done();
 			});
 		}
-		context.on(context,"time",() => {
+		context.on("time",() => {
 			console.time("beef");
 		});
-		context.on(context,"timeEnd",() => {
+		context.on("timeEnd",() => {
 				console.timeEnd("beef");
 		});
 		/*this.interval = setInterval(() => {

+ 5 - 0
test/hci-usecase/index.js

@@ -0,0 +1,5 @@
+var BundleManager = require('../../');
+
+var manager = new BundleManager({runPath: __dirname + "/bundles"});
+
+manager.loadDefaultBundles();