var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
// The current contents of the file to hand out, and its last modified time.
-var json_contents = undefined;
-var json_contents_gz = undefined;
-var json_last_modified = undefined;
+var json = undefined;
// The list of clients that are waiting for new data to show up.
// Uniquely keyed by request_id so that we can take them out of
// ourselves, so some external log-tailing daemon needs to tell us.
var viewer_count_override = undefined;
+var replace_json = function(new_json_contents, mtime) {
+ var new_json = {
+ parsed: JSON.parse(new_json_contents),
+ plain: new_json_contents,
+ last_modified: mtime
+ };
+
+ // gzip the new version, and put it into place.
+ zlib.gzip(new_json_contents, function(err, buffer) {
+ if (err) throw err;
+
+ new_json.gzip = buffer;
+ json = new_json;
+
+ // Finally, wake up any sleeping clients.
+ possibly_wakeup_clients();
+ });
+}
+
var reread_file = function(event, filename) {
if (filename != path.basename(json_filename)) {
return;
if (err) throw err;
fs.close(fd, function() {
var new_json_contents = buffer.toString('utf8', 0, bytesRead);
- zlib.gzip(new_json_contents, function(err, buffer) {
- if (err) throw err;
- json_contents = new_json_contents;
- json_contents_gz = buffer;
- json_last_modified = st.mtime.getTime();
- possibly_wakeup_clients();
- });
+ replace_json(new_json_contents, st.mtime.getTime());
});
});
});
var send_json = function(response, accept_gzip, num_viewers) {
var headers = {
'Content-Type': 'text/json',
- 'X-Remoteglot-Last-Modified': json_last_modified,
+ 'X-Remoteglot-Last-Modified': json.last_modified,
'X-Remoteglot-Num-Viewers': num_viewers,
'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
if (accept_gzip) {
headers['Content-Encoding'] = 'gzip';
response.writeHead(200, headers);
- response.write(json_contents_gz);
+ response.write(json.gzip);
} else {
response.writeHead(200, headers);
- response.write(json_contents);
+ response.write(json.text);
}
response.end();
}
// If we already have something newer than what the user has,
// just send it out and be done with it.
- if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) {
+ if (json !== undefined && (!ims || json.last_modified > ims)) {
send_json(response, accept_gzip, count_viewers());
return;
}