Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 67 lines (56 sloc) 1.537 kB
fcaef26 @rcaputo Support posting events to POE components from stages.
authored
1 package PoCoEvent;
2
3 # A component that accepts an event name to which return messages are
4 # posted. In this case, the component builds a postback for the
5 # calling session, which makes a more complex but satisfying test
6 # case.
7 #
8 # TODO - Refactor into a base class and subclasses. This is only a
9 # couple lines different from PoCoPostback.
10
11 use warnings;
12 use strict;
13 use POE;
14
15 sub new {
16 my $class = shift;
17
18 my $self = bless { }, $class;
19
20 my $result = 'aaaaaaaa';
21
22 POE::Session->create(
23 inline_states => {
24 _start => sub {
25 # Set an alias based on the object that owns us.
26 $_[HEAP]{alias} = "$self";
27 $_[KERNEL]->alias_set("$self");
28 },
29 shutdown => sub {
30 # Shutdown is triggered by the object DESTROY.
31 # Remove the alias, and gracefully exit when all pending
32 # timers are done.
33 $_[KERNEL]->alias_remove($_[HEAP]{alias});
34 },
35 request => sub {
36 # Handle a request. Feign some work.
37 my $event = $_[ARG0];
38 my $postback = $_[SENDER]->postback($event);
39 $_[KERNEL]->delay_add(
40 work_done => rand(3) => $postback => $result++
41 );
42 },
43 work_done => sub {
44 # When the work is done, post back a result.
45 my ($postback, $result) = @_[ARG0, ARG1];
46 $postback->($result);
47 },
48 },
49 );
50
51 return $self;
52 }
53
54 # Clean up the session on destruction.
55 sub DESTROY {
56 my $self = shift;
57 $poe_kernel->call("$self", "shutdown");
58 }
59
60 # Convenience method. Hide POE::Kernel->post.
61 sub request {
62 my ($self, $postback) = @_;
63 $poe_kernel->post("$self", "request", $postback);
64 }
65
66 1;
Something went wrong with that request. Please try again.