File Coverage

blib/lib/POE/Declare/Meta/Timeout.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition 1 2 50.0
subroutine 8 8 100.0
pod 0 1 0.0
total 33 35 94.2


line stmt bran cond sub pod time code
1             package POE::Declare::Meta::Timeout;
2              
3             =pod
4              
5             =head1 NAME
6              
7             POE::Declare::Meta::Timeout - A named timeout with generated support methods
8              
9             =head1 SYNOPSIS
10              
11             # Send a request
12             sub request : Event {
13             $_[SELF]->request_timeout_start;
14             $_[SELF]->{handle}->put('something');
15             }
16            
17             # Received a response
18             sub response : Event {
19             if ( $_[ARG0] eq 'keepalive' ) {
20             $_[SELF]->request_timeout_restart;
21             return;
22             }
23            
24             $_[SELF]->request_timeout_stop;
25             $_[SELF]->{parent}->post('child_response', $_[ARG0]);
26             }
27            
28             # Did not get a response
29             sub request_timeout : Timeout(30) {
30             # Take some action
31             $_[SELF]->cleanup;
32             $_[SELF]->{parent}->post('child_response', 'timeout');
33             }
34              
35             =head1 DESCRIPTION
36              
37             B is a sub-class of C with access to
38             a number of additional methods relating to timers and alarms.
39              
40             =cut
41              
42 4     4   62 use 5.008007;
  4         12  
  4         145  
43 4     4   18 use strict;
  4         6  
  4         109  
44 4     4   19 use warnings;
  4         34  
  4         113  
45 4     4   19 use POE::Declare::Meta::Event ();
  4         6  
  4         78  
46              
47 4     4   21 use vars qw{$VERSION @ISA};
  4         6  
  4         258  
48             BEGIN {
49 4     4   9 $VERSION = '0.59';
50 4         233 @ISA = 'POE::Declare::Meta::Event';
51             }
52              
53             use Class::XSAccessor {
54 4         46 getters => {
55             delay => 'delay',
56             variance => 'variance',
57             },
58 4     4   39 };
  4         7  
59              
60              
61              
62              
63              
64             #####################################################################
65             # Main Methods
66              
67             sub as_perl {
68 2     2 0 17 my $name = $_[0]->{name};
69 2         6 my $delay = $_[0]->{delay};
70 2   50     17 my $variance = $_[0]->{variance} || 0;
71 2         32 return <<"END_PERL";
72             sub ${name}_start {
73             my \$delay = \@_ > 1 ? defined \$_[1] ? \$_[1] : $delay : $delay;
74             my \$variance = \@_ > 1 ? \$_[2] || $variance : $variance;
75             \$poe_kernel->delay(
76             '$name' => \$delay + rand(\$variance * 2) - \$variance
77             ) or return;
78             die("Failed to create timeout for event '$name'");
79             }
80              
81             *${name}_restart = *${name}_start;
82              
83             sub ${name}_stop {
84             my \$self = (\@_ == 1) ? \$_[0] : \$_[HEAP];
85             \$poe_kernel->delay( '$name' ) or return;
86             die("Failed to create timeout for event '$name'");
87             }
88             END_PERL
89             }
90              
91             1;
92              
93             =pod
94              
95             =head1 SUPPORT
96              
97             Bugs should be always be reported via the CPAN bug tracker at
98              
99             L
100              
101             For other issues, or commercial enhancement or support, contact the author.
102              
103             =head1 AUTHORS
104              
105             Adam Kennedy Eadamk@cpan.orgE
106              
107             =head1 SEE ALSO
108              
109             L, L
110              
111             =head1 COPYRIGHT
112              
113             Copyright 2006 - 2012 Adam Kennedy.
114              
115             This program is free software; you can redistribute
116             it and/or modify it under the same terms as Perl itself.
117              
118             The full text of the license can be found in the
119             LICENSE file included with this module.
120              
121             =cut