File Coverage

blib/lib/POE/Session/MessageBased.pm
Criterion Covered Total %
statement 36 54 66.6
branch 4 12 33.3
condition n/a
subroutine 11 18 61.1
pod n/a
total 51 84 60.7


line stmt bran cond sub pod time code
1             # License and documentation are after __END__.
2              
3             # This is prototype code. It rummages around in its base class'
4             # namespace for constants, among other unsavory things.
5              
6             # Artur Bergman suggests keeping a static Message object in the
7             # Session itself, and reusing that instead of building and destroying
8             # messages each time. That could be a little faster.
9              
10             package POE::Session::MessageBased;
11              
12 1     1   189796 use warnings;
  1         3  
  1         37  
13 1     1   4 use strict;
  1         2  
  1         41  
14              
15 1     1   4 use vars qw($VERSION);
  1         11  
  1         44  
16             $VERSION = '0.111';
17              
18 1     1   840 use POE;
  1         580  
  1         6  
19 1     1   6022 use base qw(POE::Session);
  1         3  
  1         137  
20 1     1   6 use POSIX qw(ENOSYS);
  1         2  
  1         12  
21              
22             sub _invoke_state {
23 22     22   5018 my ($self, $source_session, $state, $etc, $file, $line) = @_;
24              
25             # Trace the state invocation if tracing is enabled.
26              
27 22 50       62 if ($self->[POE::Session::SE_OPTIONS]->{+POE::Session::OPT_TRACE}) {
28 0         0 warn(
29             $POE::Kernel::poe_kernel->ID_session_to_id($self),
30             " -> $state (from $file at $line)\n"
31             );
32             }
33              
34             # The desired destination state doesn't exist in this session.
35             # Attempt to redirect the state transition to _default.
36              
37 22 50       58 unless (exists $self->[POE::Session::SE_STATES]->{$state}) {
38              
39             # There's no _default either; redirection's not happening today.
40             # Drop the state transition event on the floor, and optionally
41             # make some noise about it.
42              
43 0 0       0 unless (
44             exists
45             $self->[POE::Session::SE_STATES]->{+POE::Session::EN_DEFAULT}
46             ) {
47 0         0 $! = ENOSYS;
48 0 0       0 if ($self->[POE::Session::SE_OPTIONS]->{+POE::Session::OPT_DEFAULT}) {
49 0         0 warn(
50             "a '$state' state was sent from $file at $line to session ",
51             $POE::Kernel::poe_kernel->ID_session_to_id($self),
52             ", but session ",
53             $POE::Kernel::poe_kernel->ID_session_to_id($self),
54             " has neither that state nor a _default state to handle it\n"
55             );
56             }
57 0         0 return undef;
58             }
59              
60             # If we get this far, then there's a _default state to redirect
61             # the transition to. Trace the redirection.
62              
63 0 0       0 if ($self->[POE::Session::SE_OPTIONS]->{+POE::Session::OPT_TRACE}) {
64 0         0 warn(
65             $POE::Kernel::poe_kernel->ID_session_to_id($self),
66             " -> $state redirected to _default\n"
67             );
68             }
69              
70             # Transmogrify the original state transition into a corresponding
71             # _default invocation.
72              
73 0         0 $etc = [ $state, $etc ];
74 0         0 $state = POE::Session::EN_DEFAULT;
75             }
76              
77             # If we get this far, then the state can be invoked. So invoke it
78             # already!
79              
80             # Inline states are invoked this way.
81              
82 22 100       62 if (ref($self->[POE::Session::SE_STATES]->{$state}) eq 'CODE') {
83 11         41 my $message = POE::Session::Message->new(
84             undef, # object
85             $self, # session
86             $POE::Kernel::poe_kernel, # kernel
87             $self->[POE::Session::SE_NAMESPACE], # heap
88             $state, # state
89             $source_session, # sender
90             undef, # unused #6
91             $file, # caller file name
92             $line, # caller file line
93             $etc # args
94             );
95              
96 11         39 return $self->[POE::Session::SE_STATES]->{$state}->($message, @$etc);
97             }
98              
99             # Package and object states are invoked this way.
100              
101 11         16 my ($object, $method) = @{$self->[POE::Session::SE_STATES]->{$state}};
  11         25  
102 11         37 my $message = POE::Session::Message->new(
103             $object, # object
104             $self, # session
105             $POE::Kernel::poe_kernel, # kernel
106             $self->[POE::Session::SE_NAMESPACE], # heap
107             $state, # state
108             $source_session, # sender
109             undef, # unused #6
110             $file, # caller file name
111             $line, # caller file line
112             $etc # args
113             );
114              
115             # Package/object are implied.
116 11         39 return $object->$method($message, @$etc);
117             }
118              
119             package POE::Session::Message;
120              
121 1     1   536 use POE::Session;
  1         3  
  1         6  
122              
123             sub new {
124 22     22   29 my $class = shift;
125 22         94 my $self = bless [ @_ ], $class;
126 22         47 return $self;
127             }
128              
129 10     10   81 sub object { $_[0]->[OBJECT] }
130 0     0   0 sub session { $_[0]->[SESSION] }
131 18     18   4968 sub kernel { $_[0]->[KERNEL] }
132 0     0     sub heap { $_[0]->[HEAP] }
133 0     0     sub state { $_[0]->[STATE] }
134 0     0     sub sender { $_[0]->[SENDER] }
135 0     0     sub caller_file { $_[0]->[CALLER_FILE] }
136 0     0     sub caller_line { $_[0]->[CALLER_LINE] }
137 0     0     sub args { @{$_[0]->[ARG0]} }
  0            
138              
139             1;
140              
141             __END__