File Coverage

blib/lib/XAS/Lib/POE/Session.pm
Criterion Covered Total %
statement 9 82 10.9
branch 0 6 0.0
condition n/a
subroutine 3 20 15.0
pod 9 9 100.0
total 21 117 17.9


line stmt bran cond sub pod time code
1             package XAS::Lib::POE::Session;
2              
3             our $VERSION = '0.04';
4              
5 1     1   880 use POE;
  1         1  
  1         7  
6              
7             use XAS::Class
8 1         11 debug => 0,
9             version => $VERSION,
10             base => 'XAS::Base',
11             mixin => 'XAS::Lib::Mixins::Handlers',
12             utils => 'weaken dotid',
13             accessors => 'session',
14             vars => {
15             PARAMS => {
16             -alias => { optional => 1, default => 'session' },
17             }
18             }
19 1     1   253 ;
  1         2  
20              
21 1     1   1255 use Data::Dumper;
  1         4727  
  1         696  
22              
23             # ----------------------------------------------------------------------
24             # Public Events
25             # ----------------------------------------------------------------------
26              
27             # ----------------------------------------------------------------------
28             # Public Methods
29             # ----------------------------------------------------------------------
30              
31             sub session_initialize {
32 0     0 1   my $self = shift;
33              
34 0           $poe_kernel->sig(HUP => 'session_interrupt');
35 0           $poe_kernel->sig(INT => 'session_interrupt');
36 0           $poe_kernel->sig(TERM => 'session_interrupt');
37 0           $poe_kernel->sig(QUIT => 'session_interrupt');
38              
39             }
40              
41             sub session_startup {
42 0     0 1   my $self = shift;
43              
44             }
45              
46             sub session_shutdown {
47 0     0 1   my $self = shift;
48              
49             }
50              
51             sub session_reload {
52 0     0 1   my $self = shift;
53              
54 0           $poe_kernel->sig_handled();
55              
56             }
57              
58             sub session_interrupt {
59 0     0 1   my $self = shift;
60 0           my $signal = shift;
61              
62 0           my $alias = $self->alias;
63              
64 0           $self->log->debug("$alias: session_interrupt()");
65 0           $self->log->warn_msg('session_signaled', $alias, $signal);
66              
67 0 0         if ($signal eq 'HUP') {
68              
69 0           $self->session_reload();
70              
71             } else {
72              
73 0           $self->session_shutdown();
74              
75             }
76              
77             }
78              
79             sub session_stop {
80 0     0 1   my $self = shift;
81              
82             }
83              
84             sub session_exception {
85 0     0 1   my $self = shift;
86 0           my $ex = shift;
87              
88 0           my $alias = $self->alias;
89              
90 0           $self->log->debug("$alias: session_exception() - session");
91 0           $self->error_handler($ex, $alias);
92              
93             }
94              
95             sub run {
96 0     0 1   my $self = shift;
97              
98 0           $poe_kernel->run();
99              
100             }
101              
102             # ----------------------------------------------------------------------
103             # Private Methods
104             # ----------------------------------------------------------------------
105              
106             sub init {
107 0     0 1   my $class = shift;
108              
109             # walk the chain
110              
111 0           my $self = $class->SUPER::init(@_);
112              
113             # set up the session
114              
115 0           $self->{'session'} = POE::Session->create(
116             object_states => [
117             $self => {
118             _start => '_session_start',
119             _stop => '_session_stop',
120             session_init => '_session_init',
121             session_reload => '_session_reload',
122             session_startup => '_session_startup',
123             session_shutdown => '_session_shutdown',
124             session_interrupt => '_session_interrupt',
125             session_exception => '_session_exception',
126             },
127             ]
128             );
129              
130 0           weaken($self->{'session'});
131              
132 0           return $self;
133              
134             }
135              
136             # ----------------------------------------------------------------------
137             # Private Events
138             # ----------------------------------------------------------------------
139              
140             sub _session_start {
141 0     0     my ($self) = $_[OBJECT];
142              
143 0           my $alias = $self->alias;
144              
145 0           $self->log->debug("$alias: _session_start()");
146              
147 0 0         if ((my $rc = $poe_kernel->alias_set($alias)) > 0) {
148              
149 0           $self->throw_msg(
150             dotid($self->class) . '._session_start.noalias',
151             'session_noalias',
152             $alias
153             );
154              
155             }
156              
157 0           $poe_kernel->sig('DIE', 'session_exception');
158 0           $poe_kernel->post($alias, 'session_init');
159              
160             }
161              
162             sub _session_init {
163 0     0     my ($self) = $_[OBJECT];
164              
165 0           my $alias = $self->alias;
166              
167 0           $self->log->debug("$alias: _session_init()");
168              
169 0           $self->session_initialize();
170              
171 0           $poe_kernel->post($alias, 'session_startup');
172              
173             }
174              
175             sub _session_startup {
176 0     0     my ($self) = $_[OBJECT];
177              
178 0           my $alias = $self->alias;
179              
180 0           $self->log->debug("$alias: _session_startup()");
181              
182 0           $self->session_startup();
183              
184             }
185              
186             sub _session_shutdown {
187 0     0     my ($self) = $_[OBJECT];
188              
189 0           my $alias = $self->alias;
190              
191 0           $self->log->debug("$alias: _session_shutdown()");
192              
193 0           $self->session_shutdown();
194              
195             }
196              
197             sub _session_reload {
198 0     0     my ($self) = $_[OBJECT];
199              
200 0           my $alias = $self->alias;
201              
202 0           $self->log->debug("$alias: _session_reload()");
203              
204 0           $self->session_reload();
205              
206             }
207              
208             sub _session_stop {
209 0     0     my ($self) = $_[OBJECT];
210              
211 0           my $alias = $self->alias;
212              
213 0           $self->log->debug("$alias: _session_stop()");
214              
215 0           $poe_kernel->sig('DIE');
216              
217 0           $self->session_stop();
218              
219 0           $poe_kernel->alias_remove($self->alias);
220              
221             }
222              
223             sub _session_interrupt {
224 0     0     my ($self, $signal) = @_[OBJECT,ARG0];
225              
226 0           my $alias = $self->alias;
227              
228 0           $self->log->debug("$alias: _session_interrupt()");
229              
230 0           $self->session_interrupt($signal);
231              
232             }
233              
234             sub _session_exception {
235 0     0     my ($self, $sig, $ex) = @_[OBJECT,ARG0,ARG1];
236              
237 0           my $alias = $self->alias;
238              
239 0           $self->log->debug("$alias: _session_exception()");
240              
241 0           $poe_kernel->sig_handled();
242              
243 0 0         if ($ex->{'source_session'} ne $_[SESSION]) {
244              
245 0           $self->log->debug(sprintf('%s: sending execption to: %s', $alias, $ex->{'source_session'}));
246 0           $poe_kernel->post($ex->{'source_session'}, 'session_exception', $sig, $ex);
247              
248             } else {
249              
250 0           $self->log->debug(sprintf('%s: handling execption: %s', $alias, $ex->{'error_str'}));
251 0           $self->session_exception($ex->{'error_str'});
252              
253             }
254              
255             }
256              
257             1;
258              
259             __END__
260              
261             =head1 NAME
262              
263             XAS::Lib::POE::Session - The base class for all POE Sessions.
264              
265             =head1 SYNOPSIS
266              
267             my $session = XAS::Lib::POE::Session->new(
268             -alias => 'name',
269             );
270              
271             =head1 DESCRIPTION
272              
273             This module provides an object based POE session. This object will perform
274             the necessary actions for the lifetime of the session. This includes signal
275             handling. Due to the nature of POE events, they can not be inherited from,
276             but the event handlers can call methods which can. To effectively inherit from
277             this class, you need to walk the SUPER chain to process overridden methods.
278              
279             =head1 METHODS
280              
281             =head2 new
282              
283             This method initializes the modules. It inherits from L<XAS::Base|XAS::Base>
284             and takes these parameters:
285              
286             =over 4
287              
288             =item B<-alias>
289              
290             The name of the POE session, defaults to 'session'.
291              
292             =back
293              
294             =head2 session_initialize
295              
296             This method is called after session startup and is used for initialization.
297             This initialization may include defining additonal event. By default it sets
298             up signal handling for these signals:
299              
300             HUP
301             INT
302             TERM
303             QUIT
304              
305             =head2 session_startup
306              
307             This method is where actual processing starts to happen.
308              
309             =head2 session_shutdown
310              
311             This method is called when your session is shutting down.
312              
313             =head2 session_reload
314              
315             This method should perform reload actions for the session. By default it
316             calls POE's sig_handled() method which terminates further handling of the
317             HUP signal.
318              
319             =head2 session_interrupt($signal)
320              
321             This is called when the process receives a signal. By default, when a
322             HUP signal is received it will call session_reload() otherwise it
323             calls session_shutdown().
324              
325             =over 4
326              
327             =item B<$signal>
328              
329             The signal that was trapped.
330              
331             =back
332              
333             =head2 session_exception($ex)
334              
335             This is called when a exception or "die" happens. By default, this just
336             prints out the messages and continues on. This provides a default exception
337             handler for a session.
338              
339             =over 4
340              
341             =item B<$ex>
342              
343             The exception hash that is generated by POE. The field 'error_str' is used
344             to print out the exception message.
345              
346             =back
347              
348             =head2 session_stop
349              
350             This method is called when POE starts doing "_stop" activities.
351              
352             =head2 run
353              
354             A short cut to POE's run() method.
355              
356             =head1 SEE ALSO
357              
358             =over 4
359              
360             =item L<XAS|XAS>
361              
362             =back
363              
364             =head1 AUTHOR
365              
366             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
367              
368             =head1 COPYRIGHT AND LICENSE
369              
370             Copyright (C) 2014 Kevin L. Esteb
371              
372             This is free software; you can redistribute it and/or modify it under
373             the terms of the Artistic License 2.0. For details, see the full text
374             of the license at http://www.perlfoundation.org/artistic_license_2_0.
375             =cut