/usr/share/perl5/App/Alice/Stream.pm is in alice 0.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | package App::Alice::Stream;
use JSON;
use Time::HiRes qw/time/;
use Try::Tiny;
use Any::Moose;
use strict;
use warnings;
has queue => (
is => 'rw',
isa => 'ArrayRef[HashRef]',
default => sub { [] },
);
sub clear_queue {$_[0]->queue([])}
sub enqueue {push @{shift->queue}, @_}
sub queue_empty {return @{$_[0]->queue} == 0}
has [qw/offset last_send start_time/]=> (
is => 'rw',
isa => 'Num',
default => 0,
);
has [qw/delayed started closed/] => (
is => 'rw',
isa => 'Bool',
default => 0,
);
has 'seperator' => (
is => 'ro',
isa => 'Str',
default => 'xalicex',
);
has 'timer' => (
is => 'rw',
);
has 'writer' => (
is => 'rw',
required => 1,
);
has min_bytes => (
is => 'ro',
default => 1024,
);
sub BUILD {
my $self = shift;
my $local_time = time;
my $remote_time = $self->start_time || $local_time;
$self->offset($local_time - $remote_time);
my $writer = $self->writer->(
[200, ['Content-Type' => 'multipart/mixed; boundary='.$self->seperator.'; charset=utf-8']]
);
$self->writer($writer);
$self->_send;
}
sub _send {
my $self = shift;
try { $self->send }
catch { $self->close };
}
sub send {
my ($self, @messages) = @_;
die "Sending on a closed stream" if $self->closed;
$self->enqueue(@messages) if @messages;
return if $self->delayed or $self->queue_empty;
if (my $delay = $self->flooded) {
$self->delay($delay);
return;
}
$self->writer->write( $self->to_string );
$self->flush;
}
sub close {
my $self = shift;
try {$self->writer->write($self->to_string)};
$self->flush;
$self->writer->close;
$self->timer(undef);
$self->closed(1);
}
sub flooded {
my $self = shift;
my $diff = time - $self->last_send;
if ($diff < 0.2) {
return 0.2 - $diff;
}
return 0;
}
sub delay {
my ($self, $delay) = @_;
$self->delayed(1);
$self->timer(AnyEvent->timer(
after => $delay,
cb => sub {
$self->delayed(0);
$self->timer(undef);
$self->_send;
},
));
}
sub flush {
my $self = shift;
$self->clear_queue;
$self->last_send(time);
}
sub to_string {
my $self = shift;
my $output;
if (! $self->started) {
$output .= "--".$self->seperator."\n";
$self->started(1);
}
$output .= to_json({
queue => $self->queue,
time => time - $self->offset,
}, {utf8 => 1});
$output .= "\n--" . $self->seperator . "\n";
$output .= " " x ($self->min_bytes - length $output);
return $output
}
__PACKAGE__->meta->make_immutable;
1;
|