Server/Bayeux/Message/Meta.pm

filter: Statementstmt 14.1% 21 / 148 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 54.5% 6 / 11 Podpod 100% 5 / 5 totaltotal 12.5% 32 / 256 CC 40 SCAR 64.4
CC
40
Cov
9%
handle
992.0
pre_handle
56.0
new
12.0
CRAP 624.0
↑ Summary Server/Bayeux/Request.pm »
1 package POE::Component::Server::Bayeux::Message::Meta;
2  
3 =head1 NAME
4  
5 POE::Component::Server::Bayeux::Message::Meta - handles /meta/ channels
6  
7 =head1 DESCRIPTION
8  
9 Subclasses L<POE::Component::Server::Bayeux::Message> to implement the /meta/* channels.
10  
11 =cut
12  
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON::Any qw(XS);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use base qw(POE::Component::Server::Bayeux::Message);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 __PACKAGE__->mk_accessors(qw(type));
21  
22 3 my %known_types = (
23 handshake => {
24 version => 1,
25 supportedConnectionTypes => { type => ARRAYREF },
26 minimumVersion => 0,
27 },
28 connect => {
29 clientId => 1,
30 connectionType => 1,
31 },
32 disconnect => {
33 clientId => 1,
34 },
35 subscribe => {
36 clientId => 1,
37 subscription => 1,
38 },
39 unsubscribe => {
40 clientId => 1,
41 subscription => 1,
42 },
43 );
44  
45 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
46 0 my $class = shift;
47 0 my $self = $class->SUPER::new(@_);
48  
49 # Extract and save the type of meta message
50  
51 0 my ($type) = $self->channel =~ m{^/meta/(.+)$};
52 0 ▶ if (! $type || ! $known_types{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type and $known_types{$type}) 0 0 0
Condition0%0/3
$type and $known_types{$type}
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $known_types{$type}
$type $known_types{$type}
Truth table0%0/3
A and B
A= $typeB= $known_types{$type}
ABresult
0X0
100
111
53 0 $self->request->error("Invalid channel ".$self->channel);
54 0 return;
55 }
56 0 $self->type($type);
57  
58 0 return $self;
59 }
60  
61 0 ▶sub validate_fields {
Subroutine validate_fields: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($self) = @_;
63  
64 0 my %validate_spec = %{ $known_types{ $self->type } };
65 0 $self->SUPER::validate_fields(%validate_spec);
66 }
67  
68 0 ▶sub pre_handle {
Subroutine pre_handle: not called CC=7 SCAR=40.3
Pod documented
69 0 my ($self) = @_;
70  
71 0 ▶ return if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 0 0 0
72  
73 0 ▶ if ($self->type eq 'connect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "connect") 0 0 0
74 # Connect needs to be the only connect message in the stack, and must be first
75  
76 0 my @new_order = ( $self );
77 0 foreach my $message (@{ $self->request->messages }) {
78 # Stringify hashref to find self
79 0 ▶ next if $message eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if $message eq $self 0 0 0
80  
81 0 ▶ if ($message->isa(__PACKAGE__) && $message->type eq 'connect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->isa(__PACKAGE__) and $message->type eq "connect") 0 0 0
Condition0%0/3
$message->isa(__PACKAGE__) and $message->type eq "connect"
!ll&&!rl&&r
000
MC/DC0%0/2
$message->isa(__PACKAGE__) and $message->type eq "connect"
$message->isa(__PACKAGE__) $message->type eq "connect"
Truth table0%0/3
A and B
A= $message->isa(__PACKAGE__)B= $message->type eq "connect"
ABresult
0X0
100
111
82 0 $message->is_error("Can only have on connect message per request");
83 0 next;
84 }
85  
86 0 push @new_order, $message;
87 }
88  
89 0 $self->request->messages( \@new_order );
90 }
91 }
92  
93 0 ▶sub handle {
Subroutine handle: not called CC=31 SCAR=69.0
Pod documented
94 0 my ($self) = @_;
95  
96 # Class handle() will call validate_fields()
97 0 $self->SUPER::handle();
98  
99 # Message may be in error, but the format of error return is dependent on
100 # the type of message we're responding to.
101  
102 0 my @responses;
103  
104 0 switch ($self->type) {
105 0 ▶ case 'handshake' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("handshake")) 0 0 0
106 # Must ignore any other messages sent in this request
107 0 $self->request->clear_stack();
108  
109 0 my $client;
110 0 ▶ if (! $self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 0 0 0
111 # Get the client by (possibly) generating a new client, passing the extra
112 # params in case they contain auth info.
113 0 $client = $self->request->client();
114  
115 # Run through acl (may set is_error flag)
116 0 $client->message_acl($self);
117 }
118  
119 0 ▶ if ($self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_error) 0 0 0
120 0 push @responses, {
121 successful => JSON::XS::false,
122 error => $self->is_error,
123 };
124 0 last;
125 }
126  
127 # TODO: Find a common connectionType
128 0 my $supported_connection_types = $POE::Component::Server::Bayeux::supported_connection_types;
129  
130 0 my %response = (
131 version => $POE::Component::Server::Bayeux::protocol_version,
132 minimumVersion => $POE::Component::Server::Bayeux::protocol_version,
133 supportedConnectionTypes => $supported_connection_types,
134 successful => JSON::XS::true,
135 clientId => $client->id,
136 advice => {
137 timeout => 2 * 60 * 1000,
138 interval => 0,
139 reconnect => 'retry',
140 },
141 ext => {
142 'json-comment-filtered' => JSON::XS::true,
143 },
144 );
145  
146 # Remember client support for json-comment-filtered
147 0 ▶ if ($self->ext && $self->ext->{'json-comment-filtered'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ext and $self->ext->{"json-comment-filtered"}) 0 0 0
Condition0%0/3
$self->ext and $self->ext->{"json-comment-filtered"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ext and $self->ext->{"json-comment-filtered"}
$self->ext $self->ext->{"json-comment-filtered"}
Truth table0%0/3
A and B
A= $self->extB= $self->ext->{"json-comment-filtered"}
ABresult
0X0
100
111
148 0 $client->flags->{'json-comment-filtered'} = 1;
149 }
150  
151 0 push @responses, \%response;
152 0 }
153 0 ▶ case 'connect' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("connect")) 0 0 0
154 0 my $client;
155 0 ▶ if (! $self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 0 0 0
156 0 $client = $self->request->client($self->clientId);
157 0 $client->message_acl($self);
158 0 ▶ $self->is_error($client->is_error) if $client->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $client->is_error 0 0 0
159 }
160  
161 0 ▶ if (! $self->error && $client->flags->{is_polling}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->error and $client->flags->{"is_polling"}) 0 0 0
Condition0%0/3
not $self->error and $client->flags->{"is_polling"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->error and $client->flags->{"is_polling"}
not $self->error $client->flags->{"is_polling"}
Truth table0%0/3
not(A) and B
A= not $self->errorB= $client->flags->{"is_polling"}
ABresult
0X0
100
111
162 0 $self->is_error("Client ".$self->clientId." is polling already (".$client->flags->{is_polling}.")");
163 }
164  
165 0 ▶ if ($self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_error) 0 0 0
166 0 push @responses, {
167 successful => JSON::XS::false,
168 error => $self->is_error,
169 clientId => $self->clientId,
170 advice => {
171 reconnect => 'handshake',
172 },
173 };
174 0 last;
175 }
176  
177 0 $client->flags->{is_polling} = $self->request->id;
178  
179 0 push @responses, {
180 successful => JSON::XS::true,
181 clientId => $client->id,
182 advice => {
183 timeout => 2 * 60 * 1000,
184 interval => 0,
185 reconnect => 'retry',
186 },
187 };
188  
189 0 my $no_delay = 0;
190  
191 # Handle queued responses
192 0 ▶ if (my $queue = delete $client->heap->{queued_responses}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $queue = delete $client->heap->{"queued_responses"}) 0 0 0
193 0 push @responses, @$queue;
194 0 $no_delay = 1;
195 }
196  
197 # Don't delay the first time they connect
198 0 ▶ if (++$client->flags->{connect_times} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$client->flags->{"connect_times"} == 1) 0 0 0
199 0 $no_delay = 1;
200 }
201 0 $client->flags->{last_connect} = time;
202  
203 # Come back to me to record last_connect time at end of connect
204 0 $self->request->add_post_handle($self);
205  
206 0 ▶ $self->request->delay(120) unless $no_delay;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_delay 0 0 0
207 0 }
208 0 ▶ case 'disconnect' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("disconnect")) 0 0 0
209 0 my $client;
210 0 ▶ if (! $self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 0 0 0
211 0 $client = $self->request->client($self->clientId);
212 0 $client->message_acl($self);
213 0 $client->disconnect();
214 }
215  
216 0 ▶ push @responses, {
Branch: 2 branches
BranchTrueFalseTotal
$self->is_error ? : 0 0 0
$self->is_error ? : 0 0 0
217 successful => $self->is_error ? JSON::XS::false : JSON::XS::true,
218 clientId => $client->id,
219 ($self->is_error ? ( error => $self->is_error ) : () ),
220 };
221 0 }
222 0 ▶ case 'subscribe' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("subscribe")) 0 0 0
223 0 my $client;
224 0 ▶ if (! $self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 0 0 0
225 0 $client = $self->request->client($self->clientId);
226 0 $client->message_acl($self);
227 }
228  
229 0 ▶ if ($self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_error) 0 0 0
230 0 push @responses, {
231 successful => JSON::XS::false,
232 error => $self->is_error,
233 clientId => $self->clientId,
234 subscription => $self->subscription,
235 };
236 0 last;
237 }
238  
239 0 push @responses, {
240 successful => JSON::XS::true,
241 clientId => $client->id,
242 subscription => $self->subscription,
243 };
244  
245 # Don't record a subscription to /meta or /service
246 0 ▶ if ($self->subscription !~ m{^/(meta|service)/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->subscription =~ m[^/(meta|service)/]) 0 0 0
247 0 $self->request->subscribe($client->id, $self->subscription);
248 }
249 0 }
250 0 ▶ case 'unsubscribe' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("unsubscribe")) 0 0 0
251 0 my $client;
252 0 ▶ if (! $self->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 0 0 0
253 0 $client = $self->request->client($self->clientId);
254 0 $client->message_acl($self);
255 }
256
257 0 ▶ if (! $self->is_error && ! $client->is_subscribed($self->subscription)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error or $client->is_subscribed($self->subscription)) 0 0 0
Condition0%0/3
$self->is_error or $client->is_subscribed($self->subscription)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_error or $client->is_subscribed($self->subscription)
$self->is_error $client->is_subscribed($self->subscription)
Truth table0%0/3
A or B
A= $self->is_errorB= $client->is_subscribed($self->subscription)
ABresult
1X1
011
000
258 0 $self->is_error("Client not subscribed to '" . $self->subscription . "'");
259 }
260  
261 0 $self->request->unsubscribe($client->id, $self->subscription);
262  
263 0 ▶ push @responses, {
Branch: 1 branch
BranchTrueFalseTotal
$self->is_error ? : 0 0 0
264 clientId => $self->clientId,
265 subscription => $self->subscription,
266 ($self->is_error ? (
267 successful => JSON::XS::false,
268 error => $self->is_error,
269 ) : (
270 successful => JSON::XS::true,
271 ))
272 };
273 0 }
274 0 }
275  
276 0 foreach my $response (@responses) {
277 0 ▶ $response->{channel} ||= $self->channel;
Condition0%0/3
$response->{"channel"} ||= $self->channel
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->{"channel"} ||= $self->channel
$response->{"channel"} $self->channel
Truth table0%0/3
A ||= B
A= $response->{"channel"}B= $self->channel
ABresult
1X1
011
000
278 0 ▶ $response->{id} = $self->id if $self->id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->id 0 0 0
279 0 $self->request->add_response($response);
280 }
281 }
282  
283 0 ▶sub post_handle {
Subroutine post_handle: not called CC=2 SCAR=17.9
Pod documented
284 0 my ($self) = @_;
285  
286 0 ▶ return unless $self->type eq 'connect';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq "connect" 0 0 0
287  
288 0 my $client = $self->request->client($self->clientId);
289 0 $client->flags->{last_connect} = time;
290 }
291  
292 =head1 COPYRIGHT
293  
294 Copyright (c) 2008 Eric Waters and XMission LLC (http://www.xmission.com/).
295 All rights reserved. This program is free software; you can redistribute it
296 and/or modify it under the same terms as Perl itself.
297  
298 The full text of the license can be found in the LICENSE file included with
299 this module.
300  
301 =head1 AUTHOR
302  
303 Eric Waters <ewaters@uarc.com>
304  
305 =cut
306  
307 3 1;
↑ Summary Server/Bayeux/Request.pm »