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');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
// set up log file
|
|
|
|
logfile = fs.createWriteStream('/var/log/aphlict.log',
|
|
|
|
{ flags: 'a',
|
|
|
|
encoding: null,
|
|
|
|
mode: 0666 });
|
|
|
|
logfile.write('----- ' + (new Date()).toLocaleString() + ' -----\n');
|
|
|
|
|
|
|
|
function log(str) {
|
|
|
|
console.log(str);
|
|
|
|
logfile.write(str + '\n');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
function getFlashPolicy() {
|
|
|
|
return [
|
|
|
|
'<?xml version="1.0"?>',
|
|
|
|
'<!DOCTYPE cross-domain-policy SYSTEM ' +
|
|
|
|
'"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">',
|
|
|
|
'<cross-domain-policy>',
|
2012-03-07 05:14:03 +01:00
|
|
|
'<allow-access-from domain="*" to-ports="2600"/>',
|
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
|
|
|
'</cross-domain-policy>'
|
2012-06-12 02:49:32 +02:00
|
|
|
].join('\n');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
net.createServer(function(socket) {
|
|
|
|
socket.on('data', function() {
|
|
|
|
socket.write(getFlashPolicy() + '\0');
|
|
|
|
});
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
socket.on('error', function (e) {
|
|
|
|
log('Error in policy server: ' + 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
|
|
|
}).listen(843);
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-12 02:49:32 +02:00
|
|
|
function write_json(socket, data) {
|
|
|
|
var serial = JSON.stringify(data);
|
|
|
|
var length = Buffer.byteLength(serial, 'utf8');
|
|
|
|
length = length.toString();
|
|
|
|
while (length.length < 8) {
|
|
|
|
length = '0' + length;
|
|
|
|
}
|
|
|
|
socket.write(length + serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var clients = {};
|
|
|
|
var current_connections = 0;
|
|
|
|
// According to the internet up to 2^53 can
|
|
|
|
// be stored in javascript, this is less than that
|
|
|
|
var MAX_ID = 9007199254740991;//2^53 -1
|
|
|
|
|
|
|
|
// If we get one connections per millisecond this will
|
|
|
|
// be fine as long as someone doesn't maintain a
|
|
|
|
// connection for longer than 6854793 years. If
|
|
|
|
// you want to write something pretty be my guest
|
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
|
|
|
function generate_id() {
|
|
|
|
if (typeof generate_id.current_id == 'undefined'
|
|
|
|
|| generate_id.current_id > MAX_ID) {
|
|
|
|
generate_id.current_id = 0;
|
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
|
|
|
return generate_id.current_id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var send_server = net.createServer(function(socket) {
|
|
|
|
var client_id = generate_id();
|
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
|
|
|
|
|
|
|
socket.on('connect', function() {
|
2012-06-12 02:49:32 +02:00
|
|
|
clients[client_id] = socket;
|
|
|
|
current_connections++;
|
|
|
|
log(client_id + ': connected\t\t('
|
|
|
|
+ current_connections + ' current connections)');
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('close', function() {
|
|
|
|
delete clients[client_id];
|
|
|
|
current_connections--;
|
|
|
|
log(client_id + ': closed\t\t('
|
|
|
|
+ current_connections + ' current connections)');
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('timeout', function() {
|
|
|
|
log(client_id + ': timed out!');
|
|
|
|
});
|
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() {
|
|
|
|
log(client_id + ': ended the connection');
|
|
|
|
// node automatically closes half-open connections
|
|
|
|
});
|
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) {
|
|
|
|
console.log('Uncaught error in send server: ' + 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
|
|
|
});
|
|
|
|
}).listen(2600);
|
2012-06-12 02:49:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var receive_server = http.createServer(function(request, response) {
|
|
|
|
response.writeHead(200, {'Content-Type' : 'text/plain'});
|
|
|
|
|
|
|
|
if (request.method == 'POST') { // Only pay attention to POST requests
|
|
|
|
var body = '';
|
|
|
|
|
|
|
|
request.on('data', function (data) {
|
|
|
|
body += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on('end', function () {
|
|
|
|
var data = querystring.parse(body);
|
|
|
|
log('notification: ' + JSON.stringify(data));
|
|
|
|
broadcast(data);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).listen(22281, '127.0.0.1');
|
|
|
|
|
|
|
|
function broadcast(data) {
|
|
|
|
for(var client_id in clients) {
|
|
|
|
try {
|
|
|
|
write_json(clients[client_id], data);
|
|
|
|
log(' wrote to client ' + client_id);
|
|
|
|
} catch (error) {
|
|
|
|
delete clients[client_id];
|
|
|
|
current_connections--;
|
|
|
|
log(' ERROR: could not write to client ' + client_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|