| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Amon2::Plugin::Web::Streaming; | 
| 2 | 2 |  |  | 2 |  | 985 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 60 |  | 
| 3 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 45 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use utf8; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 12 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 41 | use Amon2::Util; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 99 |  | 
| 7 | 2 |  |  | 2 |  | 863 | use Amon2::Web::Response::Callback; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 770 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub init { | 
| 10 | 2 |  |  | 2 | 0 | 8 | my ($class, $c, $conf) = @_; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 2 |  | 33 |  |  | 22 | Amon2::Util::add_method(ref $c || $c, 'streaming', \&_streaming); | 
| 13 | 2 |  | 33 |  |  | 13 | Amon2::Util::add_method(ref $c || $c, 'streaming_json', \&_streaming_json); | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub _streaming { | 
| 17 | 1 |  |  | 1 |  | 17 | my ($self, $code) = @_; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | return Amon2::Web::Response::Callback->new( | 
| 20 |  |  |  |  |  |  | code => sub { | 
| 21 | 1 |  |  | 1 |  | 4 | $code->(@_); | 
| 22 |  |  |  |  |  |  | } | 
| 23 | 1 |  |  |  |  | 10 | ); | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub _streaming_json { | 
| 27 | 0 |  |  | 0 |  |  | my ($self, $code) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | return Amon2::Web::Response::Callback->new( | 
| 30 |  |  |  |  |  |  | code => sub { | 
| 31 | 0 |  |  | 0 |  |  | my $respond = shift; | 
| 32 | 0 |  |  |  |  |  | my $writer = $respond->([200, ['Content-Type' => 'application/json;charset=utf-8']]); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | my $longpoll_ctx = Amon2::Plugin::Web::Streaming::Writer->new( | 
| 35 |  |  |  |  |  |  | $self, | 
| 36 |  |  |  |  |  |  | $writer | 
| 37 |  |  |  |  |  |  | ); | 
| 38 | 0 |  |  |  |  |  | $code->($longpoll_ctx); | 
| 39 |  |  |  |  |  |  | } | 
| 40 | 0 |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | package Amon2::Plugin::Web::Streaming::Writer; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub new { | 
| 46 | 0 |  |  | 0 |  |  | my ($class, $c, $writer) = @_; | 
| 47 | 0 |  |  |  |  |  | bless {ctx => $c, writer => $writer}, $class; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub write_json { | 
| 51 | 0 |  |  | 0 |  |  | my ($self, $data) = @_; | 
| 52 | 0 |  |  |  |  |  | my $json = $self->{ctx}->render_json($data)->content; | 
| 53 | 0 |  |  |  |  |  | $self->{writer}->write($json); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub close { | 
| 57 | 0 |  |  | 0 |  |  | my ($self) = @_; | 
| 58 | 0 |  |  |  |  |  | $self->{writer}->close(); | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | 1; | 
| 62 |  |  |  |  |  |  | __END__ | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =head1 NAME | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | Amon2::Plugin::Web::Streaming - streaming support for Amon2 | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | use Amon2::Lite; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | __PACKAGE__->load_plugin(qw/Web::Streaming/); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | any '/poll' => sub { | 
| 75 |  |  |  |  |  |  | my $c = shift; | 
| 76 |  |  |  |  |  |  | return $c->streaming(sub { | 
| 77 |  |  |  |  |  |  | my $respond = shift; | 
| 78 |  |  |  |  |  |  | ...; | 
| 79 |  |  |  |  |  |  | $respond->write([200, [], ['OK']]); | 
| 80 |  |  |  |  |  |  | }); | 
| 81 |  |  |  |  |  |  | }; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | any '/poll_json' => sub { | 
| 84 |  |  |  |  |  |  | my $c = shift; | 
| 85 |  |  |  |  |  |  | return $c->streaming_json(sub { | 
| 86 |  |  |  |  |  |  | my $writer = shift; | 
| 87 |  |  |  |  |  |  | ...; | 
| 88 |  |  |  |  |  |  | $writer->write_json(+{ }); | 
| 89 |  |  |  |  |  |  | $writer->close; | 
| 90 |  |  |  |  |  |  | }); | 
| 91 |  |  |  |  |  |  | }; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | This is an Amon2 plugin to support streaming. | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | You MUST use the HTTP server supporting psgi.streaming. | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 EXPORTED METHODS | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =over 4 | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =item $c->streaming($code); | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | You can return delayed response for PSGI spec. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Argument for $code is C<< $respond >>. It's same as a argument for PSGI callback. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =item $c->streaming_json($code); | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | It's a short hand utility to publish streaming JSON. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | The argument is instance of Amon2::Plugin::Web::Streaming::Writer. | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =back | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 Amon2::Plugin::Streaming::Writer METHODS | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =over 4 | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =item new | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Do not create the instance directly. | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =item $writer->write_json($data) | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | Write a $data as JSON for the socket. | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =item $writer->close() | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Close the socket. | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =back | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | L<PSGI> | 
| 140 |  |  |  |  |  |  |  |