Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/aphlict_server.js
| /** | /** | ||||
| * Notification server. Launch with: | * Notification server. Launch with: | ||||
| * | * | ||||
| * sudo node aphlict_server.js --user=aphlict | * sudo node aphlict_server.js --user=aphlict | ||||
| * | * | ||||
| * You can also specify `port`, `admin`, `host` and `log`. | * You can also specify `port`, `admin`, `host` and `log`. | ||||
| */ | */ | ||||
| var JX = require('./lib/javelin').JX; | var JX = require('./lib/javelin').JX; | ||||
| JX.require('lib/AphlictFlashPolicyServer', __dirname); | JX.require('lib/AphlictFlashPolicyServer', __dirname); | ||||
| JX.require('lib/AphlictListenerList', __dirname); | JX.require('lib/AphlictListenerList', __dirname); | ||||
| JX.require('lib/AphlictLog', __dirname); | JX.require('lib/AphlictLog', __dirname); | ||||
| var debug = new JX.AphlictLog() | |||||
| .addConsole(console); | |||||
| var clients = new JX.AphlictListenerList(); | |||||
| var config = parse_command_line_arguments(process.argv); | |||||
| if (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' | ||||
| }; | }; | ||||
| Show All 11 Lines | function parse_command_line_arguments(argv) { | ||||
| } | } | ||||
| 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; | ||||
| } | } | ||||
| var debug = new JX.AphlictLog() | |||||
| .addConsole(console); | |||||
| var clients = new JX.AphlictListenerList(); | |||||
| var config = parse_command_line_arguments(process.argv); | |||||
| if (config.logfile) { | |||||
| debug.addLogfile(config.logfile); | |||||
| } | |||||
| 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); | ||||
| ▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | |||||
| }).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(); | ||||
| function transmit(msg) { | |||||
| var listeners = clients.getListeners().filter(function(client) { | |||||
| return client.isSubscribedToAny(msg.subscribers); | |||||
| }); | |||||
| for (var i = 0; i < listeners.length; i++) { | |||||
| var listener = listeners[i]; | |||||
| try { | |||||
| listener.writeMessage(msg); | |||||
| ++messages_out; | |||||
| debug.log('<%s> Wrote Message', listener.getDescription()); | |||||
| } catch (error) { | |||||
| clients.removeListener(listener); | |||||
| debug.log('<%s> Write Error: %s', listener.getDescription(), error); | |||||
| } | |||||
| } | |||||
| } | |||||
| http.createServer(function(request, response) { | http.createServer(function(request, response) { | ||||
| // Publishing a notification. | // Publishing a notification. | ||||
| if (request.url == '/') { | if (request.url == '/') { | ||||
| 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; | ||||
| ▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | if (request.url == '/') { | ||||
| }); | }); | ||||
| } else { | } else { | ||||
| response.statusCode = 404; | response.statusCode = 404; | ||||
| response.write('404 Not Found\n'); | response.write('404 Not Found\n'); | ||||
| response.end(); | response.end(); | ||||
| } | } | ||||
| }).listen(config.admin, config.host); | }).listen(config.admin, config.host); | ||||
| function transmit(msg) { | |||||
| var listeners = clients.getListeners().filter(function(client) { | |||||
| return client.isSubscribedToAny(msg.subscribers); | |||||
| }); | |||||
| for (var i = 0; i < listeners.length; i++) { | |||||
| var listener = listeners[i]; | |||||
| try { | |||||
| listener.writeMessage(msg); | |||||
| ++messages_out; | |||||
| debug.log('<%s> Wrote Message', listener.getDescription()); | |||||
| } catch (error) { | |||||
| clients.removeListener(listener); | |||||
| debug.log('<%s> Write Error: %s', listener.getDescription(), error); | |||||
| } | |||||
| } | |||||
| } | |||||
| // If we're configured to drop permissions, get rid of them now that we've | // If we're configured to drop permissions, get rid of them now that we've | ||||
| // bound to the ports we need and opened logfiles. | // bound to the ports we need and opened logfiles. | ||||
| if (config.user) { | if (config.user) { | ||||
| process.setuid(config.user); | process.setuid(config.user); | ||||
| } | } | ||||
| debug.log('Started Server (PID %d)', process.pid); | debug.log('Started Server (PID %d)', process.pid); | ||||