Browse files

Revamps architecture, especially due to debugger agent

  • Loading branch information...
1 parent d2896f5 commit a9149df5634e250e94993a913c9fc3168b9bb48c @c4milo committed May 15, 2012
Showing with 179 additions and 105 deletions.
  1. +146 −61 index.js
  2. +2 −2 package.json
  3. +31 −42 webkit-devtools-agent.js
View
207 index.js
@@ -1,79 +1,164 @@
var agents = require('./lib');
-var fork = require('child_process').fork;
-
-var child = fork('./webkit-devtools-agent.js', process.argv);
-
-initializeAgents();
-
-child.on('message', function(message) {
- var data = message.data;
- switch(message.event) {
- case 'connection':
- //initializeAgents();
- break;
- case 'method':
- var id = data.id;
- var command = data.method.split('.');
- var domain = agents[command[0]];
- var method = command[1];
- var params = data.params;
-
- if (!domain || !domain[method]) {
- console.warn('%s is not implemented', data.method);
- return;
+var spawn = require('child_process').spawn;
+var WebSocketServer = require('ws').Server;
+
+var DevToolsAgent = function() {
+ this.proxy = null;
+ this.server = null;
+ this.socket = null;
+ this.agents = {};
+};
+
+(function() {
+ //Private variables
+ var proxyPID = 0;
+
+ //Private functions
+ var spawnProxy = function() {
+ var self = this;
+
+ this.proxy = spawn(__dirname + '/webkit-devtools-agent.js', process.argv, {
+ env: process.env,
+ cwd: __dirname
+ });
+ proxyPID = this.proxy.pid;
+
+ this.proxy.stderr.setEncoding('utf8');
+ this.proxy.stderr.on('data', function (data) {
+ console.error(data);
+ });
+
+ this.proxy.stdout.setEncoding('utf8');
+ this.proxy.stdout.on('data', function (data) {
+ console.log(data);
+ });
+ }.bind(this);
+
+ var onProxyConnection = function(socket) {
+ console.log('webkit-devtools-agent: A proxy got connected.');
+ console.log('webkit-devtools-agent: Waiting for commands...');
+
+ this.socket = socket;
+ this.socket.on('message', onProxyData);
+ this.socket.on('error', function(error) {
+ console.error(error);
+ });
+ }.bind(this);
+
+ var onProxyData = function(message) {
+ var self = this;
+
+ try {
+ data = JSON.parse(message);
+ } catch(e) {
+ console.log(e);
+ console.log(e.stack);
+ return;
+ }
+
+ var id = data.id;
+ var command = data.method.split('.');
+ var domain = this.agents[command[0]];
+ var method = command[1];
+ var params = data.params;
+
+ if (!domain || !domain[method]) {
+ console.warn('%s is not implemented', data.method);
+ return;
+ }
+
+ domain[method](params, function(result) {
+ var response = {
+ id: id,
+ result: result
+ };
+
+ self.socket.send(JSON.stringify(response));
+ });
+ }.bind(this);
+
+ var loadAgents = function() {
+ var self = this;
+
+ var sendEvent = function(data) {
+ self.socket.send(JSON.stringify(data));
+ };
+
+ var runtimeAgent = new agents.Runtime(sendEvent);
+ this.agents = {};
+
+ for (var agent in agents) {
+ if (typeof agents[agent] == 'function' && agent != 'Runtime') {
+ this.agents[agent] = new agents[agent](sendEvent, runtimeAgent);
}
+ }
+ this.agents.Runtime = runtimeAgent;
+ }.bind(this);
- domain[method](params, function(result) {
- var sent = child.send({
- event: 'result',
- data: {
- id: id,
- result: result
- }
- });
- if (!sent) {
- console.log('PUTA !!!');
- console.log(result);
- }
- });
- break;
- }
-});
+ // Public functions
+ this.start = function() {
+ var self = this;
+
+ if (this.server) return;
-function initializeAgents() {
- var sendEvent = function(data) {
- child.send({
- event: 'event',
- data: data
+ this.server = new WebSocketServer({
+ port: 3333,
+ host: 'localhost'
});
+
+ this.server.on('listening', function() {
+ console.log('webkit-devtools-agent: Spawning websocket ' +
+ 'service process...');
+
+ //Spawns webkit devtools proxy / websockets server
+ spawnProxy();
+
+ loadAgents();
+ });
+ this.server.on('connection', onProxyConnection);
};
- var runtimeAgent = new agents.Runtime(sendEvent);
+ this.stop = function() {
+ console.log('webkit-devtools-agent: Terminating websockets service' +
+ ' with PID: ' + proxyPID + '...');
- for (var agent in agents) {
- if (typeof agents[agent] == 'function' &&
- agent != 'Runtime') {
- agents[agent] = new agents[agent](sendEvent, runtimeAgent);
+ if (this.socket) {
+ this.socket.end();
}
- }
- agents['Runtime'] = runtimeAgent;
-}
+ if (proxyPID) {
+ process.kill(proxyPID, 'SIGTERM');
+ proxyPID = 0;
+ }
+
+ if (this.server) {
+ this.server.close();
+ this.server = null;
+ }
+ };
+}).call(DevToolsAgent.prototype);
+
+var nodeAgent = new DevToolsAgent();
+
+//Prepares signal handler to activate the agent
if (!module.parent) {
- child.kill('SIGUSR2');
+ nodeAgent.start();
} else {
process.on('SIGUSR2', function() {
- child.kill('SIGUSR2');
+ if (nodeAgent.server) {
+ nodeAgent.stop();
+ } else {
+ nodeAgent.start();
+ }
});
}
-child.on('exit', function(code, signal) {
- console.log(code);
- console.log(signal);
- //TODO: re-spawn it
-});
-
-process.on('exit', function() {
- child.kill('SIGTERM');
+['exit', 'uncaughtException'].forEach(function(e) {
+ process.on(e, function(e) {
+ if (e) {
+ console.log(e);
+ }
+ nodeAgent.stop();
+ });
});
View
4 package.json
@@ -23,8 +23,8 @@
},
"dependencies": {
"v8-profiler": "git://github.com/c4milo/v8-profiler.git#master",
- "ws": "0.4.13",
+ "ws": "0.4.14",
"underscore": "1.3.3"
},
- "main": "webkit-devtools-agent"
+ "main": "index"
}
View
73 webkit-devtools-agent.js 100644 → 100755
@@ -1,62 +1,43 @@
+#!/usr/bin/env node
var Debugger = require('./lib/debugger');
-var WebSocketServer = require('ws').Server;
+var WebSocket = require('ws');
+var WebSocketServer = WebSocket.Server;
-var DevToolsAgent = function() {
+var DevToolsAgentProxy = function() {
this.wss = null;
this.port = process.env.DEBUG_PORT || 9999;
this.host = process.env.DEBUG_HOST || '127.0.0.1';
};
(function() {
var self = this;
- process.on('SIGUSR2', function() {
- if (self.wss) {
- self.stop();
- } else {
- self.start();
- }
- });
-
process.on('uncaughtException', function (err) {
- console.error('webkit-devtools-agent: uncaughtException: ');
+ console.error('webkit-devtools-agent: Websockets service uncaught exception: ');
console.error(err);
console.error(err.stack);
});
this.start = function() {
- this.wss = new WebSocketServer({
- port: this.port,
- host: this.host
- });
-
- console.log('webkit-devtools-agent started on %s:%s', host, port);
+ var self = this;
- this.wss.on('connection', function(socket) {
- process.send({
- event: 'connection'
+ var backend = new WebSocket('ws://localhost:3333');
+ backend.on('open', function() {
+ //Starts websockets server for devtools front-end
+ self.wss = new WebSocketServer({
+ port: self.port,
+ host: self.host
});
- process.on('message', function(message) {
- var data = message.data;
+ console.log('webkit-devtools-agent: Websockets ' +
+ 'service started on %s:%s', self.host, self.port);
- switch(message.event) {
- case 'event':
- case 'result':
- socket.send(JSON.stringify(data));
- }
- });
-
- socket.on('message', function(message) {
- try {
- message = JSON.parse(message);
- } catch(e) {
- console.error(e);
- return;
- }
+ self.wss.on('connection', function(socket) {
+ backend.on('message', function(message) {
+ socket.send(message);
+ });
- process.send({
- event: 'method',
- data: message
+ socket.on('message', function(message) {
+ backend.send(message);
});
});
});
@@ -66,11 +47,19 @@ var DevToolsAgent = function() {
if (this.wss) {
this.wss.close();
this.wss = null;
- console.log('webkit-devtools-agent stopped');
+ console.log('webkit-devtools-agent: Websockets service with PID ' +
+ process.pid + ' stopped');
}
};
-}).call(DevToolsAgent.prototype);
+}).call(DevToolsAgentProxy.prototype);
-module.exports = new DevToolsAgent();
+var proxy = new DevToolsAgentProxy();
+proxy.start();
+
+['exit', 'SIGTERM', 'SIGHUP'].forEach(function(s) {
+ process.on(s, function() {
+ proxy.stop();
+ });
+});

0 comments on commit a9149df

Please sign in to comment.