blib/lib/XAS/Lib/Session.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 105 CC 3 SCAR 10.4
CC
3
Cov
0%
session_interrupt
6.0
_session_start
6.0
session_initialize
2.0
CRAP 2.8
1 package XAS::Lib::Session;
2  
3 our $VERSION = '0.03';
4  
5 use POE;
6  
7 use XAS::Class
8 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 utils => 'weaken',
12 accessors => 'session',
13 vars => {
14 PARAMS => {
15 -alias => { optional => 1, default => 'session' },
16 }
17 }
18 ;
19  
20 # ----------------------------------------------------------------------
21 # Public Events
22 # ----------------------------------------------------------------------
23  
24 # ----------------------------------------------------------------------
25 # Public Methods
26 # ----------------------------------------------------------------------
27  
28 sub session_initialize {
29 my $self = shift;
30  
31 $poe_kernel->sig(HUP => 'session_interrupt');
32 $poe_kernel->sig(INT => 'session_interrupt');
33 $poe_kernel->sig(TERM => 'session_interrupt');
34 $poe_kernel->sig(QUIT => 'session_interrupt');
35  
36 }
37  
38 sub session_startup {
39 my $self = shift;
40  
41 }
42  
43 sub session_shutdown {
44 my $self = shift;
45  
46 }
47  
48 sub session_reload {
49 my $self = shift;
50  
51 $poe_kernel->sig_handled();
52  
53 }
54  
55 sub session_interrupt {
56 my $self = shift;
57 my $signal = shift;
58  
59 my $alias = $self->alias;
60  
61 $self->log->debug("$alias: session_interrupt()");
62 $self->log->warn_msg('signaled', $alias, $signal);
63  
64 if ($signal eq 'HUP') {
65  
66 $self->session_reload();
67  
68 } else {
69  
70 $self->session_shutdown();
71  
72 }
73  
74 }
75  
76 sub session_stop {
77 my $self = shift;
78  
79 }
80  
81 sub run {
82 my $self = shift;
83  
84 $poe_kernel->run();
85  
86 }
87  
88 # ----------------------------------------------------------------------
89 # Private Methods
90 # ----------------------------------------------------------------------
91  
92 sub init {
93 my $class = shift;
94  
95 # initialize POE, this removes an error message if a failure happens later
96  
97 $poe_kernel->run();
98  
99 # walk the chain
100  
101 my $self = $class->SUPER::init(@_);
102  
103 # set up the session
104  
105 $self->{session} = POE::Session->create(
106 object_states => [
107 $self => {
108 _start => '_session_start',
109 _stop => '_session_stop',
110 session_init => '_session_init',
111 session_reload => '_session_reload',
112 session_startup => '_session_startup',
113 session_shutdown => '_session_shutdown',
114 session_interrupt => '_session_interrupt',
115 },
116 ]
117 );
118  
119 weaken($self->{session});
120  
121 return $self;
122  
123 }
124  
125 # ----------------------------------------------------------------------
126 # Private Events
127 # ----------------------------------------------------------------------
128  
129 sub _session_start {
130 my ($self) = $_[OBJECT];
131  
132 my $alias = $self->alias;
133  
134 $self->log->debug("$alias: _session_start()");
135  
136 if ((my $rc = $poe_kernel->alias_set($alias)) > 0) {
137  
138 $self->throw_msg(
139 'xas.session._session_start.noalias',
140 'noalias',
141 $alias
142 );
143  
144 }
145  
146 $poe_kernel->post($alias, 'session_init');
147  
148 }
149  
150 sub _session_init {
151 my ($self) = $_[OBJECT];
152  
153 my $alias = $self->alias;
154  
155 $self->log->debug("$alias: _session_init()");
156  
157 $self->session_initialize();
158  
159 $poe_kernel->post($alias, 'session_startup');
160  
161 }
162  
163 sub _session_startup {
164 my ($self) = $_[OBJECT];
165  
166 my $alias = $self->alias;
167  
168 $self->log->debug("$alias: _session_startup()");
169  
170 $self->session_startup();
171  
172 }
173  
174 sub _session_shutdown {
175 my ($self) = $_[OBJECT];
176  
177 my $alias = $self->alias;
178  
179 $self->log->debug("$alias: _session_shutdown()");
180  
181 $self->session_shutdown();
182  
183 }
184  
185 sub _session_reload {
186 my ($self) = $_[OBJECT];
187  
188 my $alias = $self->alias;
189  
190 $self->log->debug("$alias: _session_reload()");
191  
192 $self->session_reload();
193  
194 }
195  
196 sub _session_stop {
197 my ($self) = $_[OBJECT];
198  
199 my $alias = $self->alias;
200  
201 $self->log->debug("$alias: _session_stop()");
202  
203 $self->session_stop();
204  
205 $poe_kernel->alias_remove($self->alias);
206  
207 }
208  
209 sub _session_interrupt {
210 my ($self, $signal) = @_[OBJECT,ARG0];
211  
212 my $alias = $self->alias;
213  
214 $self->log->debug("$alias: _session_interrupt()");
215  
216 $self->session_interrupt($signal);
217  
218 }
219  
220 1;
221  
222 __END__
223  
224 =head1 NAME
225  
226 XAS::Lib::Session - The base class for all POE Sessions.
227  
228 =head1 SYNOPSIS
229  
230 my $session = XAS::Lib::Session->new(
231 -alias => 'name',
232 );
233  
234 =head1 DESCRIPTION
235  
236 This module provides an object based POE session. This object will perform
237 the necessary actions for the lifetime of the session. This includes handling
238 signals. The following signals INT, TERM, QUIT will trigger the 'shutdown'
239 event which invokes the session_cleanup() method. The HUP signal will invoke
240 the session_reload() method. This module inherits from XAS::Base.
241  
242 =head1 METHODS
243  
244 =head2 session_initialize
245  
246 This is where the session should do whatever initialization it needs. This
247 initialization may include defining additional events.
248  
249 =head2 session_cleanup
250  
251 This method should perform cleanup actions for the session. This is triggered
252 by a "shutdown" event.
253  
254 =head2 session_reload
255  
256 This method should perform reload actions for the session. By default it
257 calls $kernel->sig_handled() which terminates further handling of the HUP
258 signal.
259  
260 =head2 session_stop
261  
262 This method should perform stop actions for the session. This is triggered
263 by a "_stop" event.
264  
265 =head1 PUBLIC EVENTS
266  
267 The following public events are defined for the session.
268  
269 =head2 session_startup
270  
271 This event should start whatever processing the session will do. It is passed
272 two parameters:
273  
274 =head2 session_shutdown
275  
276 When you send this event to the session, it will invoke the session_cleanup()
277 method.
278  
279 =head1 PRIVATE EVENTS
280  
281 The following events are used internally:
282  
283 session_init
284 session_interrupt
285 session_reload
286 shutdown
287  
288 They should only be used with caution.
289  
290 =head1 SEE ALSO
291  
292 =over 4
293  
294 =item L<XAS|XAS>
295  
296 =back
297  
298 =head1 AUTHOR
299  
300 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
301  
302 =head1 COPYRIGHT AND LICENSE
303  
304 Copyright (C) 2014 Kevin L. Esteb
305  
306 This library is free software; you can redistribute it and/or modify
307 it under the same terms as Perl itself, either Perl version 5.8.8 or,
308 at your option, any later version of Perl 5 you may have available.
309  
310 See L<http://dev.perl.org/licenses/> for more information.
311  
312 =cut