File Coverage

blib/lib/POE/Component/ResourcePool/Request.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package POE::Component::ResourcePool::Request;
4 1     1   10660 use Moose;
  0            
  0            
5              
6             use Carp::Clan qr/^(?:POE::Component::ResourcePool|Moose|Class::MOP)/;
7              
8             use POE;
9              
10             has session_id => (
11             is => "ro",
12             default => sub { $poe_kernel->get_active_session->ID },
13             );
14              
15             has event => (
16             isa => "Str",
17             is => "ro",
18             );
19              
20             has callback => (
21             isa => "CodeRef|Str",
22             is => "rw",
23             lazy_build => 1,
24             );
25              
26             sub BUILD {
27             my $self = shift;
28             $self->callback; # force builder
29             }
30              
31             sub _build_callback {
32             my $self = shift;
33              
34             my $event = $self->event;
35              
36             unless ( defined $event ) {
37             croak "Either 'event' or 'callback' is a required parameter";
38             }
39              
40             my $session_id = $self->session_id;
41              
42             return sub {
43             my ( $self, @args ) = @_;
44             $poe_kernel->post( $session_id, $event, request => $self, @args );
45             };
46             }
47              
48             has params => (
49             isa => "HashRef",
50             is => "rw",
51             required => 1,
52             );
53              
54             has pool => (
55             isa => "POE::Component::ResourcePool",
56             is => "ro",
57             required => 1,
58             );
59              
60             has dismissed => (
61             isa => "Bool",
62             is => "rw",
63             default => 0,
64             init_arg => undef,
65             writer => "_dismissed",
66             );
67              
68             has fulfilled => (
69             isa => "Bool",
70             is => "rw",
71             default => 0,
72             init_arg => undef,
73             writer => "_fulfilled",
74             );
75              
76             sub canceled {
77             my $self = shift;
78             $self->dismissed && !$self->fulfilled;
79             }
80              
81             has results => (
82             isa => "HashRef",
83             is => "rw",
84             init_arg => undef,
85             writer => "_results",
86             );
87              
88             sub dismiss {
89             my $self = shift;
90              
91             if ( my $pool = $self->pool ) { # might be false in global destruction
92             $pool->dismiss($self);
93             }
94             }
95              
96             sub invoke_callback {
97             my ( $self, @args ) = @_;
98              
99             my $cb = $self->callback;
100              
101             $self->$cb( @args );
102             }
103              
104             sub DEMOLISH {
105             my $self = shift;
106             $self->dismiss;
107             }
108              
109             __PACKAGE__
110              
111             __END__
112              
113             =pod
114              
115             =head1 NAME
116              
117             POE::Component::ResourcePool::Request - A bundle of resource request
118             parameters.
119              
120             =head1 SYNOPSIS
121              
122             $pool->request(
123             # specify what you want
124             params => {
125             resource_name => ...,
126             other_resource => ...,
127             },
128              
129             # specify what to do when you've got what you want
130             event => "moose",
131              
132             );
133              
134             =head1 DESCRIPTION
135              
136             The request object represents a bundle of required resources in the queue.
137              
138             A request will wait in a pool's queue until sufficient resources are available
139             to dispatch them, at which point its callback will be triggerred.
140              
141             =head1 RESOURCE MANAGEMENT
142              
143             A request can be deallocated by calling C<dismiss>, returning the allocated
144             value to the resource.
145              
146             When a resource is garbage collected it will call C<dismiss> automatically.
147              
148             C<dismiss> can also be called before the request is fulfilled in order to
149             cancel it.
150              
151             =head1 METHODS
152              
153             =over 4
154              
155             =item new
156              
157             Create a new request
158              
159             =item dismiss
160              
161             If the request has already been fulfilled then deallocate it, otherwise cancel
162             it.
163              
164             =item dismissed
165              
166             Returns a boolean value denoting whether or not the request has been dismissed.
167              
168             =item fulfilled
169              
170             Returns a boolean value denoting whether or not the request has been fulfilled.
171              
172             =item canceled
173              
174             Returns a boolean value denoting whether or not the request has been canceled
175             (dismissed but not fulfilled).
176              
177             =back
178              
179             =head1 ATTRIBUTES
180              
181             =over 4
182              
183             =item callback
184              
185             The callback to call when the request is fulfilled.
186              
187             See also the C<event> attribute.
188              
189             =item event
190              
191             An event name to C<post> to on the currently active session at the time of the
192             resource's creation. Used to generate a default C<callback>.
193              
194             =item session_id
195              
196             THe ID of the currently active session at the time of the resource's creation.
197             Used to generate a default C<callback> and to increment the reference count of
198             sessions waiting on resources.
199              
200             If the current session is not the session that the request should be associated
201             with then this parameter may be specified, but in general that is discouraged.
202              
203             =back
204              
205             =cut