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');
9 var path = require('path');
12 var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
14 // The current contents of the file to hand out, and its last modified time.
15 var json_contents = undefined;
16 var json_last_modified = undefined;
18 // The list of clients that are waiting for new data to show up,
19 // and their associated timers. Uniquely keyed by request_id
20 // so that we can take them out of the queue if they time out.
21 var sleeping_clients = {};
24 // List of when clients were last seen, keyed by their unique ID.
25 // Used to show a viewer count to the user.
26 var last_seen_clients = {};
28 var reread_file = function(event, filename) {
29 if (filename != path.basename(json_filename)) {
32 console.log("Rereading " + json_filename);
33 fs.open(json_filename, 'r+', function(err, fd) {
35 fs.fstat(fd, function(err, st) {
37 var buffer = new Buffer(1048576);
38 fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
40 fs.close(fd, function() {
41 json_contents = buffer.toString('utf8', 0, bytesRead);
42 json_last_modified = st.mtime.getTime();
43 possibly_wakeup_clients();
49 var possibly_wakeup_clients = function() {
50 for (var i in sleeping_clients) {
51 clearTimeout(sleeping_clients[i].timer);
52 send_json(sleeping_clients[i].response);
54 sleeping_clients = {};
56 var send_404 = function(response) {
57 response.writeHead(404, {
58 'Content-Type': 'text/plain',
60 response.write('Something went wrong. Sorry.');
63 var send_json = function(response) {
64 response.writeHead(200, {
65 'Content-Type': 'text/json',
66 'X-Remoteglot-Last-Modified': json_last_modified,
67 'X-Remoteglot-Num-Viewers': count_viewers(),
68 'Access-Control-Allow-Origin': 'http://analysis.sesse.net',
69 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
70 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
72 response.write(json_contents);
75 var timeout_client = function(client) {
76 send_json(client.response);
77 delete sleeping_clients[client.request_id];
79 var count_viewers = function() {
80 var now = (new Date).getTime();
82 // Go through and remove old viewers, and count them at the same time.
83 var new_last_seen_clients = {};
85 for (var unique in last_seen_clients) {
86 if (now - last_seen_clients[unique] < 60000) {
88 new_last_seen_clients[unique] = last_seen_clients[unique];
91 last_seen_clients = new_last_seen_clients;
95 // Set up a watcher to catch changes to the file, then do an initial read
96 // to make sure we have a copy.
97 fs.watch(path.dirname(json_filename), reread_file);
98 reread_file(null, path.basename(json_filename));
100 http.createServer(function(request, response) {
101 var u = url.parse(request.url, true);
102 var ims = (u.query)['ims'];
103 var unique = (u.query)['unique'];
105 console.log((new Date).getTime()*1e-3 + " " + request.url);
107 if (u.pathname !== '/analysis.pl') {
108 // This is not the request you are looking for.
114 last_seen_clients[unique] = (new Date).getTime();
117 // If we already have something newer than what the user has,
118 // just send it out and be done with it.
119 if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) {
124 // OK, so we need to hang until we have something newer.
125 // Put the user on the wait list; if we don't get anything
126 // in 30 seconds, though, we'll send something anyway.
128 client.response = response;
129 client.timer = setTimeout(function() { timeout_client(client); }, 30000);
130 client.request_id = request_id;
131 sleeping_clients[request_id++] = client;