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