Connection.pm

filter: Statementstmt 72.3% 94 / 130 Branchbran 42.5% 17 / 40 Conditioncond 58.3% 7 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 66.6% 22 / 33 Podpod 77.7% 7 / 9 totaltotal 64.0% 148 / 231 CC 27 SCAR 12.9
CC
27
Cov
65%
disconnect
6.1
periodic
6.0
__ANON__
6.0
CRAP 3.6
« Connection/Raw.pm ↑ Summary Connection/Util.pm »
1 package AnyEvent::Connection;
2  
3 2 ▶use common::sense 2;m{
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use strict;
5 use warnings;
6 };
7 2 ▶use Object::Event 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use base 'Object::Event';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use AnyEvent 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use AnyEvent::Connection::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use AnyEvent::Connection::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # @rewrite s/^# //; # Development hacks, see L<Devel::Rewrite>
19 # use Devel::Leak::Cb;
20  
21 =head1 NAME
22  
23 AnyEvent::Connection - Base class for tcp connectful clients
24  
25 =cut
26  
27 2 our $VERSION = '0.06';
28  
29 =head1 SYNOPSIS
30  
31 package MyTCPClient;
32 use base 'AnyEvent::Connection';
33  
34 package main;
35 my $client = MyTCPClient->new(
36 host => 'localhost',
37 port => 12345,
38 );
39 $client->reg_cb(
40 connected => sub {
41 my ($client,$connection,$host,$port) = @_;
42 # ...
43 $client->after(
44 $interval, sub {
45 # Called after interval, if connection still alive
46 }
47 );
48 }
49 connfail = sub {
50 my ($client,$reason) = @_;
51 # ...
52 },
53 disconnect => sub {
54 my ($client,$reason) = @_;
55 },
56 error => sub {
57 my ($client,$error) = @_;
58 # Called in error conditions for callbackless methods
59 },
60 );
61 $client->connect;
62  
63 =head1 EVENTS
64  
65 =over 4
66  
67 =item connected ($connobject, $host, $port)
68  
69 Called when client get connected.
70  
71 =item connfail
72  
73 Called, when client fails to connect
74  
75 =item disconnect
76  
77 Called whenever client disconnects
78  
79 =item error
80  
81 Called in error conditions for callbackless methods (for ex: when calling push_write on non-connected client)
82  
83 =back
84  
85 =head1 OPTIONS
86  
87 =over 4
88  
89 =item host
90  
91 Host to connect to
92  
93 =item port
94  
95 Port to connect to
96  
97 =item timeout [ = 3 ]
98  
99 Connect/read/write timeout in seconds
100  
101 =item reconnect [ = 1 ]
102  
103 If true, automatically reconnect after disconnect/connfail after delay $reconnect seconds
104  
105 =item rawcon [ = AnyEvent::Connection::Raw ]
106  
107 Class that implements low-level connection
108  
109 =back
110  
111 =head1 OPERATION METHODS
112  
113 =over 4
114  
115 =item new
116  
117 Cleates connection object (see OPTIONS)
118  
119 =item connect
120  
121 Begin connection
122  
123 =item disconnect ($reason)
124  
125 Close current connection. reason is optional
126  
127 =item reconnect
128  
129  
130 Close current connection and establish a new one
131  
132 =item after($interval, $cb->())
133  
134 Helper method. AE::timer(after), associated with current connection
135  
136 Will be destroyed if connection is destroyed, so no timer invocation after connection destruction.
137  
138 =item periodic($interval, $cb->())
139  
140 Helper method. AE::timer(periodic), associated with current connection
141  
142 Will be destroyed if connection is destroyed, so no timer invocation after connection destruction.
143  
144 =item periodic_stop()
145  
146 If called within periodic callback, periodic will be stopped.
147  
148 my $count;
149 $client->periodic(1,sub {
150 $client->periodic_stop if ++$count > 10;
151 });
152
153 # callback will be called only 10 times;
154  
155 =item destroy
156  
157 Close connection, destroy all associated objects and timers, clean self
158  
159 =back
160  
161 =head1 CONNECT METHODS
162  
163 When connected, there are some methods, that proxied to raw connection or to AE::Handle
164  
165  
166 =over 4
167  
168 =item push_write
169  
170 See AE::Handle::push_write
171  
172 =item push_read
173  
174 See AE::Handle::push_read
175  
176 =item unshift_read
177  
178 See AE::Handle::unshift_read
179  
180 =item say
181  
182 Same as push_write + newline
183  
184 =item reply
185  
186 Same as push_write + newline
187  
188 =back
189  
190 For next methods there is a feature.
191 Callback will be called in any way, either by successful processing or by error or object destruction
192  
193 =over 4
194  
195 =item recv($bytes, %args, cb => $cb->())
196  
197 Similar to
198  
199 $fh->push_read(chunk => $bytes, $cb->());
200  
201 =item command($data, %args, cb => $cb->());
202  
203 Similar to
204  
205 $fh->push_write($data);
206 $fh->push_read(line => $cb->());
207  
208 =back
209  
210 =cut
211  
212 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
213 1 my $self = shift->SUPER::new(@_);
214 1 $self->init(@_);
215 1 return $self;
216 }
217  
218 1 ▶sub init {
Subroutine init: called CC=5 SCAR=16.8
Pod undocumented
219 1 my $self = shift;
220 1 ▶ $self->{debug} ||= 0;
Condition50%1/2
$self->{"debug"} ||= 0
l!l
01
MC/DC0%0/1
$self->{"debug"} ||= 0
$self->{"debug"}
Truth table50%1/2
A
A= $self->{"debug"}
Aresult
11
00
221 1 $self->{connected} = 0;
222 1 $self->{connecting} = 0;
223 1 ▶ $self->{reconnect} = 1 unless defined $self->{reconnect};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"reconnect"} 0 1 1
224 1 ▶ $self->{timeout} ||= 3;
Condition50%1/2
$self->{"timeout"} ||= 3
l!l
01
MC/DC0%0/1
$self->{"timeout"} ||= 3
$self->{"timeout"}
Truth table50%1/2
A
A= $self->{"timeout"}
Aresult
11
00
225 1 $self->{timers} = {};
226 1 ▶ $self->{rawcon} ||= 'AnyEvent::Connection::Raw';
Condition50%1/2
$self->{"rawcon"} ||= "AnyEvent::Connection::Raw"
l!l
01
MC/DC0%0/1
$self->{"rawcon"} ||= "AnyEvent::Connection::Raw"
$self->{"rawcon"}
Truth table50%1/2
A
A= $self->{"rawcon"}
Aresult
11
00
227 #warn "Init $self";
228 }
229  
230 #sub connected {
231 # warn "Connected";
232 # shift->event(connected => ());
233 #}
234  
235 5 ▶sub connect {
Subroutine connect: called CC=5 SCAR=17.0
Pod documented
236 5 my $self = shift;
237 5 ▶ $self->{connecting} and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"connecting"} and return 1 4 5
238 4 $self->{connecting} = 1;
239 4 weaken $self;
240 4 ▶ croak "Only client can connect but have $self->{type}" if $self->{type} and $self->{type} ne 'client';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} and $self->{"type"} ne "client" 0 4 4
Condition66%2/3
$self->{"type"} and $self->{"type"} ne "client"
!ll&&!rl&&r
130
MC/DC0%0/2
$self->{"type"} and $self->{"type"} ne "client"
$self->{"type"} $self->{"type"} ne "client"
Truth table66%2/3
A and B
A= $self->{"type"}B= $self->{"type"} ne "client"
ABresult
0X0
100
111
241 4 $self->{type} = 'client';
242
243 4 ▶ warn "Connecting to $self->{host}:$self->{port}..." if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
244 # @rewrite s/sub {/cb connect {/;
245 4 ▶ $self->{_}{con}{cb} = sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
246 4 pop;
247 4 delete $self->{_}{con};
248 4 ▶ if (my $fh = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = shift()) { } 4 0 4
249 4 ▶ warn "Connected @_" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
250 $self->{con} = $self->{rawcon}->new(
251 fh => $fh,
252 timeout => $self->{timeout},
253 debug => $self->{debug},
254 4 );
255 $self->{con}->reg_cb(
256 1 ▶ disconnect => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
257 1 ▶ warn "Disconnected $self->{host}:$self->{port} @_" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
258 1 $self->disconnect(@_);
259 1 $self->_reconnect_after();
260 },
261 4 );
262 4 $self->{connected} = 1;
263 #warn "Send connected event";
264 4 $self->event(connected => $self->{con}, @_);
265 } else {
266 0 ▶ warn "Not connected $self->{host}:$self->{port}: $!" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
267 0 $self->event(connfail => "$!");
268 0 $self->_reconnect_after();
269 }
270 4 };
271 4 ▶ $self->{_}{con}{pre} = sub { $self->{timeout} };
Subroutine __ANON__: called CC=1 SCAR=0.0
272 $self->{_}{con}{grd} =
273 AnyEvent::Socket::tcp_connect
274 $self->{host}, $self->{port},
275 $self->{_}{con}{cb}, $self->{_}{con}{pre}
276 4 ;
277 }
278  
279 0 ▶sub accept {
Subroutine accept: not called CC=1 SCAR=6.9
Pod undocumented
280 0 croak "Not implemented yet";
281 }
282  
283  
284 1 ▶sub _reconnect_after {
Subroutine _reconnect_after: called CC=2 SCAR=7.1
285 1 weaken( my $self = shift );
286 1 ▶ $self->{reconnect} or return $self->{connecting} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"reconnect"} or return $self->{"connecting"} = 0 1 0 1
287 $self->{timers}{reconnect} = AnyEvent->timer(
288 after => $self->{reconnect},
289 1 ▶ cb => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
290 1 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 1 0 1
291 1 delete $self->{timers}{reconnect};
292 1 $self->{connecting} = 0;
293 1 $self->connect;
294 }
295 1 );
296 }
297  
298 sub periodic_stop;
299 0 ▶sub periodic {
Subroutine periodic: not called CC=2 SCAR=17.9
Pod documented
300 0 weaken( my $self = shift );
301 0 my $interval = shift;
302 0 my $cb = shift;
303 #warn "Create periodic $interval";
304 $self->{timers}{int $cb} = AnyEvent->timer(
305 after => $interval,
306 interval => $interval,
307 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
308 0 ▶ local *periodic_stop = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
309 0 warn "Stopping periodic ".int $cb;
310 0 delete $self->{timers}{int $cb}; undef $cb
311 0 };
312 0 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 0 0 0
313 0 $cb->();
314 },
315 0 );
316 0 ▶ defined wantarray and return AnyEvent::Util::guard(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
317 0 delete $self->{timers}{int $cb};
318 0 undef $cb;
319 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray and return &AnyEvent::Util::guard(sub { delete $self->{"timers"}{int $cb}; undef $cb; } ) 0 0 0
320 0 return;
321 }
322  
323 0 ▶sub after {
Subroutine after: not called CC=2 SCAR=17.9
Pod documented
324 0 weaken( my $self = shift );
325 0 my $interval = shift;
326 0 my $cb = shift;
327 #warn "Create after $interval";
328 $self->{timers}{int $cb} = AnyEvent->timer(
329 after => $interval,
330 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
331 0 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 0 0 0
332 0 delete $self->{timers}{int $cb};
333 0 $cb->();
334 0 undef $cb;
335 },
336 0 );
337 0 ▶ defined wantarray and return AnyEvent::Util::guard(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
338 0 delete $self->{timers}{int $cb};
339 0 undef $cb;
340 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray and return &AnyEvent::Util::guard(sub { delete $self->{"timers"}{int $cb}; undef $cb; } ) 0 0 0
341 0 return;
342 }
343  
344 1 ▶sub reconnect {
Subroutine reconnect: called CC=1 SCAR=0.0
Pod documented
345 1 my $self = shift;
346 1 $self->disconnect;
347 1 $self->connect;
348 }
349  
350 5 ▶sub disconnect {
Subroutine disconnect: called CC=6 SCAR=18.0
Pod documented
351 5 my $self = shift;
352 #$self->{con} or return;
353 #warn "Disconnecting $self->{connected} || $self->{connecting} || $self->{reconnect} by @{[ (caller)[1,2] ]}";
354 5 ▶ ref $self->{con} eq 'HASH' and warn dumper($self->{con});
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"con"} eq "HASH" and warn &dumper($self->{"con"}) 0 5 5
355 5 ▶ $self->{con} and eval{ $self->{con}->close; };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"con"} and eval { do { $self->{"con"}->close } } 4 1 5
356 5 ▶ warn if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
357 5 delete $self->{con};
358 5 ▶ my $wascon = $self->{connected} || $self->{connecting};
Condition66%2/3
$self->{'connected'} || $self->{'connecting'}
l!l&&r!l&&!r
401
MC/DC50%1/2
$self->{'connected'} || $self->{'connecting'}
$self->{'connected'} $self->{'connecting'}
Truth table66%2/3
A || B
A= $self->{'connected'}B= $self->{'connecting'}
ABresult
1X1
011
000
359 5 $self->{connected} = 0;
360 5 $self->{connecting} = 0;
361 #$self->{reconnect} = 0;
362 5 delete $self->{timers}{reconnect};
363 5 ▶ $self->event('disconnect',@_) if $wascon;
Branch: 1 branch
BranchTrueFalseTotal
if $wascon 4 1 5
364 5 return;
365 }
366  
367 0 ▶sub AnyEvent::Connection::destroyed::AUTOLOAD {}
Subroutine AUTOLOAD: not called CC=1 SCAR=0.0
368  
369 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
370 0 my ($self) = @_;
371 0 $self->DESTROY;
372 0 bless $self, "AnyEvent::Connection::destroyed";
373 }
374  
375 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
376 1 my $self = shift;
377 1 ▶ warn "(".int($self).") Destroying AE::CNN" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
378 1 $self->disconnect;
379 1 %$self = ();
380 }
381  
382 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
383 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
384 2 for my $m (qw(push_write push_read unshift_read say reply recv command want_command)) {
385 0 ▶ *$m = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
386 0 my $self = shift;
387 0 ▶ $self->{connected} or return $self->event( error => "Not connected for $m" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"connected"} or return $self->event("error", "Not connected for $m") 0 0 0
388 0 $self->{con}->$m(@_);
389 16 };
390 }
391 }
392  
393 =head1 AUTHOR
394  
395 Mons Anderson, C<< <mons at cpan.org> >>
396  
397 =head1 BUGS
398  
399 Please report any bugs or feature requests to C<bug-anyevent-connection at rt.cpan.org>, or through
400 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-Connection>. I will be notified, and then you'll
401 automatically be notified of progress on your bug as I make changes.
402  
403  
404  
405  
406 =head1 SUPPORT
407  
408 You can find documentation for this module with the perldoc command.
409  
410 perldoc AnyEvent::Connection
411  
412  
413 You can also look for information at:
414  
415 =over 4
416  
417 =item * RT: CPAN's request tracker
418  
419 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-Connection>
420  
421 =item * AnnoCPAN: Annotated CPAN documentation
422  
423 L<http://annocpan.org/dist/AnyEvent-Connection>
424  
425 =item * CPAN Ratings
426  
427 L<http://cpanratings.perl.org/d/AnyEvent-Connection>
428  
429 =item * Search CPAN
430  
431 L<http://search.cpan.org/dist/AnyEvent-Connection/>
432  
433 =back
434  
435  
436 =head1 ACKNOWLEDGEMENTS
437  
438  
439 =head1 COPYRIGHT & LICENSE
440  
441 Copyright 2009 Mons Anderson, all rights reserved.
442  
443 This program is free software; you can redistribute it and/or modify it
444 under the same terms as Perl itself.
445  
446  
447 =cut
448  
449 2 1; # End of AnyEvent::Connection
« Connection/Raw.pm ↑ Summary Connection/Util.pm »