| 1 | package POE::Declare::Meta::Timeout; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | POE::Declare::Meta::Timeout - A named timeout event with 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 | # Recieved 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<POE::Declare::Meta::Timeout> is a sub-class of C<Event> with access to | ||
| 38 | a number of additional methods relating to timers and alarms. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | 7 | ▶ | use 5.008007; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 7 | ▶ | use POE::Declare::Meta::Event (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | |||
| 47 | 7 | ▶ | use vars qw{$VERSION @ISA}; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | BEGIN { | ||
| 49 | 7 | ▶ | $VERSION = '0.23_01'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 7 | @ISA = 'POE::Declare::Meta::Event'; | |
| 51 | } | ||
| 52 | |||
| 53 | use Class::XSAccessor | ||
| 54 | 7 | getters => { | |
| 55 | delay => 'delay', | ||
| 56 | 7 | ▶ | }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | ##################################################################### | ||
| 63 | # Main Methods | ||
| 64 | |||
| 65 | sub _compile { | ||
| 66 | 0 | ▶ | my $name = $_[0]->{name}; |
|
Subroutine
_compile: not called CC=1 SCAR=6.9
| |||
| 67 | 0 | my $delay = $_[0]->{delay}; | |
| 68 | 0 | return <<"END_PERL"; | |
| 69 | sub ${name}_start { | ||
| 70 | my \$self = (\@_ == 1) ? \$_[0] : \$_[HEAP]; | ||
| 71 | if ( \$self->{$name} ) { | ||
| 72 | # Clear any existing timer | ||
| 73 | \$self->${name}_clear; | ||
| 74 | } | ||
| 75 | my \$timer = \$poe_kernel->delay_set( | ||
| 76 | $name => $delay | ||
| 77 | ); | ||
| 78 | if ( \$timer ) { | ||
| 79 | \$self->{$name} = \$timer; | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Invalid timer creation | ||
| 84 | die('${name}_start provided invalid params to delay_set'); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub ${name}_restart { | ||
| 88 | my \$self = (\@_ == 1) ? \$_[0] : \$_[HEAP]; | ||
| 89 | if ( \$self->{$name} ) { | ||
| 90 | # Have an existing timer id, try to reset it | ||
| 91 | my \$rv = \$poe_kernel->delay_adjust( | ||
| 92 | $name => $delay | ||
| 93 | ); | ||
| 94 | return 1 if \$rv; | ||
| 95 | if ( \$! == Errno::ESRCH ) { | ||
| 96 | # Previous keepalive expired and dispatched | ||
| 97 | delete \$self->{$name}; | ||
| 98 | } elsif ( \$! == Errno::EPERM ) { | ||
| 99 | # Set in wrong context | ||
| 100 | Carp::croak('Tried to reset $name timeout in bad context'); | ||
| 101 | } else { | ||
| 102 | # Something else | ||
| 103 | die('${name}_keepalive provided invalid params to delay_adjust'); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | # No existing timer, create a new one | ||
| 108 | my \$timer = \$poe_kernel->delay_set( | ||
| 109 | $name => $delay | ||
| 110 | ); | ||
| 111 | if ( \$timer ) { | ||
| 112 | # Timer was set correctly | ||
| 113 | \$self->{$name} = \$timer; | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Invalid timer creation | ||
| 118 | die('${name}_keepalive provided invalid params to delay_set'); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub ${name}_stop { | ||
| 122 | my \$self = (\@_ == 1) ? \$_[0] : \$_[HEAP]; | ||
| 123 | my \$timer = \$self->{$name} or return; | ||
| 124 | \$poe_kernel->alarm_remove( | ||
| 125 | delete \$self->{$name} | ||
| 126 | ); | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | END_PERL | ||
| 130 | } | ||
| 131 | |||
| 132 | 7 | 1; | |
| 133 | |||
| 134 | =pod | ||
| 135 | |||
| 136 | =head1 SUPPORT | ||
| 137 | |||
| 138 | Bugs should be always be reported via the CPAN bug tracker at | ||
| 139 | |||
| 140 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare> | ||
| 141 | |||
| 142 | For other issues, or commercial enhancement or support, contact the author. | ||
| 143 | |||
| 144 | =head1 AUTHORS | ||
| 145 | |||
| 146 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<POE>, L<POE::Declare> | ||
| 151 | |||
| 152 | =head1 COPYRIGHT | ||
| 153 | |||
| 154 | Copyright 2006 - 2009 Adam Kennedy. | ||
| 155 | |||
| 156 | This program is free software; you can redistribute | ||
| 157 | it and/or modify it under the same terms as Perl itself. | ||
| 158 | |||
| 159 | The full text of the license can be found in the | ||
| 160 | LICENSE file included with this module. | ||
| 161 | |||
| 162 | =cut | ||