Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 61 lines (50 sloc) 1.257 kB
bf57dfb @rcaputo Support POE::Components that wish to be given postbacks.
authored
1 package PoCoPostback;
5b0127b @rcaputo Remove emit() from roles.
authored
2 # vim: ts=2 sw=2 noexpandtab
bf57dfb @rcaputo Support POE::Components that wish to be given postbacks.
authored
3
4 # A component that accepts postbacks to return messages.
5
6 use warnings;
7 use strict;
8 use POE;
9
10 sub new {
11 my $class = shift;
12
13 my $self = bless { }, $class;
14
15 my $result = 'aaaaaaaa';
16
17 POE::Session->create(
18 inline_states => {
19 _start => sub {
20 # Set an alias based on the object that owns us.
21 $_[HEAP]{alias} = "$self";
22 $_[KERNEL]->alias_set("$self");
23 },
24 shutdown => sub {
25 # Shutdown is triggered by the object DESTROY.
26 # Remove the alias, and gracefully exit when all pending
27 # timers are done.
28 $_[KERNEL]->alias_remove($_[HEAP]{alias});
29 },
30 request => sub {
31 # Handle a request. Feign some work.
32 my $postback = $_[ARG0];
33 $_[KERNEL]->delay_add(
34 work_done => rand(3) => $postback => $result++
35 );
36 },
37 work_done => sub {
38 # When the work is done, post back a result.
39 my ($postback, $result) = @_[ARG0, ARG1];
40 $postback->($result);
41 },
42 },
43 );
44
45 return $self;
46 }
47
48 # Clean up the session on destruction.
49 sub DESTROY {
50 my $self = shift;
51 $poe_kernel->call("$self", "shutdown");
52 }
53
54 # Convenience method. Hide POE::Kernel->post.
55 sub request {
56 my ($self, $postback) = @_;
57 $poe_kernel->post("$self", "request", $postback);
58 }
59
60 1;
Something went wrong with that request. Please try again.