2012-06-14 15:12:54 +02:00
|
|
|
/**
|
|
|
|
* Notification server. Launch with:
|
|
|
|
*
|
|
|
|
* sudo node aphlict_server.js --user=aphlict
|
|
|
|
*
|
2012-12-20 00:19:23 +01:00
|
|
|
* You can also specify `port`, `admin`, `host` and `log`.
|
2012-06-14 15:12:54 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-18 01:00:01 +01:00
|
|
|
var JX = require('./lib/javelin').JX;
|
|
|
|
|
2014-02-18 01:01:09 +01:00
|
|
|
JX.require('lib/AphlictFlashPolicyServer', __dirname);
|
2014-02-18 01:00:51 +01:00
|
|
|
JX.require('lib/AphlictListenerList', __dirname);
|
|
|
|
JX.require('lib/AphlictLog', __dirname);
|
|
|
|
|
|
|
|
var debug = new JX.AphlictLog()
|
|
|
|
.addConsole(console);
|
|
|
|
|
|
|
|
var clients = new JX.AphlictListenerList();
|
2014-02-18 01:00:01 +01:00
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
var config = parse_command_line_arguments(process.argv);
|
|
|
|
|
2014-02-18 01:00:51 +01:00
|
|
|
if (config.logfile) {
|
|
|
|
debug.addLogfile(config.logfile);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
function parse_command_line_arguments(argv) {
|
|
|
|
var config = {
|
|
|
|
port : 22280,
|
|
|
|
admin : 22281,
|
2012-12-20 00:19:23 +01:00
|
|
|
host : '127.0.0.1',
|
2012-06-14 15:12:54 +02:00
|
|
|
user : null,
|
|
|
|
log: '/var/log/aphlict.log'
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var ii = 2; ii < argv.length; ii++) {
|
|
|
|
var arg = argv[ii];
|
|
|
|
var matches = arg.match(/^--([^=]+)=(.*)$/);
|
|
|
|
if (!matches) {
|
|
|
|
throw new Error("Unknown argument '"+arg+"'!");
|
|
|
|
}
|
|
|
|
if (!(matches[1] in config)) {
|
|
|
|
throw new Error("Unknown argument '"+matches[1]+"'!");
|
|
|
|
}
|
|
|
|
config[matches[1]] = matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
config.port = parseInt(config.port, 10);
|
|
|
|
config.admin = parseInt(config.admin, 10);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2013-05-19 02:04:22 +02:00
|
|
|
if (process.getuid() !== 0) {
|
2012-06-14 15:12:54 +02:00
|
|
|
console.log(
|
|
|
|
"ERROR: "+
|
|
|
|
"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 "+
|
|
|
|
"less-privileged user after binding if you pass a user in the command "+
|
|
|
|
"line arguments with '--user=alincoln'.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
var net = require('net');
|
2012-06-12 02:49:32 +02:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
|
2012-06-20 22:20:47 +02:00
|
|
|
process.on('uncaughtException', function (err) {
|
2014-02-18 01:01:09 +01:00
|
|
|
debug.log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err);
|
2012-06-20 22:20:47 +02:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2014-02-18 01:01:09 +01:00
|
|
|
var flash_server = new JX.AphlictFlashPolicyServer()
|
|
|
|
.setDebugLog(debug)
|
|
|
|
.setAccessPort(config.port)
|
|
|
|
.start();
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
|
|
|
|
|
2012-06-12 02:49:32 +02:00
|
|
|
var send_server = net.createServer(function(socket) {
|
2014-02-18 01:00:51 +01:00
|
|
|
var listener = clients.addListener(socket);
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('<%s> Connected from %s',
|
|
|
|
listener.getDescription(),
|
|
|
|
socket.remoteAddress);
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
socket.on('close', function() {
|
2014-02-18 01:00:51 +01:00
|
|
|
clients.removeListener(listener);
|
|
|
|
debug.log('<%s> Disconnected', listener.getDescription());
|
2012-06-12 02:49:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('timeout', function() {
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('<%s> Timed Out', listener.getDescription());
|
2012-06-12 02:49:32 +02:00
|
|
|
});
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
|
2012-06-12 02:49:32 +02:00
|
|
|
socket.on('end', function() {
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('<%s> Ended Connection', listener.getDescription());
|
2012-06-12 02:49:32 +02:00
|
|
|
});
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
|
2012-06-12 02:49:32 +02:00
|
|
|
socket.on('error', function (e) {
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('<%s> Error: %s', listener.getDescription(), e);
|
Aphlict, simple notification server
Summary:
This is purely a prototype at the moment, but the basic functionality sort of
works.
I'm not sure how far I want to go with this but I think we might be able to get
somewhere without it being gross.
The idea here is to build a notification server WITHOUT using Comet, since Comet
is extremely difficult and complicated.
Instead, I use Flash on the client. LocalConnection allows flash instances to
talk to each other and connect() can be used as a locking primitive. This allows
all the instances to elect a master instance in a race-safe way. The master is
responsible for opening a single connnection to the server.
On the server, I use Node.js since PHP is pretty unsuitable for this task.
See Github Issue #3: https://github.com/facebook/phabricator/issues/3
One thing I need to figure out next is if I can reasonably do SSL/TSL over Flash
(it looks like I can, in theory, with the as3crypto library) or if the server
needs to just send down version information and trigger a separate Ajax call on
the client.
Test Plan:
Created a client pool and connected it to the server, with election and failover
apparently working correctly.
Reviewed By: aran
Reviewers: Girish, aran, jungejason, tuomaspelkonen, davidrecordon
Commenters: Girish, davidrecordon
CC: aran, epriestley, Girish, davidrecordon
Differential Revision: 284
2011-05-15 23:05:02 +02:00
|
|
|
});
|
2014-02-18 01:00:51 +01:00
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
}).listen(config.port);
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
var messages_out = 0;
|
|
|
|
var messages_in = 0;
|
|
|
|
var start_time = new Date().getTime();
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
var receive_server = http.createServer(function(request, response) {
|
|
|
|
response.writeHead(200, {'Content-Type' : 'text/plain'});
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
// Publishing a notification.
|
|
|
|
if (request.method == 'POST') {
|
2012-06-12 02:49:32 +02:00
|
|
|
var body = '';
|
|
|
|
|
|
|
|
request.on('data', function (data) {
|
|
|
|
body += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on('end', function () {
|
2012-06-14 15:12:54 +02:00
|
|
|
++messages_in;
|
|
|
|
|
2012-06-12 02:49:32 +02:00
|
|
|
var data = querystring.parse(body);
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('notification: ' + JSON.stringify(data));
|
2012-06-12 02:49:32 +02:00
|
|
|
broadcast(data);
|
|
|
|
response.end();
|
|
|
|
});
|
2012-06-14 15:12:54 +02:00
|
|
|
} else if (request.url == '/status/') {
|
2013-04-12 00:45:50 +02:00
|
|
|
request.on('data', function(data) {
|
|
|
|
// 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.
|
|
|
|
});
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
request.on('end', function() {
|
|
|
|
var status = {
|
|
|
|
'uptime': (new Date().getTime() - start_time),
|
2014-02-18 01:00:51 +01:00
|
|
|
'clients.active': clients.getActiveListenerCount(),
|
|
|
|
'clients.total': clients.getTotalListenerCount(),
|
2012-06-14 15:12:54 +02:00
|
|
|
'messages.in': messages_in,
|
|
|
|
'messages.out': messages_out,
|
2014-02-18 00:59:39 +01:00
|
|
|
'log': config.log,
|
2014-02-18 01:00:01 +01:00
|
|
|
'version': 3
|
2012-06-14 15:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
response.write(JSON.stringify(status));
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
response.statusCode = 400;
|
|
|
|
response.write('400 Bad Request');
|
|
|
|
response.end();
|
2012-06-12 02:49:32 +02:00
|
|
|
}
|
2012-06-14 15:12:54 +02:00
|
|
|
|
2012-12-20 00:19:23 +01:00
|
|
|
}).listen(config.admin, config.host);
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
function broadcast(data) {
|
2014-02-18 01:00:51 +01:00
|
|
|
var listeners = clients.getListeners();
|
|
|
|
for (var id in listeners) {
|
|
|
|
var listener = listeners[id];
|
2012-06-12 02:49:32 +02:00
|
|
|
try {
|
2014-02-18 01:00:51 +01:00
|
|
|
listener.writeMessage(data);
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
++messages_out;
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('<%s> Wrote Message', listener.getDescription());
|
2012-06-12 02:49:32 +02:00
|
|
|
} catch (error) {
|
2014-02-18 01:00:51 +01:00
|
|
|
clients.removeListener(listener);
|
|
|
|
debug.log('<%s> Write Error: %s', error);
|
2012-06-12 02:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:12:54 +02:00
|
|
|
// If we're configured to drop permissions, get rid of them now that we've
|
|
|
|
// bound to the ports we need and opened logfiles.
|
|
|
|
if (config.user) {
|
|
|
|
process.setuid(config.user);
|
|
|
|
}
|
|
|
|
|
2014-02-18 01:00:51 +01:00
|
|
|
debug.log('Started Server (PID %d)', process.pid);
|