]> git.sesse.net Git - remoteglot/blob - www/serve-analysis.js
56845eef01f7d0d37391392a1810605441b51dca
[remoteglot] / www / serve-analysis.js
1 // node.js version of analysis.pl; hopefully scales a bit better
2 // for this specific kind of task.
3
4 // Modules.
5 var http = require('http');
6 var fs = require('fs');
7 var url = require('url');
8 var querystring = require('querystring');
9
10 // Constants.
11 var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
12
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;
16
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 = {};
21 var request_id = 0;
22
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 = {};
26
27 var reread_file = function() {
28         fs.open(json_filename, 'r+', function(err, fd) {
29                 if (err) throw err;
30                 fs.fstat(fd, function(err, st) {
31                         if (err) throw err;
32                         var buffer = new Buffer(1048576);
33                         fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
34                                 if (err) throw err;
35                                 fs.close(fd, function() {
36                                         json_contents = buffer.toString('utf8', 0, bytesRead);
37                                         json_last_modified = st.mtime.getTime();
38                                         possibly_wakeup_clients();
39                                 });
40                         });
41                 });
42         });
43 }
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);
48         }
49         sleeping_clients = {};
50 }
51 var send_404 = function(response) {
52         response.writeHead(404, {
53                 'Content-Type': 'text/plain',
54         });
55         response.write('Something went wrong. Sorry.');
56         response.end();
57 }
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',
66         });
67         response.write(json_contents);
68         response.end();
69 }
70 var timeout_client = function(client) {
71         send_json(client.response);
72         delete sleeping_clients[client.request_id];
73 }
74 var count_viewers = function() {
75         var now = (new Date).getTime();
76
77         // Go through and remove old viewers, and count them at the same time.
78         var new_last_seen_clients = {};
79         var num_viewers = 0;
80         for (var unique in last_seen_clients) {
81                 if (now - last_seen_clients[unique] < 60000) {
82                         ++num_viewers;
83                         new_last_seen_clients[unique] = last_seen_clients[unique];
84                 }
85         }
86         last_seen_clients = new_last_seen_clients;
87         return num_viewers;
88 }
89
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);
93 reread_file();
94
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'];
99
100         console.log((new Date).getTime()*1e-3 + " " + request.url);
101
102         if (u.pathname !== '/analysis.pl') {
103                 // This is not the request you are looking for.
104                 send_404(response);
105                 return;
106         }
107
108         if (unique) {
109                 last_seen_clients[unique] = (new Date).getTime();
110         }
111
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) {
115                 send_json(response);
116                 return;
117         }
118
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.
122         var client = {};
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;
127 }).listen(5000);