Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/aphlict_server.js
| Show All 19 Lines | |||||
| var config = parse_command_line_arguments(process.argv); | var config = parse_command_line_arguments(process.argv); | ||||
| if (config.logfile) { | if (config.logfile) { | ||||
| debug.addLogfile(config.logfile); | debug.addLogfile(config.logfile); | ||||
| } | } | ||||
| function parse_command_line_arguments(argv) { | function parse_command_line_arguments(argv) { | ||||
| var config = { | var config = { | ||||
| port : 22280, | port: 22280, | ||||
| admin : 22281, | admin: 22281, | ||||
| host : '127.0.0.1', | host: '127.0.0.1', | ||||
| user : null, | user: null, | ||||
| log: '/var/log/aphlict.log' | log: '/var/log/aphlict.log' | ||||
| }; | }; | ||||
| for (var ii = 2; ii < argv.length; ii++) { | for (var ii = 2; ii < argv.length; ii++) { | ||||
| var arg = argv[ii]; | var arg = argv[ii]; | ||||
| var matches = arg.match(/^--([^=]+)=(.*)$/); | var matches = arg.match(/^--([^=]+)=(.*)$/); | ||||
| if (!matches) { | if (!matches) { | ||||
| throw new Error("Unknown argument '"+arg+"'!"); | throw new Error("Unknown argument '" + arg + "'!"); | ||||
| } | } | ||||
| if (!(matches[1] in config)) { | if (!(matches[1] in config)) { | ||||
| throw new Error("Unknown argument '"+matches[1]+"'!"); | throw new Error("Unknown argument '" + matches[1] + "'!"); | ||||
| } | } | ||||
| config[matches[1]] = matches[2]; | config[matches[1]] = matches[2]; | ||||
| } | } | ||||
| config.port = parseInt(config.port, 10); | config.port = parseInt(config.port, 10); | ||||
| config.admin = parseInt(config.admin, 10); | config.admin = parseInt(config.admin, 10); | ||||
| return config; | return config; | ||||
| } | } | ||||
| if (process.getuid() !== 0) { | if (process.getuid() !== 0) { | ||||
| console.log( | console.log( | ||||
| "ERROR: "+ | "ERROR: " + | ||||
| "This server must be run as root because it needs to bind to privileged "+ | "This server must be run as root because it needs to bind to privileged " + | ||||
| "port 843 to start a Flash policy server. It will downgrade to run as a "+ | "port 843 to start a Flash policy server. It will downgrade to run as a " + | ||||
| "less-privileged user after binding if you pass a user in the command "+ | "less-privileged user after binding if you pass a user in the command " + | ||||
| "line arguments with '--user=alincoln'."); | "line arguments with '--user=alincoln'."); | ||||
| process.exit(1); | process.exit(1); | ||||
| } | } | ||||
| var net = require('net'); | var net = require('net'); | ||||
| var http = require('http'); | var http = require('http'); | ||||
| var url = require('url'); | var url = require('url'); | ||||
| process.on('uncaughtException', function (err) { | process.on('uncaughtException', function(err) { | ||||
| debug.log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err); | debug.log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err); | ||||
| process.exit(1); | process.exit(1); | ||||
| }); | }); | ||||
| var flash_server = new JX.AphlictFlashPolicyServer() | var flash_server = new JX.AphlictFlashPolicyServer() | ||||
| .setDebugLog(debug) | .setDebugLog(debug) | ||||
| .setAccessPort(config.port) | .setAccessPort(config.port) | ||||
| .start(); | .start(); | ||||
| Show All 14 Lines | var send_server = net.createServer(function(socket) { | ||||
| socket.on('timeout', function() { | socket.on('timeout', function() { | ||||
| debug.log('<%s> Timed Out', listener.getDescription()); | debug.log('<%s> Timed Out', listener.getDescription()); | ||||
| }); | }); | ||||
| socket.on('end', function() { | socket.on('end', function() { | ||||
| debug.log('<%s> Ended Connection', listener.getDescription()); | debug.log('<%s> Ended Connection', listener.getDescription()); | ||||
| }); | }); | ||||
| socket.on('error', function (e) { | socket.on('error', function(e) { | ||||
| debug.log('<%s> Error: %s', listener.getDescription(), e); | debug.log('<%s> Error: %s', listener.getDescription(), e); | ||||
| }); | }); | ||||
| }).listen(config.port); | }).listen(config.port); | ||||
| var messages_out = 0; | var messages_out = 0; | ||||
| var messages_in = 0; | var messages_in = 0; | ||||
| var start_time = new Date().getTime(); | var start_time = new Date().getTime(); | ||||
| var receive_server = http.createServer(function(request, response) { | var receive_server = http.createServer(function(request, response) { | ||||
| response.writeHead(200, {'Content-Type' : 'text/plain'}); | |||||
| // Publishing a notification. | // Publishing a notification. | ||||
| if (request.method == 'POST') { | if (request.method == 'POST') { | ||||
| var body = ''; | var body = ''; | ||||
| request.on('data', function (data) { | request.on('data', function(data) { | ||||
| body += data; | body += data; | ||||
| }); | }); | ||||
| request.on('end', function () { | request.on('end', function() { | ||||
| ++messages_in; | try { | ||||
| var msg = JSON.parse(body); | var msg = JSON.parse(body); | ||||
| debug.log('notification: ' + JSON.stringify(msg)); | debug.log('notification: ' + JSON.stringify(msg)); | ||||
| broadcast(msg.data); | ++messages_in; | ||||
| broadcast(msg); | |||||
epriestley: This is a functional change (msg.data -> msg) -- is that OK? | |||||
| response.writeHead(200, {'Content-Type': 'text/plain'}); | |||||
| } catch (err) { | |||||
| response.statusCode = 400; | |||||
| response.write('400 Bad Request'); | |||||
| } finally { | |||||
| response.end(); | response.end(); | ||||
| } | |||||
| }); | }); | ||||
| } else if (request.url == '/status/') { | } else if (request.url == '/status/') { | ||||
| request.on('data', function(data) { | request.on('data', function(data) { | ||||
| // We just ignore the request data, but newer versions of Node don't | // We just ignore the request data, but newer versions of Node don't | ||||
| // get to 'end' if we don't process the data. See T2953. | // get to 'end' if we don't process the data. See T2953. | ||||
| }); | }); | ||||
| request.on('end', function() { | request.on('end', function() { | ||||
| var status = { | var status = { | ||||
| 'uptime': (new Date().getTime() - start_time), | 'uptime': (new Date().getTime() - start_time), | ||||
| 'clients.active': clients.getActiveListenerCount(), | 'clients.active': clients.getActiveListenerCount(), | ||||
| 'clients.total': clients.getTotalListenerCount(), | 'clients.total': clients.getTotalListenerCount(), | ||||
| 'messages.in': messages_in, | 'messages.in': messages_in, | ||||
| 'messages.out': messages_out, | 'messages.out': messages_out, | ||||
| 'log': config.log, | 'log': config.log, | ||||
| 'version': 5 | 'version': 6 | ||||
| }; | }; | ||||
| response.writeHead(200, {'Content-Type': 'text/plain'}); | |||||
| response.write(JSON.stringify(status)); | response.write(JSON.stringify(status)); | ||||
| response.end(); | response.end(); | ||||
| }); | }); | ||||
| } else { | } else { | ||||
| response.statusCode = 400; | response.statusCode = 400; | ||||
| response.write('400 Bad Request'); | response.write('400 Bad Request'); | ||||
| response.end(); | response.end(); | ||||
| } | } | ||||
| Show All 26 Lines | |||||
This is a functional change (msg.data -> msg) -- is that OK?