| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# $Id: Stage.pm 201 2009-07-28 06:39:31Z rcaputo $ |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
POE::Stage - a base class for message-driven objects |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#!/usr/bin/env perl |
|
10
|
|
|
|
|
|
|
{ |
|
11
|
|
|
|
|
|
|
package App; |
|
12
|
|
|
|
|
|
|
use POE::Stage::App qw(:base); |
|
13
|
|
|
|
|
|
|
sub on_run { |
|
14
|
|
|
|
|
|
|
print "hello, ", my $arg_whom, "!\n"; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
} |
|
17
|
|
|
|
|
|
|
App->new()->run( whom => "world" ); |
|
18
|
|
|
|
|
|
|
exit; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
POE::Stage is a set of base classes for message-driven objects. It |
|
23
|
|
|
|
|
|
|
cleanly implements standard patterns that have emerged from years of |
|
24
|
|
|
|
|
|
|
working with POE and POE::Component modules. |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
As I hope the name implies, POE::Stage objects encapsulate discrete |
|
27
|
|
|
|
|
|
|
steps, or stages, of a larger task. Eventually they come together to |
|
28
|
|
|
|
|
|
|
implement programs. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
For example, HTTP requests are performed in four or so distinct |
|
31
|
|
|
|
|
|
|
stages: 1. The server's address is resolved. 2. The client |
|
32
|
|
|
|
|
|
|
establishes a connection to the server. 3. The client transmits a |
|
33
|
|
|
|
|
|
|
request. 4. The client receives a response. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
By design, POE::Stage promotes the decomposition of tasks into |
|
36
|
|
|
|
|
|
|
multiple, smaller stages. If these stages are generic enough, new |
|
37
|
|
|
|
|
|
|
tasks may be handled by reusing them in different configurations. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The hypothetical HTTP client might be a single stage composed of three |
|
40
|
|
|
|
|
|
|
smaller ones: A DNS resolver stage, which accepts DNS requests and |
|
41
|
|
|
|
|
|
|
returns DNS responses. A TCP client connection factory, which takes |
|
42
|
|
|
|
|
|
|
socket endpoint descriptions and other parameters, and eventually |
|
43
|
|
|
|
|
|
|
returns established connections. Finally, there would be an HTTP |
|
44
|
|
|
|
|
|
|
protocol stage that uses established connections to send requests and |
|
45
|
|
|
|
|
|
|
parse responses. |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
These stages would be encapsulated by a higher-level HTTP client |
|
48
|
|
|
|
|
|
|
stage. This would accept HTTP requests and return HTTP responses |
|
49
|
|
|
|
|
|
|
after performing the necessary steps to gather them. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This will sound familiar to anyone working with objects. |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
These objects are asynchronous and message-driven, however. The base |
|
54
|
|
|
|
|
|
|
message class, POE::Request, and its subclasses, implement a standard |
|
55
|
|
|
|
|
|
|
request/response interface between POE::Stage objects. Where |
|
56
|
|
|
|
|
|
|
possible, these messages attempt to mimic simpler, more direct |
|
57
|
|
|
|
|
|
|
call/return syntax, albeit asynchronously. POE::Stage also provides a |
|
58
|
|
|
|
|
|
|
powerful closure-based system for maintaining request and response |
|
59
|
|
|
|
|
|
|
state, so you don't have to. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
package POE::Stage; |
|
64
|
|
|
|
|
|
|
|
|
65
|
1
|
|
|
1
|
|
24355
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
26
|
|
|
66
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
31
|
|
|
67
|
|
|
|
|
|
|
|
|
68
|
1
|
|
|
1
|
|
11
|
use vars qw($VERSION); |
|
|
1
|
|
|
|
|
7
|
|
|
|
1
|
|
|
|
|
63
|
|
|
69
|
|
|
|
|
|
|
$VERSION = '0.060'; |
|
70
|
|
|
|
|
|
|
|
|
71
|
1
|
|
|
1
|
|
1985
|
use POE::Session; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
use Attribute::Handlers; |
|
74
|
|
|
|
|
|
|
use Carp qw(croak); |
|
75
|
|
|
|
|
|
|
use Devel::LexAlias qw(lexalias); |
|
76
|
|
|
|
|
|
|
use PadWalker qw(var_name); |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
use Hash::Util::FieldHash; |
|
79
|
|
|
|
|
|
|
use POE::Callback; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use POE::Request::Emit; |
|
82
|
|
|
|
|
|
|
use POE::Request::Return; |
|
83
|
|
|
|
|
|
|
use POE::Request::Recall; |
|
84
|
|
|
|
|
|
|
use POE::Request qw(REQ_ID); |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Field hash tracks POE::Stage's out-of-band data for each object. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub STAGE_DATA () { 0 } # The stage's object-scoped data. |
|
89
|
|
|
|
|
|
|
sub COMBINED_KEYS () { 1 } # Temporary space for iteration. |
|
90
|
|
|
|
|
|
|
sub REQUEST () { 2 } # Currently active request. |
|
91
|
|
|
|
|
|
|
sub RESPONSE () { 3 } # Currently active response. |
|
92
|
|
|
|
|
|
|
sub REQ_CONTEXTS () { 4 } # Contexts for each request in play. |
|
93
|
|
|
|
|
|
|
sub REQ_INIT () { 5 } # The init request shares the stage's lifetime. |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Hash::Util::FieldHash::fieldhash(my %private); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _get_request { return $private{$_[0]}[REQUEST] } |
|
98
|
|
|
|
|
|
|
sub _get_response { return $private{$_[0]}[RESPONSE] } |
|
99
|
|
|
|
|
|
|
sub _set_req_rsp { $private{$_[0]}[REQUEST] = $_[1]; $private{$_[0]}[RESPONSE] = $_[2] } |
|
100
|
|
|
|
|
|
|
sub _set_req_init { $private{$_[0]}[REQ_INIT] = $_[1] } |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _self_store { |
|
103
|
|
|
|
|
|
|
my ($self, $key, $value) = @_; |
|
104
|
|
|
|
|
|
|
return $private{$self}[STAGE_DATA]{$key} = $value; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _self_fetch { |
|
108
|
|
|
|
|
|
|
my ($self, $key) = @_; |
|
109
|
|
|
|
|
|
|
return $private{$self}[STAGE_DATA]{$key}; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _request_context_store { |
|
113
|
|
|
|
|
|
|
my ($self, $req_id, $key, $value) = @_; |
|
114
|
|
|
|
|
|
|
return $private{$self}[REQ_CONTEXTS]{$req_id}{$key} = $value; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _request_context_fetch { |
|
118
|
|
|
|
|
|
|
my ($self, $req_id, $key) = @_; |
|
119
|
|
|
|
|
|
|
return $private{$self}[REQ_CONTEXTS]{$req_id}{$key}; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _request_context_destroy { |
|
123
|
|
|
|
|
|
|
my ($self, $req_id) = @_; |
|
124
|
|
|
|
|
|
|
delete $private{$self}[REQ_CONTEXTS]{$req_id}; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Track classes that use() POE::Stage, and methods with explicit |
|
128
|
|
|
|
|
|
|
# :Handler magic (so we don't wrap them twice). |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my %subclass; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub import { |
|
133
|
|
|
|
|
|
|
my $class = shift(); |
|
134
|
|
|
|
|
|
|
my $caller = caller(); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
strict->import(); |
|
137
|
|
|
|
|
|
|
warnings->import(); |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$subclass{$caller} = { } unless exists $subclass{$caller}; |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
foreach my $export (@_) { |
|
142
|
|
|
|
|
|
|
no strict 'refs'; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
if ($export eq ":base") { |
|
145
|
|
|
|
|
|
|
unshift @{ $caller . "::ISA" }, $class; |
|
146
|
|
|
|
|
|
|
next; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# If $class can't supply $export, check for it from __PACKAGE__. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $which = $class; |
|
152
|
|
|
|
|
|
|
unless (defined *{$which . "::$export"}) { |
|
153
|
|
|
|
|
|
|
$which = __PACKAGE__; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
unless (defined *{$which . "::$export"}) { |
|
156
|
|
|
|
|
|
|
croak "Neither $class nor ", __PACKAGE__, " export $export"; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
*{ $caller . "::$export" } = *{ $which . "::$export" }; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# At CHECK time, find (and wrap) the methods that begin with "on_" |
|
164
|
|
|
|
|
|
|
# with :Handler magic. If they haven't already been wrapped. |
|
165
|
|
|
|
|
|
|
# |
|
166
|
|
|
|
|
|
|
# But only if they don't already have it. Must go before |
|
167
|
|
|
|
|
|
|
# Attribute::Handlers is loaded, otherwise A::H's check comes later. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# The missing pieces: |
|
170
|
|
|
|
|
|
|
# |
|
171
|
|
|
|
|
|
|
# 1 - POE::Callback (was: _add_handler_magic) |
|
172
|
|
|
|
|
|
|
# 2 - :Handler that uses POE::Callback. |
|
173
|
|
|
|
|
|
|
# 3 - Package wrapper magic. |
|
174
|
|
|
|
|
|
|
# 4 - track wrappers so they aren't rewrapped |
|
175
|
|
|
|
|
|
|
# TODO 5 - Anon coderefs are wrapped when passed to POE::Stage users. |
|
176
|
|
|
|
|
|
|
# TODO 6 - Built-in class reloader. Wraps reloaded classes. |
|
177
|
|
|
|
|
|
|
# 7 - Magic at CHECK time to ensure initial wrap. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub _wrap_package { |
|
180
|
|
|
|
|
|
|
my $package = shift; |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
no strict 'refs'; |
|
183
|
|
|
|
|
|
|
foreach my $symbol (values %{$package . "::"}) { |
|
184
|
|
|
|
|
|
|
my $sub_name = *{$symbol}{NAME}; |
|
185
|
|
|
|
|
|
|
next unless defined($sub_name) and $sub_name =~ /^on_/; |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
no warnings 'redefine'; |
|
188
|
|
|
|
|
|
|
my $full_name = $package . '::' . $sub_name; |
|
189
|
|
|
|
|
|
|
*{$full_name} = POE::Callback->new( |
|
190
|
|
|
|
|
|
|
{ |
|
191
|
|
|
|
|
|
|
name => $full_name, |
|
192
|
|
|
|
|
|
|
code => *{$symbol}{CODE}, |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
); |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
CHECK { |
|
199
|
|
|
|
|
|
|
foreach my $subclass (sort keys %subclass) { |
|
200
|
|
|
|
|
|
|
# Never subclassed... |
|
201
|
|
|
|
|
|
|
# TODO - Would it be good to throw a warning? |
|
202
|
|
|
|
|
|
|
next unless $subclass->isa(__PACKAGE__); |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
_wrap_package($subclass); |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# An internal singleton POE::Session that will drive all the stages |
|
209
|
|
|
|
|
|
|
# for the application. This should be structured such that we can |
|
210
|
|
|
|
|
|
|
# create multiple stages later, each driving some smaller part of the |
|
211
|
|
|
|
|
|
|
# program. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $singleton_session_id = POE::Session->create( |
|
214
|
|
|
|
|
|
|
inline_states => { |
|
215
|
|
|
|
|
|
|
_start => sub { |
|
216
|
|
|
|
|
|
|
$_[KERNEL]->alias_set(__PACKAGE__); |
|
217
|
|
|
|
|
|
|
}, |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Handle a request. Map the request to a stage object/method |
|
220
|
|
|
|
|
|
|
# call. |
|
221
|
|
|
|
|
|
|
stage_request => sub { |
|
222
|
|
|
|
|
|
|
my $request = $_[ARG0]; |
|
223
|
|
|
|
|
|
|
$request->deliver(); |
|
224
|
|
|
|
|
|
|
}, |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Handle a timer. Deliver it to its resource. |
|
227
|
|
|
|
|
|
|
# $resource is an envelope around a weak POE::Watcher reference. |
|
228
|
|
|
|
|
|
|
stage_timer => sub { |
|
229
|
|
|
|
|
|
|
my $resource = $_[ARG0]; |
|
230
|
|
|
|
|
|
|
eval { $resource->[0]->deliver(); }; |
|
231
|
|
|
|
|
|
|
die if $@; |
|
232
|
|
|
|
|
|
|
}, |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# Handle an I/O event. Deliver it to its resource. |
|
235
|
|
|
|
|
|
|
# $resource is an envelope around a weak POE::Watcher reference. |
|
236
|
|
|
|
|
|
|
stage_io => sub { |
|
237
|
|
|
|
|
|
|
my $resource = $_[ARG2]; |
|
238
|
|
|
|
|
|
|
eval { $resource->[0]->deliver(); }; |
|
239
|
|
|
|
|
|
|
die if $@; |
|
240
|
|
|
|
|
|
|
}, |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# Deliver to wheels based on the wheel ID. Different wheels pass |
|
243
|
|
|
|
|
|
|
# their IDs in different ARGn offsets, so we need a few of these. |
|
244
|
|
|
|
|
|
|
wheel_event_0 => sub { |
|
245
|
|
|
|
|
|
|
$_[CALLER_FILE] =~ m{/([^/.]+)\.pm}; |
|
246
|
|
|
|
|
|
|
eval { "POE::Watcher::Wheel::$1"->deliver(0, @_[ARG0..$#_]); }; |
|
247
|
|
|
|
|
|
|
die if $@; |
|
248
|
|
|
|
|
|
|
}, |
|
249
|
|
|
|
|
|
|
wheel_event_1 => sub { |
|
250
|
|
|
|
|
|
|
$_[CALLER_FILE] =~ m{/([^/.]+)\.pm}; |
|
251
|
|
|
|
|
|
|
eval { "POE::Watcher::Wheel::$1"->deliver(1, @_[ARG0..$#_]); }; |
|
252
|
|
|
|
|
|
|
die if $@; |
|
253
|
|
|
|
|
|
|
}, |
|
254
|
|
|
|
|
|
|
wheel_event_2 => sub { |
|
255
|
|
|
|
|
|
|
$_[CALLER_FILE] =~ m{/([^/.]+)\.pm}; |
|
256
|
|
|
|
|
|
|
eval { "POE::Watcher::Wheel::$1"->deliver(2, @_[ARG0..$#_]); }; |
|
257
|
|
|
|
|
|
|
die if $@; |
|
258
|
|
|
|
|
|
|
}, |
|
259
|
|
|
|
|
|
|
wheel_event_3 => sub { |
|
260
|
|
|
|
|
|
|
$_[CALLER_FILE] =~ m{/([^/.]+)\.pm}; |
|
261
|
|
|
|
|
|
|
eval { "POE::Watcher::Wheel::$1"->deliver(3, @_[ARG0..$#_]); }; |
|
262
|
|
|
|
|
|
|
die if $@; |
|
263
|
|
|
|
|
|
|
}, |
|
264
|
|
|
|
|
|
|
wheel_event_4 => sub { |
|
265
|
|
|
|
|
|
|
$_[CALLER_FILE] =~ m{/([^/.]+)\.pm}; |
|
266
|
|
|
|
|
|
|
eval { "POE::Watcher::Wheel::$1"->deliver(4, @_[ARG0..$#_]); }; |
|
267
|
|
|
|
|
|
|
die if $@; |
|
268
|
|
|
|
|
|
|
}, |
|
269
|
|
|
|
|
|
|
}, |
|
270
|
|
|
|
|
|
|
)->ID(); |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub _get_session_id { |
|
273
|
|
|
|
|
|
|
return $singleton_session_id; |
|
274
|
|
|
|
|
|
|
} |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 RESERVED METHODS |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
To do its job, POE::Stage requires some methods for its own. To be |
|
279
|
|
|
|
|
|
|
extensible, it reserves other methods for standard purposes. To |
|
280
|
|
|
|
|
|
|
remain useful, it reserves the least number of methods possible. |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 new ARGUMENT_PAIRS |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
new() creates and returns a new POE::Stage object. An optional set of |
|
285
|
|
|
|
|
|
|
named ARGUMENT_PAIRS will be passed to the object's init() callback |
|
286
|
|
|
|
|
|
|
before new() returns. |
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
Subclasses should not override new() unless they're careful to call |
|
289
|
|
|
|
|
|
|
the base POE::Stage's constructor. Object construction is customized |
|
290
|
|
|
|
|
|
|
through the init() callback instead. |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=cut |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub new { |
|
295
|
|
|
|
|
|
|
my $class = shift; |
|
296
|
|
|
|
|
|
|
croak "$class->new(...) requires an even number of parameters" if @_ % 2; |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
my %args = @_; |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
my $self = bless { }, $class; |
|
301
|
|
|
|
|
|
|
$private{$self} = [ |
|
302
|
|
|
|
|
|
|
{ }, # STAGE_DATA |
|
303
|
|
|
|
|
|
|
[ ], # COMBINED_KEYS |
|
304
|
|
|
|
|
|
|
undef, # REQUEST |
|
305
|
|
|
|
|
|
|
undef, # RESPONSE |
|
306
|
|
|
|
|
|
|
{ }, # REQ_CONTEXTS |
|
307
|
|
|
|
|
|
|
undef, # REQ_INIT |
|
308
|
|
|
|
|
|
|
]; |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# Set the context of init() to that of a new request to the new |
|
311
|
|
|
|
|
|
|
# object. Any resources created in on_init() will need to be stored |
|
312
|
|
|
|
|
|
|
# within $self rather than $req, otherwise they won't be visible to |
|
313
|
|
|
|
|
|
|
# other requests. |
|
314
|
|
|
|
|
|
|
# |
|
315
|
|
|
|
|
|
|
# The target stage is weakened immediately after the request is |
|
316
|
|
|
|
|
|
|
# delivered. The request's target stage refers to the stage, and |
|
317
|
|
|
|
|
|
|
# the stage holds a copy of the target request. This would be a |
|
318
|
|
|
|
|
|
|
# circular reference. TODO - Investigte saving the request in the |
|
319
|
|
|
|
|
|
|
# creator stage. |
|
320
|
|
|
|
|
|
|
# |
|
321
|
|
|
|
|
|
|
# TODO - In theory, new() could also be given parameters that are |
|
322
|
|
|
|
|
|
|
# passed to the hidden request. |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
my %on = map { $_ => delete $args{$_} } grep /^on_/, keys %args; |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
my $req = POE::Request->new_without_send( |
|
327
|
|
|
|
|
|
|
stage => $self, |
|
328
|
|
|
|
|
|
|
%on, |
|
329
|
|
|
|
|
|
|
method => "on_init", |
|
330
|
|
|
|
|
|
|
( |
|
331
|
|
|
|
|
|
|
exists($args{role}) ? (role => delete($args{role})) : () |
|
332
|
|
|
|
|
|
|
), |
|
333
|
|
|
|
|
|
|
args => \%args, |
|
334
|
|
|
|
|
|
|
); |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
$req->deliver(); |
|
337
|
|
|
|
|
|
|
$private{$self}[REQ_INIT] = $req; |
|
338
|
|
|
|
|
|
|
$req->_weaken_target_stage(); |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
return $self; |
|
341
|
|
|
|
|
|
|
} |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=head2 init ARGUMENT_PAIRS |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
init() is a callback used to initialize POE::Stage objects after they |
|
346
|
|
|
|
|
|
|
are constructed. POE::Stage's new() constructor passes its named |
|
347
|
|
|
|
|
|
|
ARGUMENT_PAIRS to init() prior to returning the new object. The |
|
348
|
|
|
|
|
|
|
values of these arguments will be available as $arg_name lexicals |
|
349
|
|
|
|
|
|
|
within the init() callback: |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
my $object = POE::Stage::Something->new( foo => 123 ); |
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
package POE::Stage::Something; |
|
354
|
|
|
|
|
|
|
sub init { |
|
355
|
|
|
|
|
|
|
print my $arg_foo, "\n"; # displays "123\n". |
|
356
|
|
|
|
|
|
|
} |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
The init() callback is optional. |
|
359
|
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
=cut |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
sub init { |
|
363
|
|
|
|
|
|
|
# Do nothing. Don't even throw an error. |
|
364
|
|
|
|
|
|
|
undef; |
|
365
|
|
|
|
|
|
|
} |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
# TODO - Make these internal? |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
sub self { |
|
370
|
|
|
|
|
|
|
package DB; |
|
371
|
|
|
|
|
|
|
my @x = caller(1); |
|
372
|
|
|
|
|
|
|
return $DB::args[0]; |
|
373
|
|
|
|
|
|
|
} |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
sub req { |
|
376
|
|
|
|
|
|
|
my $stage = POE::Request->_get_current_stage(); |
|
377
|
|
|
|
|
|
|
return $stage->_get_request(); |
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
sub rsp { |
|
381
|
|
|
|
|
|
|
my $stage = POE::Request->_get_current_stage(); |
|
382
|
|
|
|
|
|
|
return $stage->_get_response(); |
|
383
|
|
|
|
|
|
|
} |
|
384
|
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=head2 Handler |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
The Handler method implements an attribute handler that defines which |
|
389
|
|
|
|
|
|
|
methods handle messages. Only message handlers have access to the |
|
390
|
|
|
|
|
|
|
closures that maintain state between messages. |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
The Handler method is used as a subroutine attribute: |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
sub some_method :Handler { |
|
395
|
|
|
|
|
|
|
# Lexical magic occurs here. |
|
396
|
|
|
|
|
|
|
} |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
sub not_a_handler { |
|
399
|
|
|
|
|
|
|
# No lexical magic happens in this one. |
|
400
|
|
|
|
|
|
|
} |
|
401
|
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
Methods with names beginning with "on_" acquire Handler magic |
|
403
|
|
|
|
|
|
|
automatically. |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
sub on_event { |
|
406
|
|
|
|
|
|
|
# Lexical magic occurs here. No :Handler necessary. |
|
407
|
|
|
|
|
|
|
} |
|
408
|
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
=cut |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
sub Handler :ATTR(CODE) { |
|
412
|
|
|
|
|
|
|
my ($pkg, $sym, $ref, $attr, $data, $phase) = @_; |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
no strict 'refs'; |
|
415
|
|
|
|
|
|
|
my $sub_name = *{$sym}{NAME}; |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
return if exists $subclass{$pkg}{$sub_name}; |
|
418
|
|
|
|
|
|
|
$subclass{$pkg}{$sub_name} = 1; |
|
419
|
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# FIXME - Appropriate carplevel. |
|
421
|
|
|
|
|
|
|
# FIXME - Is there a way to wrap anonymous coderefs? I don't think |
|
422
|
|
|
|
|
|
|
# so... |
|
423
|
|
|
|
|
|
|
unless (defined $sub_name) { |
|
424
|
|
|
|
|
|
|
croak ":Handler on anonymous coderefs not supported (nor needed)"; |
|
425
|
|
|
|
|
|
|
} |
|
426
|
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
no warnings 'redefine'; |
|
428
|
|
|
|
|
|
|
my $full_name = $pkg . '::' . $sub_name; |
|
429
|
|
|
|
|
|
|
*{$full_name} = POE::Callback->new( |
|
430
|
|
|
|
|
|
|
{ |
|
431
|
|
|
|
|
|
|
name => $full_name, |
|
432
|
|
|
|
|
|
|
code => $ref, |
|
433
|
|
|
|
|
|
|
} |
|
434
|
|
|
|
|
|
|
); |
|
435
|
|
|
|
|
|
|
} |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
=head2 expose OBJECT, LEXICAL [, LEXICAL[, LEXICAL ...]] |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
expose() is a function (not a method) that allows handlers to expose |
|
440
|
|
|
|
|
|
|
members of specific request or response OBJECT. Each member will be |
|
441
|
|
|
|
|
|
|
exposed as a particular LEXICAL variable. OBJECTs must inherit from |
|
442
|
|
|
|
|
|
|
POE::Request. |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
The LEXICAL's name is significant. The part of the variable name up |
|
445
|
|
|
|
|
|
|
to the leading underscore is treated as a prefix and ignored. The |
|
446
|
|
|
|
|
|
|
remainder of the variable name must match one of the OBJECT's member |
|
447
|
|
|
|
|
|
|
names. The sigil is also significant, and it is treated as part of |
|
448
|
|
|
|
|
|
|
the member name. |
|
449
|
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
The following example exposes the '$cookie' member of a POE::Request |
|
451
|
|
|
|
|
|
|
object as the '$sub_cookie' lexical variable. The exposed variable is |
|
452
|
|
|
|
|
|
|
then initialized. In doing so, the value stored into it is saved |
|
453
|
|
|
|
|
|
|
within the request's closure. It will be available whenever that |
|
454
|
|
|
|
|
|
|
request (or a response to it) is visible. |
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
use POE::Stage qw(expose); |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
sub do_request :Handler { |
|
459
|
|
|
|
|
|
|
my $req_subrequest = POE::Request->new( ... ); |
|
460
|
|
|
|
|
|
|
expose $req_subrequest, my $sub_cookie; |
|
461
|
|
|
|
|
|
|
$sub_cookie = "stored in the subrequest"; |
|
462
|
|
|
|
|
|
|
} |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
LEXICAL prefixes are useful for exposing the same member name from |
|
465
|
|
|
|
|
|
|
multiple OBJECTs within the same lexical scope. Otherwise the |
|
466
|
|
|
|
|
|
|
variable names would clash. |
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=cut |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
sub expose ($\[$@%];\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%\[$@%\[$@%]]]\[$@%]) { |
|
471
|
|
|
|
|
|
|
my $request = shift; |
|
472
|
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
# Validate that we're exposing a member of a POE::Request object. |
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
croak "Unknown request object '$request'" unless ( |
|
476
|
|
|
|
|
|
|
UNIVERSAL::isa($request, "POE::Request") |
|
477
|
|
|
|
|
|
|
); |
|
478
|
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
# Translate prefixed lexicals into POE::Request member names. Alias |
|
480
|
|
|
|
|
|
|
# the members to the lexicals, creating new members as necessary. |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
for (my $i = 0; $i < @_; $i++) { |
|
483
|
|
|
|
|
|
|
my $var_reference = $_[$i]; |
|
484
|
|
|
|
|
|
|
my $var_name = var_name(1, $var_reference); |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
unless ($var_name =~ /^([\$\@\%])([^_]+)_(\S+)/) { |
|
487
|
|
|
|
|
|
|
croak "'$var_name' is an illegal lexical name"; |
|
488
|
|
|
|
|
|
|
} |
|
489
|
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
my ($sigil, $prefix, $base_member_name) = ($1, $2, $3); |
|
491
|
|
|
|
|
|
|
my $member_name = $sigil . $base_member_name; |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
# Some prefixes fail. |
|
494
|
|
|
|
|
|
|
croak "can't expose $var_name" if $prefix =~ /^(arg|req|rsp|self)$/; |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
my $stage = POE::Request->_get_current_stage(); |
|
497
|
|
|
|
|
|
|
my $member_ref = $stage->_request_context_fetch( |
|
498
|
|
|
|
|
|
|
$request->get_id(), |
|
499
|
|
|
|
|
|
|
$member_name, |
|
500
|
|
|
|
|
|
|
); |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
# Autovivify a new member. |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
unless (defined $member_ref) { |
|
505
|
|
|
|
|
|
|
if ($sigil eq '$') { |
|
506
|
|
|
|
|
|
|
# Because I'm afraid to say $scalar = \$scalar. |
|
507
|
|
|
|
|
|
|
my $new_scalar = undef; |
|
508
|
|
|
|
|
|
|
$stage->_request_context_store( |
|
509
|
|
|
|
|
|
|
$request->get_id(), |
|
510
|
|
|
|
|
|
|
$member_name, |
|
511
|
|
|
|
|
|
|
$member_ref = \$new_scalar, |
|
512
|
|
|
|
|
|
|
); |
|
513
|
|
|
|
|
|
|
} |
|
514
|
|
|
|
|
|
|
elsif ($sigil eq '@') { |
|
515
|
|
|
|
|
|
|
$stage->_request_context_store( |
|
516
|
|
|
|
|
|
|
$request->get_id(), |
|
517
|
|
|
|
|
|
|
$member_name, |
|
518
|
|
|
|
|
|
|
$member_ref = [], |
|
519
|
|
|
|
|
|
|
); |
|
520
|
|
|
|
|
|
|
} |
|
521
|
|
|
|
|
|
|
elsif ($sigil eq '%') { |
|
522
|
|
|
|
|
|
|
$stage->_request_context_store( |
|
523
|
|
|
|
|
|
|
$request->get_id(), |
|
524
|
|
|
|
|
|
|
$member_name, |
|
525
|
|
|
|
|
|
|
$member_ref = {}, |
|
526
|
|
|
|
|
|
|
); |
|
527
|
|
|
|
|
|
|
} |
|
528
|
|
|
|
|
|
|
else { |
|
529
|
|
|
|
|
|
|
croak "'$var_name' has an odd sigil"; |
|
530
|
|
|
|
|
|
|
} |
|
531
|
|
|
|
|
|
|
} |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
# Alias that puppy. |
|
534
|
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
lexalias(1, $var_name, $member_ref); |
|
536
|
|
|
|
|
|
|
} |
|
537
|
|
|
|
|
|
|
} |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
1; |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
=head1 USING |
|
542
|
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
TODO - Describe how POE::Stage is used. Outline the general pattern |
|
544
|
|
|
|
|
|
|
for designing and subclassing. |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
=head1 DESIGN GOALS |
|
547
|
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
POE::Stage implements the most important and common design patterns |
|
549
|
|
|
|
|
|
|
for POE programs in a consistent and convenient way. |
|
550
|
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
POE::Stage hides nearly all of POE, including the need to create |
|
552
|
|
|
|
|
|
|
POE::Session objects and explicitly define event names and their |
|
553
|
|
|
|
|
|
|
handlers. The :Handler subroutine attribute defines which methods |
|
554
|
|
|
|
|
|
|
handle messages. There's never a need to guess which message types |
|
555
|
|
|
|
|
|
|
they handle: |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# Handle the "foo" message. |
|
558
|
|
|
|
|
|
|
sub foo :Handler { |
|
559
|
|
|
|
|
|
|
... |
|
560
|
|
|
|
|
|
|
} |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
POE::Stage simplifies message passing and response handling in at |
|
563
|
|
|
|
|
|
|
least three ways. Consider: |
|
564
|
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
my $request = POE::Request->new( |
|
566
|
|
|
|
|
|
|
stage => $target_stage, |
|
567
|
|
|
|
|
|
|
method => $target_method, |
|
568
|
|
|
|
|
|
|
args => \%arguments, |
|
569
|
|
|
|
|
|
|
on_response_x => "handler_x", |
|
570
|
|
|
|
|
|
|
on_response_y => "handler_y", |
|
571
|
|
|
|
|
|
|
on_response_z => "handler_z", |
|
572
|
|
|
|
|
|
|
); |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
First, it provides standard message clasess. Developers don't need to |
|
575
|
|
|
|
|
|
|
roll their own, potentially non-interoperable message-passing schemes. |
|
576
|
|
|
|
|
|
|
The named \%arguments are supplied and are available to each handler |
|
577
|
|
|
|
|
|
|
in a standard way, which is described later in the MAGICAL LEXICAL |
|
578
|
|
|
|
|
|
|
TOUR. |
|
579
|
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
Second, POE::Stage provides request-scoped closures via $req_foo, |
|
581
|
|
|
|
|
|
|
$rsp_foo, and expose(). Stages use these mechanisms to save and |
|
582
|
|
|
|
|
|
|
access data in specific request and response contexts, eliminating the |
|
583
|
|
|
|
|
|
|
need to do it explicitly. |
|
584
|
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
Third, response destinations are tied to the requests themselves. In |
|
586
|
|
|
|
|
|
|
the above example, responses of type "response_x" will be handled by |
|
587
|
|
|
|
|
|
|
"handler_x". The logic flow of a complex program is more readily |
|
588
|
|
|
|
|
|
|
apparent. It gets better, too. See HANDLER NAMING CONVENTIONS. |
|
589
|
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
The mechanisms of message passing and context management become |
|
591
|
|
|
|
|
|
|
implicit, allowing them to be extended transparently. This will be |
|
592
|
|
|
|
|
|
|
extended across processes, hopefully with few or no seams. |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
POE::Stage includes object-oriented classes for low-level event |
|
595
|
|
|
|
|
|
|
watchers. They simplify and standardize POE::Kernel's interface, and |
|
596
|
|
|
|
|
|
|
they allow watchers to be extended cleanly through normal OO |
|
597
|
|
|
|
|
|
|
techniques. The lifespan of each resource is tightly coupled to the |
|
598
|
|
|
|
|
|
|
lifespan of each object, so ownership and relevance are clearly |
|
599
|
|
|
|
|
|
|
indicated. |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
POE::Stage standardizes shutdown semantics for requests and stages. |
|
602
|
|
|
|
|
|
|
Requests are canceled by destroying their objects, and stages are shut |
|
603
|
|
|
|
|
|
|
down the same way. |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
POE::Stage simplifies the cleanup of complex, multi-stage activity. |
|
606
|
|
|
|
|
|
|
Resources for a particular request should be stored within its |
|
607
|
|
|
|
|
|
|
closure. Canceling the request triggers destruction of that closure |
|
608
|
|
|
|
|
|
|
and its contents, which in turn triggers the destruction of the |
|
609
|
|
|
|
|
|
|
resources allocated to that request. These resources include stages |
|
610
|
|
|
|
|
|
|
and requests created during the lifetime of the request. They too are |
|
611
|
|
|
|
|
|
|
canceled and freedm |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
=head1 MAGICAL LEXICAL TOUR |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
POE::Stage uses lexical aliasing to expose state data to message |
|
616
|
|
|
|
|
|
|
handlers, which are specified by either the :Handler method attribute |
|
617
|
|
|
|
|
|
|
or the use of an on_ prefix in the method's name. |
|
618
|
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
Lexical variable prefixes indicate the data's origin. For example, |
|
620
|
|
|
|
|
|
|
$arg_name is the "name" argument included with a message: |
|
621
|
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
my $request = POE::Request->new( |
|
623
|
|
|
|
|
|
|
method => "something", |
|
624
|
|
|
|
|
|
|
args => { name => "ralph" }, |
|
625
|
|
|
|
|
|
|
..., |
|
626
|
|
|
|
|
|
|
); |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
sub something :Handler { |
|
629
|
|
|
|
|
|
|
my $arg_name; # already contains "ralph" |
|
630
|
|
|
|
|
|
|
} |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
The full list of prefixes and data sources: |
|
633
|
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
=head2 The "arg_" lexical prefix, e.g., $arg_foo |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
Argument (parameter) "xyz". If an "args" parameter is passed to a |
|
637
|
|
|
|
|
|
|
POE::Request constructor, its value must be a reference to a hash. |
|
638
|
|
|
|
|
|
|
Usually it's an anonymous hashref. Anyway, the hash's members are |
|
639
|
|
|
|
|
|
|
named arguments to the message handler. See above for an example. |
|
640
|
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
=head2 The "req_" lexical prefix, e.g., $req_foo |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
An incoming request may trigger more than one handler, especially if a |
|
644
|
|
|
|
|
|
|
POE::Stage object calls itself, or sends sub-requests to a helper |
|
645
|
|
|
|
|
|
|
stage. The "req_" lexical prefix refers to data members within the |
|
646
|
|
|
|
|
|
|
current request's scope. Their values will magically reflect the |
|
647
|
|
|
|
|
|
|
proper request scope, regardless what that is. |
|
648
|
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
TODO - Example. |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
=head2 The "self_" lexical prefix, e.g., $self_foo |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
The "self" scope refers to the currently active POE::Stage object. |
|
654
|
|
|
|
|
|
|
Data may be stored there, in which case it's available from any and |
|
655
|
|
|
|
|
|
|
all requests handled by that object. This scope is useful for |
|
656
|
|
|
|
|
|
|
"singleton" or static data that must be shared between or persistent |
|
657
|
|
|
|
|
|
|
between all requests. |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
TODO - Example |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=head2 The "rsp_" lexical prefix, e.g., $rsp_foo |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
The "rsp" scope refers to data stored in a sub-request's scope, but |
|
664
|
|
|
|
|
|
|
from the response handler's point of view. That is, when persisting |
|
665
|
|
|
|
|
|
|
data between a request to a substage and its response, one should |
|
666
|
|
|
|
|
|
|
store the data in the substage's request, then retrieve it later from |
|
667
|
|
|
|
|
|
|
the corresponding "rsp" variable. |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
TODO - Example. |
|
670
|
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
=head2 The $self, $req, and $rsp lexicals |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
Certain variables are standard: $self refers to the current object; |
|
674
|
|
|
|
|
|
|
it need not be initialized from @_. $req refers to the higher-level |
|
675
|
|
|
|
|
|
|
request we're currently handling. When handling responses from |
|
676
|
|
|
|
|
|
|
substages, $rsp refers to those responses. |
|
677
|
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
All three variables are intended as invocatnts for method calls. |
|
679
|
|
|
|
|
|
|
Other prefixes exist to access data members within each object's |
|
680
|
|
|
|
|
|
|
scope. |
|
681
|
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
TODO - Example. |
|
683
|
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
The techniques used here have been abstracted and released as |
|
685
|
|
|
|
|
|
|
Lexical::Persistence. |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=head1 HANDLER NAMING CONVENTIONS |
|
688
|
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
Message handlers are defined in one of two ways. They may be named |
|
690
|
|
|
|
|
|
|
anything as long as they have a :Handler attribute, or they may be |
|
691
|
|
|
|
|
|
|
prefixed with "on_". In both cases, they gain lexical persistence |
|
692
|
|
|
|
|
|
|
magic, as discussed previously. |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
# Handle the "foo" message. |
|
695
|
|
|
|
|
|
|
sub foo :Handler { ... } |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
# Handle the "on_foo" and "foo" messages. |
|
698
|
|
|
|
|
|
|
sub on_foo { ... } |
|
699
|
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
The on_foo() method above handles both "on_foo" and "foo" messages. |
|
701
|
|
|
|
|
|
|
Given both a foo() and an on_foo(), however, on_foo() will take |
|
702
|
|
|
|
|
|
|
precedence. |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
Requests include on_* parameters that map response types to response |
|
705
|
|
|
|
|
|
|
handlers. For example, this request expects two return types, |
|
706
|
|
|
|
|
|
|
"success" and "failure". On success, the handle_success() method is |
|
707
|
|
|
|
|
|
|
called. On failure, handle_failure() is called. |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
my $req_subrequest = POE::Request->new( |
|
710
|
|
|
|
|
|
|
..., |
|
711
|
|
|
|
|
|
|
on_success => "handle_success", |
|
712
|
|
|
|
|
|
|
on_failure => "handle_failure", |
|
713
|
|
|
|
|
|
|
); |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
Response types are specified by the "type" parameter to $req->emit() |
|
716
|
|
|
|
|
|
|
and $req->return(). "emit" and "return" are the default types for |
|
717
|
|
|
|
|
|
|
emit() and return(), respectively. |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
Requests can also have roles, which are usually descriptive of the |
|
720
|
|
|
|
|
|
|
transaction. For example, consider a DNS request for a web client |
|
721
|
|
|
|
|
|
|
component: |
|
722
|
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
my $req_resolve = POE::Request->new( |
|
724
|
|
|
|
|
|
|
..., |
|
725
|
|
|
|
|
|
|
role => "resolver", |
|
726
|
|
|
|
|
|
|
); |
|
727
|
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
This is the role of the request, not of the stage that will handle it. |
|
729
|
|
|
|
|
|
|
In this case, there are no on_* parameters. Success and failure come |
|
730
|
|
|
|
|
|
|
back to methods named "on_" . $request_role . "_" . $response_type. |
|
731
|
|
|
|
|
|
|
In the previous example, they are: |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
sub on_resolver_success { ... } |
|
734
|
|
|
|
|
|
|
sub on_resolver_failure { ... } |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
When subclassing a POE::Stage class, it's sometimes useful to |
|
737
|
|
|
|
|
|
|
intercept emit() and return() messages. The subclass may implement |
|
738
|
|
|
|
|
|
|
handlers directly, or it may override or extend the response. This is |
|
739
|
|
|
|
|
|
|
done by defining "on_my_" . $response_type methdos in the subclass. |
|
740
|
|
|
|
|
|
|
For example, a TCP connection stage might emit an "input" event, like |
|
741
|
|
|
|
|
|
|
so: |
|
742
|
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
sub on_socket_readable { |
|
744
|
|
|
|
|
|
|
...; |
|
745
|
|
|
|
|
|
|
$req->emit( type => "input", input => $data ); |
|
746
|
|
|
|
|
|
|
} |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
A subclass might implement the code to handle the input. It can do so |
|
749
|
|
|
|
|
|
|
by defining on_my_input(): |
|
750
|
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
sub on_my_input { |
|
752
|
|
|
|
|
|
|
# send a response here |
|
753
|
|
|
|
|
|
|
} |
|
754
|
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
Messages intercepted like this will not be rethrown automatically to |
|
756
|
|
|
|
|
|
|
the caller. If that's desired, on_my_input() will need to emit() or |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
TODO - Make a better example. Something that can tie all these things |
|
759
|
|
|
|
|
|
|
together conceptually. |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
=head1 BUGS |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
POE::Stage is not ready for production. Check back here early and |
|
764
|
|
|
|
|
|
|
often to find out when it will be. Please contact the author if you |
|
765
|
|
|
|
|
|
|
would like to see POE::Stage production-ready sooner. |
|
766
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=head1 BUG TRACKER |
|
768
|
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=POE-Stage |
|
770
|
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
=head1 REPOSITORY |
|
772
|
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
http://thirdlobe.com/svn/poe-stage/ |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=head1 OTHER RESOURCES |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
http://search.cpan.org/dist/POE-Stage/ |
|
778
|
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
780
|
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
POE::Stage is the base class for message-driven objects. |
|
782
|
|
|
|
|
|
|
POE::Request is the base class for POE::Stage messages. |
|
783
|
|
|
|
|
|
|
POE::Watcher is the base class for event watchers. |
|
784
|
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
L - POE::Stage is hosted |
|
786
|
|
|
|
|
|
|
here. |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
L - SEDA, the Staged |
|
789
|
|
|
|
|
|
|
Event Driven Architecture. It's Java, though. |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=head1 AUTHORS |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Rocco Caputo. |
|
794
|
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
=head1 LICENSE |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
POE::Stage is Copyright 2005-2009 by Rocco Caputo. All rights are |
|
798
|
|
|
|
|
|
|
reserved. You may use, modify, and/or distribute this module under |
|
799
|
|
|
|
|
|
|
the same terms as Perl itself. |
|
800
|
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=cut |