Skip to content

HTTPS clone URL

Subversion checkout URL

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