From 25670cbd1966f7c5ea02c2dc6c008c28bb1a56e7 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Sat, 16 Jul 2011 00:28:13 -0700 Subject: [PATCH] refactored debug-server --- bin/inspector.js | 13 +++--- lib/debug-server.js | 96 ++++++++++++++++++++++----------------------- lib/session.js | 6 +-- 3 files changed, 57 insertions(+), 58 deletions(-) diff --git a/bin/inspector.js b/bin/inspector.js index 57250cd..3aeccf2 100755 --- a/bin/inspector.js +++ b/bin/inspector.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -var dserver = require('../lib/debug-server'), +var DebugServer = require('../lib/debug-server').DebugServer, fs = require('fs'), path = require('path'), options = {}; @@ -12,7 +12,6 @@ process.argv.forEach(function (arg) { if (parts.length > 1) { switch (parts[0]) { case '--web-port': - case '--agent-port': options.webPort = parseInt(parts[1], 10); break; default: @@ -21,7 +20,7 @@ process.argv.forEach(function (arg) { } } else if (parts[0] === '--help') { - console.log('Usage: node [node_options] debug-agent.js [options]'); + console.log('Usage: node-inspector [options]'); console.log('Options:'); console.log('--web-port=[port] port to host the inspector (default 8080)'); process.exit(); @@ -30,7 +29,8 @@ process.argv.forEach(function (arg) { }); fs.readFile(path.join(__dirname, '../config.json'), function(err, data) { - var config; + var config, + dserver; if (err) { console.warn("could not load config.json\n" + err.toString()); config = {}; @@ -49,8 +49,11 @@ fs.readFile(path.join(__dirname, '../config.json'), function(err, data) { if (!config.debugPort) { config.debugPort = 5858; } - dserver.create(options, config).on('close', function () { + + dserver = new DebugServer(); + dserver.on('close', function () { console.log('session closed'); process.exit(); }); + dserver.listen(options.webPort || config.webPort); }); diff --git a/lib/debug-server.js b/lib/debug-server.js index afa83ff..c97b3c9 100644 --- a/lib/debug-server.js +++ b/lib/debug-server.js @@ -1,65 +1,63 @@ var Http = require('http'), EventEmitter = require('events').EventEmitter, - path = require('path'), WebSocket = require('websocket-server'), paperboy = require('paperboy'), Session = require('./session'), - WEBROOT = path.join(__dirname, '../front-end'), - sessions = {}; + inherits = require('util').inherits, + WEBROOT = require('path').join(__dirname, '../front-end'), + sessions = {}, + webPort; -function staticFile(req, res) { +function serveStaticFiles(req, res) { req.url = req.url.replace(/^\/debug/, '/'); paperboy.deliver(WEBROOT, req, res); } -function override(options, defaults) { - var result = {}; - Object.keys(defaults).forEach(function(key) { - result[key] = options[key] || defaults[key]; - }); - return result; +function getDebuggerPort(url, defaultPort) { + return parseInt((/\?port=(\d+)/.exec(url) || [null, defaultPort])[1], 10); } -exports.create = function(options, config) { - var defaults = { webPort: config.webPort }, - settings = override(options || {}, defaults), - httpServer = Http.createServer(staticFile), - wsServer = WebSocket.createServer({server: httpServer}), - debugPort = config.debugPort.toString(); +function getSession(debuggerPort) { + var session = sessions[debuggerPort]; + if (!session) { + session = Session.create(debuggerPort, {}); // TODO fix config + sessions[debuggerPort] = session; + // TODO session on close + } + return session; +} - wsServer.on('connection', function(conn) { - var port = - parseInt((/\?port=(\d+)/.exec(conn._req.url) || [null, debugPort])[1], 10), - session = sessions[port]; - if (!session) { - session = Session.create(port, config); - sessions[port] = session; - } - session.join(conn) - // XXX should session and debugger be bound together? - }); +function handleWebSocketConnection(conn) { + var port = getDebuggerPort(conn._req.url, 5858); // TODO + getSession(port).join(conn) +} - wsServer.listen(settings.webPort); +function handleServerListening() { + console.log( + 'visit http://0.0.0.0:' + + webPort + + '/debug?port=5858 to start debugging'); // TODO port +} - wsServer.on('listening', function() { - console.log( - 'visit http://0.0.0.0:' + - settings.webPort + - '/debug?port=5858 to start debugging'); - }); +function DebugServer() { + var httpServer = Http.createServer(serveStaticFiles); + this.wsServer = WebSocket.createServer({ server: httpServer }); + this.wsServer.on('connection', handleWebSocketConnection); + this.wsServer.on('listening', handleServerListening); +} - return Object.create(EventEmitter.prototype, { - close: { - value: function() - { - if (wsServer) { - wsServer.close(); - } - this.emit('close'); - } - }, - webPort: { - get: function() { return settings.webPort; } - } - }); -}; +inherits(DebugServer, EventEmitter); + +DebugServer.prototype.close = function() { + if (this.wsServer) { + this.wsServer.close(); + this.emit('close'); + } +} + +DebugServer.prototype.listen = function(port) { + webPort = port; + this.wsServer.listen(port); +} + +exports.DebugServer = DebugServer; diff --git a/lib/session.js b/lib/session.js index 3316387..e932691 100644 --- a/lib/session.js +++ b/lib/session.js @@ -1,7 +1,5 @@ -var http = require('http'), - events = require('events'), - debugr = require('./debugger'), - path = require('path'); +var events = require('events'), + debugr = require('./debugger'); /////////////////////////////////////////////////////////// // exports