Skip to content

HTTPS clone URL

Subversion checkout URL

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