| 1 | package XAS::Lib::Service; | ||
| 2 | |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | |||
| 5 | use POE; | ||
| 6 | |||
| 7 | use XAS::Class | ||
| 8 | debug => 0, | ||
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Lib::Session', | ||
| 11 | vars => { | ||
| 12 | PARAMS => { | ||
| 13 | -alias => { optional => 1, default => 'service' } | ||
| 14 | } | ||
| 15 | } | ||
| 16 | ; | ||
| 17 | |||
| 18 | # ---------------------------------------------------------------------- | ||
| 19 | # Public Events | ||
| 20 | # ---------------------------------------------------------------------- | ||
| 21 | |||
| 22 | # ---------------------------------------------------------------------- | ||
| 23 | # Public Methods | ||
| 24 | # ---------------------------------------------------------------------- | ||
| 25 | |||
| 26 | sub session_initialize { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | $poe_kernel->state('session_idle', $self, '_session_idle'); | ||
| 30 | $poe_kernel->state('session_pause', $self, '_session_pause'); | ||
| 31 | $poe_kernel->state('session_resume', $self, '_session_resume'); | ||
| 32 | $poe_kernel->state('session_status', $self, '_session_status'); | ||
| 33 | |||
| 34 | $poe_kernel->sig('HUP', 'session_interrupt'); | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | sub session_idle { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | my $alias = $self->alias; | ||
| 42 | |||
| 43 | $self->log->debug("$alias: session_idle()"); | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | sub session_pause { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | my $alias = $self->alias; | ||
| 51 | |||
| 52 | $self->log->debug("$alias: session_pause()"); | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub session_resume { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | my $alias = $self->alias; | ||
| 60 | |||
| 61 | $self->log->debug("$alias: session_resume()"); | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | sub session_status { | ||
| 66 | my $self = shift; | ||
| 67 | my $status = shift; | ||
| 68 | |||
| 69 | my $alias = $self->alias; | ||
| 70 | |||
| 71 | $self->log->debug("$alias: session_status()"); | ||
| 72 | |||
| 73 | $self->{'__status'} = $status if (defined($status)); | ||
| 74 | |||
| 75 | return $self->{'__status'}; | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | # ---------------------------------------------------------------------- | ||
| 80 | # Public Accessors | ||
| 81 | # ---------------------------------------------------------------------- | ||
| 82 | |||
| 83 | # ---------------------------------------------------------------------- | ||
| 84 | # Private Methods | ||
| 85 | # ---------------------------------------------------------------------- | ||
| 86 | |||
| 87 | # ---------------------------------------------------------------------- | ||
| 88 | # Private Events | ||
| 89 | # ---------------------------------------------------------------------- | ||
| 90 | |||
| 91 | sub _session_init { | ||
| 92 | my ($self) = $_[OBJECT]; | ||
| 93 | |||
| 94 | my $alias = $self->alias; | ||
| 95 | |||
| 96 | $self->log->debug("$alias: _session_init()"); | ||
| 97 | |||
| 98 | $self->session_initialize(); | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | sub _session_idle { | ||
| 103 | my ($self) = $_[OBJECT]; | ||
| 104 | |||
| 105 | my $alias = $self->alias; | ||
| 106 | |||
| 107 | $self->log->debug("$alias: session_idle()"); | ||
| 108 | |||
| 109 | $self->session_idle(); | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | sub _session_pause { | ||
| 114 | my ($self) = $_[OBJECT]; | ||
| 115 | |||
| 116 | my $alias = $self->alias; | ||
| 117 | |||
| 118 | $self->log->debug("$alias: session_pause()"); | ||
| 119 | |||
| 120 | $self->session_pause(); | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | sub _session_resume { | ||
| 125 | my ($self) = $_[OBJECT]; | ||
| 126 | |||
| 127 | my $alias = $self->alias; | ||
| 128 | |||
| 129 | $self->log->debug("$alias: _session_resume()"); | ||
| 130 | |||
| 131 | $self->session_resume(); | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | sub _session_status { | ||
| 136 | my ($self, $status) = @_[OBJECT, ARG0]; | ||
| 137 | |||
| 138 | my $alias = $self->alias; | ||
| 139 | |||
| 140 | $self->log->debug("$alias: _session_status()"); | ||
| 141 | |||
| 142 | $self->session_status($status); | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ | ||
| 149 | |||
| 150 | =head1 NAME | ||
| 151 | |||
| 152 | XAS::Lib::Service - The base class for all POE Sessions. | ||
| 153 | |||
| 154 | =head1 SYNOPSIS | ||
| 155 | |||
| 156 | my $session = XAS::Lib::Session->new( | ||
| 157 | -alias => 'name', | ||
| 158 | ); | ||
| 159 | |||
| 160 | =head1 DESCRIPTION | ||
| 161 | |||
| 162 | This module provides an object based POE session. This object will perform | ||
| 163 | the necessary actions for the lifetime of the session. This includes handling | ||
| 164 | signals. The following signals INT, TERM, QUIT will trigger the 'shutdown' | ||
| 165 | event which invokes the session_cleanup() method. The HUP signal will invoke | ||
| 166 | the session_reload() method. This module inherits from XAS::Base. | ||
| 167 | |||
| 168 | =head1 METHODS | ||
| 169 | |||
| 170 | =head2 session_initialize | ||
| 171 | |||
| 172 | This is where the session should do whatever initialization it needs. This | ||
| 173 | initialization may include defining additional events. | ||
| 174 | |||
| 175 | =head2 session_shutdown | ||
| 176 | |||
| 177 | This method should perform cleanup actions for the session. This is triggered | ||
| 178 | by a "shutdown" event. | ||
| 179 | |||
| 180 | =head2 session_reload | ||
| 181 | |||
| 182 | This method should perform reload actions for the session. By default it | ||
| 183 | calls $kernel->sig_handled() which terminates further handling of the HUP | ||
| 184 | signal. | ||
| 185 | |||
| 186 | =head2 session_stop | ||
| 187 | |||
| 188 | This method should perform stop actions for the session. This is triggered | ||
| 189 | by a "_stop" event. | ||
| 190 | |||
| 191 | =head1 PUBLIC EVENTS | ||
| 192 | |||
| 193 | The following public events are defined for the session. | ||
| 194 | |||
| 195 | =head2 session_startup | ||
| 196 | |||
| 197 | This event should start whatever processing the session will do. It is passed | ||
| 198 | two parameters: | ||
| 199 | |||
| 200 | =head2 session_shutdown | ||
| 201 | |||
| 202 | When you send this event to the session, it will invoke the session_cleanup() | ||
| 203 | method. | ||
| 204 | |||
| 205 | =head1 PRIVATE EVENTS | ||
| 206 | |||
| 207 | The following events are used internally: | ||
| 208 | |||
| 209 | session_init | ||
| 210 | session_interrupt | ||
| 211 | session_reload | ||
| 212 | shutdown | ||
| 213 | |||
| 214 | They should only be used with caution. | ||
| 215 | |||
| 216 | =head1 SEE ALSO | ||
| 217 | |||
| 218 | =over 4 | ||
| 219 | |||
| 220 | =item L<XAS|XAS> | ||
| 221 | |||
| 222 | =back | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 227 | |||
| 228 | =head1 COPYRIGHT AND LICENSE | ||
| 229 | |||
| 230 | Copyright (C) 2014 Kevin L. Esteb | ||
| 231 | |||
| 232 | This library is free software; you can redistribute it and/or modify | ||
| 233 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 234 | at your option, any later version of Perl 5 you may have available. | ||
| 235 | |||
| 236 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 237 | |||
| 238 | =cut |