Hippie/Pipe.pm

filter: Statementstmt 30.8% 25 / 81 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 8 / 16 Podpod 66.6% 2 / 3 totaltotal 24.6% 35 / 142 CC 16 SCAR 39.3
CC
16
Cov
22%
call
132.0
get_listener
20.0
prepare_app
6.0
CRAP 50.7
↑ Summary Hippie.pm »
1 package Web::Hippie::Pipe;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.01';
6 1 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::Util::Accessor qw( bus client_mgr );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use AnyMQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=2 SCAR=17.9
Pod documented
15 0 my $self = shift;
16 0 $self->client_mgr({});
17 0 ▶ $self->bus(AnyMQ->new) unless $self->bus;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bus 0 0 0
18 }
19  
20 0 ▶sub call {
Subroutine call: not called CC=11 SCAR=48.8
Pod documented
21 0 my ($self, $env) = @_;
22 0 $env->{'hippie.bus'} = $self->bus;
23  
24 my $client_id = $env->{'hippie.client_id'} ||=
25 0 ▶ Plack::Request->new($env)->param('client_id') || $env->{HTTP_X_HIPPIE_CLIENTID};
Condition0%0/6
$env->{"hippie.client_id"} ||= "Plack::Request"->new($env)->param("client_id") || $env->{"HTTP_X_HIPPIE_CLIENTID"}
l!l&&r!l&&!r
000
'Plack::Request'->new($env)->param('client_id') || $env->{'HTTP_X_HIPPIE_CLIENTID'}
l!l&&r!l&&!r
000
MC/DC0%0/3
$env->{"hippie.client_id"} ||= "Plack::Request"->new($env)->param("client_id") || $env->{"HTTP_X_HIPPIE_CLIENTID"}
$env->{"hippie.client_id"} 'Plack::Request'->new($env)->param('client_id') $env->{'HTTP_X_HIPPIE_CLIENTID'}
Truth table0%0/4
A ||= B || C
A= $env->{"hippie.client_id"}B= 'Plack::Request'->new($env)->param('client_id')C= $env->{'HTTP_X_HIPPIE_CLIENTID'}
ABCresult
1XX1
01X1
0011
0000
26 0 ▶ if ($env->{PATH_INFO} eq '/poll') {
Branch: 3 branches
BranchTrueFalseTotal
if ($env->{'PATH_INFO'} eq '/poll') { } 0 0 0
elsif ($env->{'PATH_INFO'} eq '/init') { } 0 0 0
elsif ($env->{'PATH_INFO'} eq '/error') { } 0 0 0
27 0 my $args = $env->{'hippie.args'};
28  
29 # redirect to poll url again with client_id
30 0 ▶ unless ($client_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_id) 0 0 0
31 0 my $res = Plack::Response->new;
32 0 my $req = Plack::Request->new($env);
33 0 my $uri = $req->uri;
34 0 $uri->path( $uri->path . '/' . $args );
35 0 $uri->query_form(client_id => rand(1));
36 0 $res->redirect($uri);
37 0 return $res->finalize;
38 }
39  
40 # now we are sure there's client_id
41 0 my $sub = $self->get_listener($env);
42  
43 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my $responder = shift;
45 0 my $writer = $responder->
46 ([200,
47 [ 'Content-Type' => 'application/json',
48 'Cache-Control' => 'no-cache, must-revalidate',
49 'Pragma' => 'no-cache',
50 'Expires' => HTTP::Date::time2str(0),
51 'Last-Modified' => HTTP::Date::time2str(time())
52 ]]);
53 0 ▶ $sub->poll_once(sub { $writer->write(JSON::encode_json(\@_));
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 $writer->close });
55 }
56 0 }
57 elsif ($env->{PATH_INFO} eq '/init') {
58 0 ▶ my $h = $env->{'hippie.handle'}
Branch: 1 branch
BranchTrueFalseTotal
my $h = $env->{"hippie.handle"} or return [400, ["Content-Type", "text/plain"], [""]] 0 0 0
59 or return [ '400', [ 'Content-Type' => 'text/plain' ], [ "" ] ];
60  
61 0 my $sub = $self->get_listener($env);
62 0 ▶ if ($env->{'hippie.handle'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"hippie.handle"} and $env->{"hippie.handle"}->isa("Web::Hippie::Handle::WebSocket")) 0 0 0
Condition0%0/3
$env->{"hippie.handle"} and $env->{"hippie.handle"}->isa("Web::Hippie::Handle::WebSocket")
!ll&&!rl&&r
000
MC/DC0%0/2
$env->{"hippie.handle"} and $env->{"hippie.handle"}->isa("Web::Hippie::Handle::WebSocket")
$env->{"hippie.handle"} $env->{"hippie.handle"}->isa("Web::Hippie::Handle::WebSocket")
Truth table0%0/3
A and B
A= $env->{"hippie.handle"}B= $env->{"hippie.handle"}->isa("Web::Hippie::Handle::WebSocket")
ABresult
0X0
100
111
63 $env->{'hippie.handle'}->isa('Web::Hippie::Handle::WebSocket')) {
64 0 $sub->timeout(15);
65 }
66  
67 0 ▶ $sub->on_error(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my ($queue, $error, @msg) = @_;
69 0 $queue->persistent(0);
70 0 $queue->append(@msg);
71 0 });
72 0 ▶ $sub->poll(sub { $h->send_msg($_) for @_ });
Subroutine __ANON__: not called CC=2 SCAR=17.9
73 }
74 elsif ($env->{PATH_INFO} eq '/error') {
75 0 ▶ my $sub = $env->{'hippie.listener'} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $env->{"hippie.listener"} or die 0 0 0
76 # XXX: AnyMQ should provide unpoll method.
77 0 $sub->cv->cb(undef);
78 0 $sub->persistent(0);
79 0 $sub->{timer} = $sub->_reaper;
80 }
81 else {
82 0 $self->get_listener($env);
83 0 return $self->app->($env);
84 }
85 }
86  
87 0 ▶sub get_listener {
Subroutine get_listener: not called CC=4 SCAR=30.0
Pod undocumented
88 0 my ($self, $env) = @_;
89 0 ▶ my $client_id = $env->{'hippie.client_id'} ||= rand(1);
Condition0%0/3
$env->{"hippie.client_id"} ||= rand 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$env->{"hippie.client_id"} ||= rand 1
$env->{"hippie.client_id"} rand 1
Truth table0%0/3
A ||= B
A= $env->{"hippie.client_id"}B= rand 1
ABresult
1X1
011
000
90 0 my $sub = $self->client_mgr->{$client_id};
91  
92 0 ▶ my $new = !$sub || $sub->destroyed;
Condition0%0/3
!$sub || $sub->destroyed
l!l&&r!l&&!r
000
MC/DC0%0/2
!$sub || $sub->destroyed
!$sub $sub->destroyed
Truth table0%0/3
not(A) || B
A= !$subB= $sub->destroyed
ABresult
1X1
011
000
93 0 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 0 0 0
94 0 $sub = $self->client_mgr->{$client_id} = $self->bus->new_listener();
95 0 ▶ $sub->on_timeout(sub { $_[0]->destroyed(1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 $env->{PATH_INFO} = '/error';
97 0 $self->app->($env);
98 0 delete $self->client_mgr->{$client_id};
99 0 });
100 0 $env->{'hippie.listener'} = $sub;
101 # XXX the recycling should be done in anymq
102 0 $env->{PATH_INFO} = '/new_listener';
103 0 $self->app->($env);
104 0 $sub->append({ type => 'hippie.pipe.set_client_id',
105 client_id => $client_id} );
106 }
107  
108 # XXX: callback to verify we have access to this listener.
109 0 $env->{'hippie.listener'} = $sub;
110 0 return $sub;
111  
112 }
113  
114 1 1;
115  
116 =head1 NAME
117  
118 Web::Hippie::Pipe - Persistent Connection Abstraction for Hippie
119  
120 =head1 SYNOPSIS
121  
122 use Plack::Builder;
123 use AnyMQ;
124  
125 builder {
126 mount '/_hippie' => builder {
127 enable "+Web::Hippie";
128 enable "+Web::Hippie::Pipe", bus => AnyMQ->new;
129 sub { my $env = shift;
130 my $bus = $env->{'hippie.bus'}; # AnyMQ bus
131 my $listener = $env->{'hippie.listener'}; # AnyMQ::Queue
132 my $client_id = $env->{'hippie.client_id'}; # client id
133  
134 # Your handler based on PATH_INFO: /new_listener, /error, /message
135 }
136 };
137 mount '/' => my $app;
138 };
139  
140 =head1 DESCRIPTION
141  
142 Web::Hippie::Pipe provides unified bidirectional communication over
143 HTTP via websocket, mxhr, or long-poll, for your C<PSGI> applications.
144  
145 =head1 SEE ALSO
146  
147 L<Web::Hippie>
148  
149 =head1 AUTHOR
150  
151 Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
152  
153 =head1 LICENSE
154  
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself.
157  
158 =head1 SEE ALSO
159  
160 =cut
↑ Summary Hippie.pm »