Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Revamps architecture, especially due to debugger agent

  • Loading branch information...
commit a9149df5634e250e94993a913c9fc3168b9bb48c 1 parent d2896f5
Camilo Aguilar authored May 15, 2012
207  index.js
... ...
@@ -1,79 +1,164 @@
1 1
 var agents = require('./lib');
2  
-var fork = require('child_process').fork;
3  
-
4  
-var child = fork('./webkit-devtools-agent.js', process.argv);
5  
-
6  
-initializeAgents();
7  
-
8  
-child.on('message', function(message) {
9  
-    var data = message.data;
10  
-    switch(message.event) {
11  
-        case 'connection':
12  
-            //initializeAgents();
13  
-            break;
14  
-        case 'method':
15  
-            var id = data.id;
16  
-            var command = data.method.split('.');
17  
-            var domain = agents[command[0]];
18  
-            var method = command[1];
19  
-            var params = data.params;
20  
-
21  
-            if (!domain || !domain[method]) {
22  
-                console.warn('%s is not implemented', data.method);
23  
-                return;
  2
+var spawn = require('child_process').spawn;
  3
+var WebSocketServer = require('ws').Server;
  4
+
  5
+var DevToolsAgent = function() {
  6
+    this.proxy = null;
  7
+    this.server = null;
  8
+    this.socket = null;
  9
+    this.agents = {};
  10
+};
  11
+
  12
+(function() {
  13
+    //Private variables
  14
+    var proxyPID = 0;
  15
+
  16
+    //Private functions
  17
+    var spawnProxy = function() {
  18
+        var self = this;
  19
+
  20
+        this.proxy = spawn(__dirname + '/webkit-devtools-agent.js', process.argv, {
  21
+            env: process.env,
  22
+            cwd: __dirname
  23
+        });
  24
+        proxyPID = this.proxy.pid;
  25
+
  26
+        this.proxy.stderr.setEncoding('utf8');
  27
+        this.proxy.stderr.on('data', function (data) {
  28
+            console.error(data);
  29
+        });
  30
+
  31
+        this.proxy.stdout.setEncoding('utf8');
  32
+        this.proxy.stdout.on('data', function (data) {
  33
+            console.log(data);
  34
+        });
  35
+    }.bind(this);
  36
+
  37
+    var onProxyConnection = function(socket) {
  38
+        console.log('webkit-devtools-agent: A proxy got connected.');
  39
+        console.log('webkit-devtools-agent: Waiting for commands...');
  40
+
  41
+        this.socket = socket;
  42
+        this.socket.on('message', onProxyData);
  43
+        this.socket.on('error', function(error) {
  44
+            console.error(error);
  45
+        });
  46
+    }.bind(this);
  47
+
  48
+    var onProxyData = function(message) {
  49
+        var self = this;
  50
+
  51
+        try {
  52
+            data = JSON.parse(message);
  53
+        } catch(e) {
  54
+            console.log(e);
  55
+            console.log(e.stack);
  56
+            return;
  57
+        }
  58
+
  59
+        var id = data.id;
  60
+        var command = data.method.split('.');
  61
+        var domain = this.agents[command[0]];
  62
+        var method = command[1];
  63
+        var params = data.params;
  64
+
  65
+        if (!domain || !domain[method]) {
  66
+            console.warn('%s is not implemented', data.method);
  67
+            return;
  68
+        }
  69
+
  70
+        domain[method](params, function(result) {
  71
+            var response = {
  72
+                id: id,
  73
+                result: result
  74
+            };
  75
+
  76
+            self.socket.send(JSON.stringify(response));
  77
+        });
  78
+    }.bind(this);
  79
+
  80
+    var loadAgents = function() {
  81
+        var self = this;
  82
+
  83
+        var sendEvent = function(data) {
  84
+            self.socket.send(JSON.stringify(data));
  85
+        };
  86
+
  87
+        var runtimeAgent = new agents.Runtime(sendEvent);
  88
+        this.agents = {};
  89
+
  90
+        for (var agent in agents) {
  91
+            if (typeof agents[agent] == 'function' && agent != 'Runtime') {
  92
+                this.agents[agent] = new agents[agent](sendEvent, runtimeAgent);
24 93
             }
  94
+        }
  95
+        this.agents.Runtime = runtimeAgent;
  96
+    }.bind(this);
25 97
 
26  
-            domain[method](params, function(result) {
27  
-                var sent = child.send({
28  
-                    event: 'result',
29  
-                    data: {
30  
-                        id: id,
31  
-                        result: result
32  
-                    }
33  
-                });
34  
-                if (!sent) {
35  
-                    console.log('PUTA !!!');
36  
-                    console.log(result);
37  
-                }
38  
-            });
39  
-            break;
40  
-    }
41  
-});
  98
+    // Public functions
  99
+    this.start = function() {
  100
+        var self = this;
  101
+
  102
+        if (this.server) return;
42 103
 
43  
-function initializeAgents() {
44  
-    var sendEvent = function(data) {
45  
-        child.send({
46  
-            event: 'event',
47  
-            data: data
  104
+        this.server = new WebSocketServer({
  105
+            port: 3333,
  106
+            host: 'localhost'
48 107
         });
  108
+
  109
+        this.server.on('listening', function() {
  110
+            console.log('webkit-devtools-agent: Spawning websocket ' +
  111
+            'service process...');
  112
+
  113
+            //Spawns webkit devtools proxy / websockets server
  114
+            spawnProxy();
  115
+
  116
+            loadAgents();
  117
+        });
  118
+        this.server.on('connection', onProxyConnection);
49 119
     };
50 120
 
51  
-    var runtimeAgent = new agents.Runtime(sendEvent);
  121
+    this.stop = function() {
  122
+        console.log('webkit-devtools-agent: Terminating websockets service' +
  123
+        ' with PID: ' + proxyPID + '...');
52 124
 
53  
-    for (var agent in agents) {
54  
-        if (typeof agents[agent] == 'function' &&
55  
-            agent != 'Runtime') {
56  
-                agents[agent] = new agents[agent](sendEvent, runtimeAgent);
  125
+        if (this.socket) {
  126
+            this.socket.end();
57 127
         }
58  
-    }
59  
-    agents['Runtime'] = runtimeAgent;
60  
-}
61 128
 
  129
+        if (proxyPID) {
  130
+            process.kill(proxyPID, 'SIGTERM');
  131
+            proxyPID = 0;
  132
+        }
  133
+
  134
+        if (this.server) {
  135
+            this.server.close();
  136
+            this.server = null;
  137
+        }
  138
+    };
  139
+}).call(DevToolsAgent.prototype);
  140
+
  141
+var nodeAgent = new DevToolsAgent();
  142
+
  143
+//Prepares signal handler to activate the agent
62 144
 if (!module.parent) {
63  
-    child.kill('SIGUSR2');
  145
+    nodeAgent.start();
64 146
 } else {
65 147
     process.on('SIGUSR2', function() {
66  
-        child.kill('SIGUSR2');
  148
+        if (nodeAgent.server) {
  149
+            nodeAgent.stop();
  150
+        } else {
  151
+            nodeAgent.start();
  152
+        }
67 153
     });
68 154
 }
69 155
 
70  
-child.on('exit', function(code, signal) {
71  
-    console.log(code);
72  
-    console.log(signal);
73  
-    //TODO: re-spawn it
74  
-});
75  
-
76  
-process.on('exit', function() {
77  
-    child.kill('SIGTERM');
  156
+['exit', 'uncaughtException'].forEach(function(e) {
  157
+    process.on(e, function(e) {
  158
+        if (e) {
  159
+            console.log(e);
  160
+        }
  161
+        nodeAgent.stop();
  162
+    });
78 163
 });
79 164
 
4  package.json
@@ -23,8 +23,8 @@
23 23
     },
24 24
     "dependencies": {
25 25
         "v8-profiler": "git://github.com/c4milo/v8-profiler.git#master",
26  
-        "ws": "0.4.13",
  26
+        "ws": "0.4.14",
27 27
         "underscore": "1.3.3"
28 28
     },
29  
-    "main": "webkit-devtools-agent"
  29
+    "main": "index"
30 30
 }
73  webkit-devtools-agent.js 100644 → 100755
... ...
@@ -1,7 +1,9 @@
  1
+#!/usr/bin/env node
1 2
 var Debugger = require('./lib/debugger');
2  
-var WebSocketServer = require('ws').Server;
  3
+var WebSocket = require('ws');
  4
+var WebSocketServer = WebSocket.Server;
3 5
 
4  
-var DevToolsAgent = function() {
  6
+var DevToolsAgentProxy = function() {
5 7
     this.wss = null;
6 8
     this.port = process.env.DEBUG_PORT || 9999;
7 9
     this.host = process.env.DEBUG_HOST || '127.0.0.1';
@@ -9,54 +11,33 @@ var DevToolsAgent = function() {
9 11
 
10 12
 (function() {
11 13
     var self = this;
12  
-    process.on('SIGUSR2', function() {
13  
-        if (self.wss) {
14  
-            self.stop();
15  
-        } else {
16  
-            self.start();
17  
-        }
18  
-    });
19  
-
20 14
     process.on('uncaughtException', function (err) {
21  
-        console.error('webkit-devtools-agent: uncaughtException: ');
  15
+        console.error('webkit-devtools-agent: Websockets service uncaught exception: ');
22 16
         console.error(err);
23 17
         console.error(err.stack);
24 18
     });
25 19
 
26 20
     this.start = function() {
27  
-        this.wss = new WebSocketServer({
28  
-            port: this.port,
29  
-            host: this.host
30  
-        });
31  
-
32  
-        console.log('webkit-devtools-agent started on %s:%s', host, port);
  21
+        var self = this;
33 22
 
34  
-        this.wss.on('connection', function(socket) {
35  
-            process.send({
36  
-                event: 'connection'
  23
+        var backend = new WebSocket('ws://localhost:3333');
  24
+        backend.on('open', function() {
  25
+            //Starts websockets server for devtools front-end
  26
+            self.wss = new WebSocketServer({
  27
+                port: self.port,
  28
+                host: self.host
37 29
             });
38 30
 
39  
-            process.on('message', function(message) {
40  
-                var data = message.data;
  31
+            console.log('webkit-devtools-agent: Websockets ' +
  32
+            'service started on %s:%s', self.host, self.port);
41 33
 
42  
-                switch(message.event) {
43  
-                    case 'event':
44  
-                    case 'result':
45  
-                        socket.send(JSON.stringify(data));
46  
-                }
47  
-            });
48  
-
49  
-            socket.on('message', function(message) {
50  
-                try {
51  
-                    message = JSON.parse(message);
52  
-                } catch(e) {
53  
-                    console.error(e);
54  
-                    return;
55  
-                }
  34
+            self.wss.on('connection', function(socket) {
  35
+                backend.on('message', function(message) {
  36
+                    socket.send(message);
  37
+                });
56 38
 
57  
-                process.send({
58  
-                    event: 'method',
59  
-                    data: message
  39
+                socket.on('message', function(message) {
  40
+                    backend.send(message);
60 41
                 });
61 42
             });
62 43
         });
@@ -66,11 +47,19 @@ var DevToolsAgent = function() {
66 47
         if (this.wss) {
67 48
             this.wss.close();
68 49
             this.wss = null;
69  
-            console.log('webkit-devtools-agent stopped');
  50
+            console.log('webkit-devtools-agent: Websockets service with PID ' +
  51
+            process.pid + ' stopped');
70 52
         }
71 53
     };
72  
-}).call(DevToolsAgent.prototype);
  54
+}).call(DevToolsAgentProxy.prototype);
73 55
 
74  
-module.exports = new DevToolsAgent();
  56
+var proxy = new DevToolsAgentProxy();
75 57
 
  58
+proxy.start();
  59
+
  60
+['exit', 'SIGTERM', 'SIGHUP'].forEach(function(s) {
  61
+    process.on(s, function() {
  62
+        proxy.stop();
  63
+    });
  64
+});
76 65
 

0 notes on commit a9149df

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