10 our $json_filename = "/srv/analysis.sesse.net/www/analysis.json";
12 my $cv = AnyEvent->condvar;
15 my $inotify = Linux::Inotify2->new;
16 $inotify->watch($json_filename, IN_MODIFY, sub {
21 my $inotify_w = AnyEvent->io (
22 fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll }
24 my $wait = AnyEvent->timer (
26 cb => sub { $cv->send; },
29 my $unique = $cgi->param('unique');
30 our $num_viewers = count_viewers($unique);
32 # Yes, this is reinventing If-Modified-Since, but browsers are so incredibly
33 # unpredictable on this, so blargh.
35 if (defined($cgi->param('ims')) && $cgi->param('ims') ne '') {
36 $ims = $cgi->param('ims');
38 my $time = (stat($json_filename))[9];
40 # If we have something that's modified since IMS, send it out at once
46 # If not, wait, then send.
53 my $share = IPC::ShareLite->new(
58 ) or die "IPC::ShareLite: $!";
59 $share->lock(IPC::ShareLite::LOCK_EX);
62 $viewers = Storable::thaw($share->fetch());
64 $viewers->{$unique} = time;
66 # Go through and remove old viewers, and count them at the same time.
68 while (my ($key, $value) = each %$viewers) {
69 if ($time - $value > 60) {
70 delete $viewers->{$key};
76 $share->store(Storable::freeze($viewers));
83 open my $fh, "<", $json_filename
84 or die "$json_filename: $!";
90 my $time = (stat($fh))[9];
93 print CGI->header(-type=>'text/json',
94 -x_remoteglot_last_modified=>$time,
95 -x_remoteglot_num_viewers=>$num_viewers,
96 -access_control_allow_origin=>'http://analysis.sesse.net',
97 -access_control_expose_headers=>'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',