Skip to content
Browse files

two way communiation established.

  • Loading branch information...
1 parent 298ea19 commit 1810991f0b7bbe84cc3590b7f53e8167174eac5a Joachim Kainz committed Sep 24, 2011
Showing with 102 additions and 11 deletions.
  1. +95 −3 lib/backgrounder-launcher.js
  2. +5 −4 lib/backgrounder.js
  3. +1 −3 tests/master.js
  4. +1 −1 tests/worker.js
View
98 lib/backgrounder-launcher.js
@@ -1,4 +1,96 @@
-var out = JSON.stringify({"type":"console", "content" : "started!!!"});
+var util = require('util');
+var events = require("events");
-console.error(out);
-process.stdout.write(out + '\n');
+function format(f) {
+ if (typeof f !== 'string') {
+ var objects = [];
+ for (var i = 0; i < arguments.length; i++) {
+ objects.push(util.inspect(arguments[i]));
+ }
+ return objects.join(' ');
+ }
+
+
+ var i = 1;
+ var args = arguments;
+ var str = String(f).replace(/%[sdj]/g, function(x) {
+ switch (x) {
+ case '%s': return String(args[i++]);
+ case '%d': return Number(args[i++]);
+ case '%j': return JSON.stringify(args[i++]);
+ default:
+ return x;
+ }
+ });
+ for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
+ if (x === null || typeof x !== 'object') {
+ str += ' ' + x;
+ } else {
+ str += ' ' + util.inspect(x);
+ }
+ }
+ return str;
+}
+
+function log(message) {
+ var _message = {"type": "console", "content" : message};
+ var json = JSON.stringify(_message);
+
+ process.stdout.write(json + '\n');
+}
+
+console.log = function() {
+ var message = format.apply(this, arguments);
+
+ log(message);
+};
+
+console.dir = function(object) {
+ var message = util.inspect(object);
+
+ log(message);
+};
+
+var times = {};
+console.time = function(label) {
+ times[label] = Date.now();
+};
+
+
+console.timeEnd = function(label) {
+ var duration = Date.now() - times[label]
+
+ console.log('%s: %dms', label, duration);
+};
+
+var buffer = "";
+var emitter = new events.EventEmitter();
+
+process.stdin.resume();
+process.stdin.setEncoding('utf8');
+process.stdin.on('data', function(data) {
+ buffer += data.toString();
+
+ if (buffer.substr(-1) === '\n') {
+ var parsed = JSON.parse(buffer);
+ buffer = "";
+
+ emitter.emit("message", parsed);
+ }
+})
+
+process.on = function(message, listener) {
+ emitter.on(message, listener);
+}
+
+process.send = function(message) {
+ var json = JSON.stringify({"type":"message", "content" : message});
+
+ process.stdout.write(json + '\n');
+}
+
+console.info = console.log;
+
+var module = process.argv[2];
+
+require(module);
View
9 lib/backgrounder.js
@@ -20,15 +20,13 @@ function Child(mdl, module) {
this.process.stdout.on('data', function(data) {
self.buffer += data.toString();
- console.log("this.buffer: %s", self.buffer);
-
if (self.buffer.substr(-1) === '\n') {
+ console.error("self.buffer: %s", self.buffer);
var parsed = JSON.parse(self.buffer);
self.buffer = "";
processMessage(this.emitter, parsed);
}
-
})
this.process.stderr.on('data', function(data) {
var _data = data.toString();
@@ -44,7 +42,10 @@ function Child(mdl, module) {
Child.prototype.on = function(event, listener) {
this.emitter.on(event, listener);
};
-Child.prototype.send = function() {
+Child.prototype.send = function(message) {
+ var json = JSON.stringify(message);
+
+ this.process.stdin.write(json + '\n');
};
module.exports.spawn = function(module) {
View
4 tests/master.js
@@ -1,13 +1,11 @@
var util = require("util");
var backgrounder = require("../lib/backgrounder");
-var worker = backgrounder.spawn("./worker");
+var worker = backgrounder.spawn(__dirname + "/worker.js");
worker.on("message", function(message) {
console.log(util.inspect(message, false, 100));
});
-console.log(util.inspect(console));
-
worker.send({
"title": "hello world!",
"flag": true
View
2 tests/worker.js
@@ -1,5 +1,5 @@
var util = require("util");
-console.log('Started the working');
+console.log('Started the worker');
process.on('message', function(message) {
console.log('Worker received: %s', util.inspect(message, false, 100));
process.send({

0 comments on commit 1810991

Please sign in to comment.
Something went wrong with that request. Please try again.