1 // node.js version of analysis.pl; hopefully scales a bit better
2 // for this specific kind of task.
5 var http = require('http');
6 var fs = require('fs');
7 var url = require('url');
8 var querystring = require('querystring');
11 var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
13 // The current contents of the file to hand out, and its last modified time.
14 var json_contents = null;
15 var json_last_modified = null;
17 // The list of clients that are waiting for new data to show up,
18 // and their associated timers. Uniquely keyed by request_id
19 // so that we can take them out of the queue if they time out.
20 var sleeping_clients = {};
23 // List of when clients were last seen, keyed by their unique ID.
24 // Used to show a viewer count to the user.
25 var last_seen_clients = {};
27 var reread_file = function() {
28 fs.open(json_filename, 'r+', function(err, fd) {
30 fs.fstat(fd, function(err, st) {
32 var buffer = new Buffer(1048576);
33 fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
35 fs.close(fd, function() {
36 json_contents = buffer.toString('utf8', 0, bytesRead);
37 json_last_modified = st.mtime.getTime();
38 possibly_wakeup_clients();
44 var possibly_wakeup_clients = function() {
45 for (var i in sleeping_clients) {
46 clearTimeout(sleeping_clients[i].timer);
47 send_json(sleeping_clients[i].response);
49 sleeping_clients = {};
51 var send_404 = function(response) {
52 response.writeHead(404, {
53 'Content-Type': 'text/plain',
55 response.write('Something went wrong. Sorry.');
58 var send_json = function(response) {
59 response.writeHead(200, {
60 'Content-Type': 'text/json',
61 'X-Remoteglot-Last-Modified': json_last_modified,
62 'X-Remoteglot-Num-Viewers': count_viewers(),
63 'Access-Control-Allow-Origin': 'http://analysis.sesse.net',
64 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
65 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
67 response.write(json_contents);
70 var timeout_client = function(client) {
71 send_json(client.response);
72 delete sleeping_clients[client.request_id];
74 var count_viewers = function() {
75 var now = (new Date).getTime();
77 // Go through and remove old viewers, and count them at the same time.
78 var new_last_seen_clients = {};
80 for (var unique in last_seen_clients) {
81 if (now - last_seen_clients[unique] < 60000) {
83 new_last_seen_clients[unique] = last_seen_clients[unique];
86 last_seen_clients = new_last_seen_clients;
90 // Set up a watcher to catch changes to the file, then do an initial read
91 // to make sure we have a copy.
92 fs.watch(json_filename, reread_file);
95 http.createServer(function(request, response) {
96 var u = url.parse(request.url, true);
97 var ims = (u.query)['ims'];
98 var unique = (u.query)['unique'];
100 console.log((new Date).getTime()*1e-3 + " " + request.url);
102 if (u.pathname !== '/analysis.pl') {
103 // This is not the request you are looking for.
109 last_seen_clients[unique] = (new Date).getTime();
112 // If we already have something newer than what the user has,
113 // just send it out and be done with it.
114 if (!ims || json_last_modified > ims) {
119 // OK, so we need to hang until we have something newer.
120 // Put the user on the wait list; if we don't get anything
121 // in 30 seconds, though, we'll send something anyway.
123 client.response = response;
124 client.timer = setTimeout(function() { timeout_client(client); }, 30000);
125 client.request_id = request_id;
126 sleeping_clients[request_id++] = client;