| 1 | package XAS::Lib::Net::Server; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Socket ':all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use POE::Filter::Line; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use POE::Wheel::ReadWrite; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use POE::Wheel::SocketFactory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | use XAS::Class | ||||||||||
| 13 | 1 | debug => 0, | |||||||||
| 14 | version => $VERSION, | ||||||||||
| 15 | base => 'XAS::Lib::POE::Service', | ||||||||||
| 16 | mixin => 'XAS::Lib::Mixins::Handlers', | ||||||||||
| 17 | utils => 'weaken params', | ||||||||||
| 18 | accessors => 'session', | ||||||||||
| 19 | constants => 'ARRAY', | ||||||||||
| 20 | vars => { | ||||||||||
| 21 | PARAMS => { | ||||||||||
| 22 | -port => 1, | ||||||||||
| 23 | -tcp_keepalive => { optional => 1, default => 0 }, | ||||||||||
| 24 | -inactivity_timer => { optional => 1, default => 600 }, | ||||||||||
| 25 | -filter => { optional => 1, default => undef }, | ||||||||||
| 26 | -address => { optional => 1, default => 'localhost' }, | ||||||||||
| 27 | -eol => { optional => 1, default => "\015\012" }, | ||||||||||
| 28 | } | ||||||||||
| 29 | } | ||||||||||
| 30 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | #use Data::Dumper; | ||||||||||
| 33 | |||||||||||
| 34 | # ---------------------------------------------------------------------- | ||||||||||
| 35 | # Public Methods | ||||||||||
| 36 | # ---------------------------------------------------------------------- | ||||||||||
| 37 | |||||||||||
| 38 | sub session_initialize { | ||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_initialize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | my $alias = $self->alias; | |||||||||
| 42 | |||||||||||
| 43 | 0 | $self->log->debug("$alias: entering session_intialize()"); | |||||||||
| 44 | |||||||||||
| 45 | # private events | ||||||||||
| 46 | |||||||||||
| 47 | 0 | $poe_kernel->state('client_error', $self, '_client_error'); | |||||||||
| 48 | 0 | $poe_kernel->state('client_input', $self, '_client_input'); | |||||||||
| 49 | 0 | $poe_kernel->state('client_reaper', $self, '_client_reaper'); | |||||||||
| 50 | 0 | $poe_kernel->state('client_output', $self, '_client_output'); | |||||||||
| 51 | 0 | $poe_kernel->state('client_connected', $self, '_client_connected'); | |||||||||
| 52 | 0 | $poe_kernel->state('client_connection', $self, '_client_connection'); | |||||||||
| 53 | 0 | $poe_kernel->state('client_connection_failed', $self, '_client_connection_failed'); | |||||||||
| 54 | |||||||||||
| 55 | 0 | $poe_kernel->state('process_errors', $self, '_process_errors'); | |||||||||
| 56 | 0 | $poe_kernel->state('process_request', $self, '_process_request'); | |||||||||
| 57 | 0 | $poe_kernel->state('process_response', $self, '_process_response'); | |||||||||
| 58 | 0 | $poe_kernel->state('handle_connection', $self, '_handle_connection'); | |||||||||
| 59 | |||||||||||
| 60 | # walk the chain | ||||||||||
| 61 | |||||||||||
| 62 | 0 | $self->SUPER::session_initialize(); | |||||||||
| 63 | |||||||||||
| 64 | 0 | $self->log->debug("$alias: leaving session_intialize()"); | |||||||||
| 65 | |||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | sub session_startup { | ||||||||||
| 69 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_startup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my $alias = $self->alias; | |||||||||
| 72 | |||||||||||
| 73 | 0 | $self->log->debug("$alias: entering session_startup()"); | |||||||||
| 74 | |||||||||||
| 75 | 0 | $poe_kernel->call($alias, 'client_connection'); | |||||||||
| 76 | |||||||||||
| 77 | # walk the chain | ||||||||||
| 78 | |||||||||||
| 79 | 0 | $self->SUPER::session_startup(); | |||||||||
| 80 | |||||||||||
| 81 | 0 | $self->log->debug("$alias: leaving session_startup()"); | |||||||||
| 82 | |||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | sub session_shutdown { | ||||||||||
| 86 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_shutdown: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | my $alias = $self->alias; | |||||||||
| 89 | 0 | my $clients = $self->{clients}; | |||||||||
| 90 | |||||||||||
| 91 | 0 | $self->log->debug("$alias: entering session_shutdown()"); | |||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | while (my $client = keys %$clients) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | |||||||||||
| 95 | 0 | $poe_kernel->alarm_remove($client->{watchdog}); | |||||||||
| 96 | 0 | $client = undef; | |||||||||
| 97 | |||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | delete $self->{listener}; | |||||||||
| 101 | |||||||||||
| 102 | # walk the chain | ||||||||||
| 103 | |||||||||||
| 104 | 0 | $self->SUPER::session_shutdown(); | |||||||||
| 105 | |||||||||||
| 106 | 0 | $self->log->debug("$alias: leaving session_shutdown()"); | |||||||||
| 107 | |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub session_pause { | ||||||||||
| 111 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_pause: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 112 | |||||||||||
| 113 | 0 | my $alias = $self->alias; | |||||||||
| 114 | 0 | my $clients = $self->{clients}; | |||||||||
| 115 | |||||||||||
| 116 | 0 | $self->log->debug("$alias: entering session_pause()"); | |||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | while (my $wheel = keys %$clients) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 0 | $wheel->pause_input(); | |||||||||
| 121 | 0 | $poe_kernel->alarm_remove($wheel->{watchdog}); | |||||||||
| 122 | |||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # walk the chain | ||||||||||
| 126 | |||||||||||
| 127 | 0 | $self->SUPER::session_pause(); | |||||||||
| 128 | |||||||||||
| 129 | 0 | $self->log->debug("$alias: leaving session_pause()"); | |||||||||
| 130 | |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | sub session_resume { | ||||||||||
| 134 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_resume: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 135 | |||||||||||
| 136 | 0 | my $alias = $self->alias; | |||||||||
| 137 | 0 | my $clients = $self->{clients}; | |||||||||
| 138 | 0 | my $inactivity = $self->inactivity_timer; | |||||||||
| 139 | |||||||||||
| 140 | 0 | $self->log->debug("$alias: entering session_resume()"); | |||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | while (my $wheel = keys %$clients) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | |||||||||||
| 144 | 0 | $wheel->resume_input(); | |||||||||
| 145 | 0 | $poe_kernel->alarm_set('client_reaper', $inactivity, $wheel); | |||||||||
| 146 | |||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | # walk the chain | ||||||||||
| 150 | |||||||||||
| 151 | 0 | $self->SUPER::session_resume(); | |||||||||
| 152 | |||||||||||
| 153 | 0 | $self->log->debug("$alias: leaving session_resume()"); | |||||||||
| 154 | |||||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | sub reaper { | ||||||||||
| 158 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
reaper: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 159 | 0 | my ($wheel) = $self->validate_params(\@_, [1]); | |||||||||
| 160 | |||||||||||
| 161 | 0 | my $alias = $self->alias; | |||||||||
| 162 | |||||||||||
| 163 | 0 | $self->log->debug_msg('net_client_reaper', $alias, $self->host($wheel), $self->peerport($wheel)); | |||||||||
| 164 | |||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | sub process_request { | ||||||||||
| 168 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
process_request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 169 | 0 | my ($input, $ctx) = $self->validate_params(\@_, [1,1]); | |||||||||
| 170 | |||||||||||
| 171 | 0 | return $input; | |||||||||
| 172 | |||||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | sub process_response { | ||||||||||
| 176 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
process_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 177 | 0 | my ($output, $ctx) = $self->validate_params(\@_, [1,1]); | |||||||||
| 178 | |||||||||||
| 179 | 0 | return $output; | |||||||||
| 180 | |||||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | sub process_errors { | ||||||||||
| 184 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
process_errors: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 185 | 0 | my ($output, $ctx) = $self->validate_params(\@_, [1,1]); | |||||||||
| 186 | |||||||||||
| 187 | 0 | return $output; | |||||||||
| 188 | |||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | sub handle_connection { | ||||||||||
| 192 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
handle_connection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 193 | 0 | my ($wheel) = $self->validate_params(\@_, [1]); | |||||||||
| 194 | |||||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | # ---------------------------------------------------------------------- | ||||||||||
| 198 | # Public Accessors | ||||||||||
| 199 | # ---------------------------------------------------------------------- | ||||||||||
| 200 | |||||||||||
| 201 | sub peerport { | ||||||||||
| 202 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
peerport: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 203 | 0 | my ($wheel) = $self->validate_params(\@_, [1]); | |||||||||
| 204 | |||||||||||
| 205 | 0 | return $self->{clients}->{$wheel}->{port}; | |||||||||
| 206 | |||||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | sub peerhost { | ||||||||||
| 210 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
peerhost: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 211 | 0 | my ($wheel) = $self->validate_params(\@_, [1]); | |||||||||
| 212 | |||||||||||
| 213 | 0 | return $self->{clients}->{$wheel}->{host}; | |||||||||
| 214 | |||||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | sub client { | ||||||||||
| 218 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
client: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 219 | 0 | my ($wheel) = $self->validate_params(\@_, [1]); | |||||||||
| 220 | |||||||||||
| 221 | 0 | return $self->{clients}->{$wheel}->{client}; | |||||||||
| 222 | |||||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | # ---------------------------------------------------------------------- | ||||||||||
| 226 | # Public Events | ||||||||||
| 227 | # ---------------------------------------------------------------------- | ||||||||||
| 228 | |||||||||||
| 229 | # ---------------------------------------------------------------------- | ||||||||||
| 230 | # Private Events | ||||||||||
| 231 | # ---------------------------------------------------------------------- | ||||||||||
| 232 | |||||||||||
| 233 | sub _process_request { | ||||||||||
| 234 | 0 | ▶ | my ($self, $input, $ctx) = @_[OBJECT,ARG0,ARG1]; | ||||||||
|
Subroutine
_process_request: not called CC=1 SCAR=6.9
| |||||||||||
| 235 | |||||||||||
| 236 | 0 | my $alias = $self->alias; | |||||||||
| 237 | 0 | my $data = $self->process_request($input, $ctx); | |||||||||
| 238 | |||||||||||
| 239 | 0 | $self->log->debug("$alias: _process_request()"); | |||||||||
| 240 | 0 | $poe_kernel->post($alias, 'process_response', $data, $ctx); | |||||||||
| 241 | |||||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | sub _process_response { | ||||||||||
| 245 | 0 | ▶ | my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1]; | ||||||||
|
Subroutine
_process_response: not called CC=1 SCAR=6.9
| |||||||||||
| 246 | |||||||||||
| 247 | 0 | my $alias = $self->alias; | |||||||||
| 248 | 0 | my $data = $self->process_response($output, $ctx); | |||||||||
| 249 | |||||||||||
| 250 | 0 | $self->log->debug("$alias: _process_response()"); | |||||||||
| 251 | 0 | $poe_kernel->post($alias, 'client_output', $data, $ctx); | |||||||||
| 252 | |||||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | sub _process_errors { | ||||||||||
| 256 | 0 | ▶ | my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1]; | ||||||||
|
Subroutine
_process_errors: not called CC=1 SCAR=6.9
| |||||||||||
| 257 | |||||||||||
| 258 | 0 | my $alias = $self->alias; | |||||||||
| 259 | 0 | my $data = $self->process_errors($output, $ctx); | |||||||||
| 260 | |||||||||||
| 261 | 0 | $self->log->debug("$alias: _process_errors()"); | |||||||||
| 262 | 0 | $poe_kernel->post($alias, 'client_output', $data, $ctx); | |||||||||
| 263 | |||||||||||
| 264 | } | ||||||||||
| 265 | |||||||||||
| 266 | sub _handle_connection { | ||||||||||
| 267 | 0 | ▶ | my ($self, $wheel) = @_[OBJECT, ARG0]; | ||||||||
|
Subroutine
_handle_connection: not called CC=1 SCAR=6.9
| |||||||||||
| 268 | |||||||||||
| 269 | 0 | my $alias = $self->alias; | |||||||||
| 270 | |||||||||||
| 271 | 0 | $self->log->debug("$alias: _handle_connection()"); | |||||||||
| 272 | 0 | $self->handle_connection($wheel); | |||||||||
| 273 | |||||||||||
| 274 | } | ||||||||||
| 275 | |||||||||||
| 276 | sub _client_connection { | ||||||||||
| 277 | 0 | ▶ | my ($self) = $_[OBJECT]; | ||||||||
|
Subroutine
_client_connection: not called CC=1 SCAR=6.9
| |||||||||||
| 278 | |||||||||||
| 279 | 0 | my $alias = $self->alias; | |||||||||
| 280 | |||||||||||
| 281 | 0 | $self->log->debug("$alias: _client_connection()"); | |||||||||
| 282 | |||||||||||
| 283 | # start listening for connections | ||||||||||
| 284 | |||||||||||
| 285 | 0 | $self->{listener} = POE::Wheel::SocketFactory->new( | |||||||||
| 286 | BindAddress => $self->address, | ||||||||||
| 287 | BindPort => $self->port, | ||||||||||
| 288 | SocketType => SOCK_STREAM, | ||||||||||
| 289 | SocketDomain => AF_INET, | ||||||||||
| 290 | SocketProtocol => 'tcp', | ||||||||||
| 291 | Reuse => 1, | ||||||||||
| 292 | SuccessEvent => 'client_connected', | ||||||||||
| 293 | FailureEvent => 'client_connection_failed' | ||||||||||
| 294 | ); | ||||||||||
| 295 | |||||||||||
| 296 | } | ||||||||||
| 297 | |||||||||||
| 298 | sub _client_connected { | ||||||||||
| 299 | 0 | ▶ | my ($self, $socket, $peeraddr, $peerport, $wheel_id) = @_[OBJECT,ARG0..ARG3]; | ||||||||
|
Subroutine
_client_connected: not called CC=1 SCAR=6.9
| |||||||||||
| 300 | |||||||||||
| 301 | 0 | my $alias = $self->alias; | |||||||||
| 302 | 0 | my $inactivity = $self->inactivity_timer; | |||||||||
| 303 | |||||||||||
| 304 | 0 | $self->log->debug("$alias: _client_connected()"); | |||||||||
| 305 | |||||||||||
| 306 | 0 | my $client = POE::Wheel::ReadWrite->new( | |||||||||
| 307 | Handle => $socket, | ||||||||||
| 308 | Filter => $self->filter, | ||||||||||
| 309 | InputEvent => 'client_input', | ||||||||||
| 310 | ErrorEvent => 'client_error' | ||||||||||
| 311 | ); | ||||||||||
| 312 | |||||||||||
| 313 | 0 | my $wheel = $client->ID; | |||||||||
| 314 | 0 | my $host = gethostbyaddr($peeraddr, AF_INET); | |||||||||
| 315 | |||||||||||
| 316 | 0 | $self->{clients}->{$wheel}->{host} = $host; | |||||||||
| 317 | 0 | $self->{clients}->{$wheel}->{port} = $peerport; | |||||||||
| 318 | 0 | $self->{clients}->{$wheel}->{client} = $client; | |||||||||
| 319 | 0 | $self->{clients}->{$wheel}->{active} = time(); | |||||||||
| 320 | 0 | $self->{clients}->{$wheel}->{socket} = $socket; | |||||||||
| 321 | 0 | $self->{clients}->{$wheel}->{watchdog} = $poe_kernel->alarm_set('client_reaper', $inactivity, $wheel); | |||||||||
| 322 | |||||||||||
| 323 | 0 | $self->log->info_msg('net_client_connect', $alias, $host, $peerport); | |||||||||
| 324 | |||||||||||
| 325 | 0 | $poe_kernel->post($alias, 'handle_connection', $wheel); | |||||||||
| 326 | |||||||||||
| 327 | } | ||||||||||
| 328 | |||||||||||
| 329 | sub _client_connection_failed { | ||||||||||
| 330 | 0 | ▶ | my ($self, $syscall, $errnum, $errstr, $wheel) = @_[OBJECT,ARG0..ARG3]; | ||||||||
|
Subroutine
_client_connection_failed: not called CC=1 SCAR=6.9
| |||||||||||
| 331 | |||||||||||
| 332 | 0 | my $alias = $self->alias; | |||||||||
| 333 | |||||||||||
| 334 | 0 | $self->log->error_msg('net_client_connection_failed', $alias, $errnum, $errstr); | |||||||||
| 335 | |||||||||||
| 336 | 0 | delete $self->{listener}; | |||||||||
| 337 | |||||||||||
| 338 | } | ||||||||||
| 339 | |||||||||||
| 340 | sub _client_input { | ||||||||||
| 341 | 0 | ▶ | my ($self, $input, $wheel) = @_[OBJECT,ARG0,ARG1]; | ||||||||
|
Subroutine
_client_input: not called CC=1 SCAR=6.9
| |||||||||||
| 342 | |||||||||||
| 343 | 0 | my $alias = $self->alias; | |||||||||
| 344 | 0 | my $ctx = { | |||||||||
| 345 | wheel => $wheel | ||||||||||
| 346 | }; | ||||||||||
| 347 | |||||||||||
| 348 | 0 | $self->log->debug("$alias: _client_input()"); | |||||||||
| 349 | |||||||||||
| 350 | 0 | $self->{clients}->{$wheel}->{active} = time(); | |||||||||
| 351 | |||||||||||
| 352 | 0 | $poe_kernel->post($alias, 'process_request', $input, $ctx); | |||||||||
| 353 | |||||||||||
| 354 | } | ||||||||||
| 355 | |||||||||||
| 356 | sub _client_output { | ||||||||||
| 357 | 0 | ▶ | my ($self, $data, $ctx) = @_[OBJECT,ARG0,ARG1]; | ||||||||
|
Subroutine
_client_output: not called CC=1 SCAR=6.9
| |||||||||||
| 358 | |||||||||||
| 359 | 0 | my $alias = $self->alias; | |||||||||
| 360 | 0 | my $wheel = $ctx->{wheel}; | |||||||||
| 361 | 0 | my @buffer; | |||||||||
| 362 | |||||||||||
| 363 | 0 | $self->log->debug("$alias: _client_output()"); | |||||||||
| 364 | |||||||||||
| 365 | try { | ||||||||||
| 366 | |||||||||||
| 367 | 0 | ▶ | if (defined($wheel)) { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||
| 368 | |||||||||||
| 369 | # emulate IO::Socket connected() method. this method | ||||||||||
| 370 | # calls getpeername(). getpeername() returns undef when | ||||||||||
| 371 | # the network stack can't validate the socket. | ||||||||||
| 372 | |||||||||||
| 373 | 0 | ▶ | if (getpeername($self->{clients}->{$wheel}->{socket})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 374 | |||||||||||
| 375 | 0 | push(@buffer, $data); | |||||||||
| 376 | 0 | $self->{clients}->{$wheel}->{client}->put(@buffer); | |||||||||
| 377 | |||||||||||
| 378 | } else { | ||||||||||
| 379 | |||||||||||
| 380 | 0 | $self->log->error_msg( | |||||||||
| 381 | 'net_client_nosocket', | ||||||||||
| 382 | $alias, | ||||||||||
| 383 | $self->peerhist($wheel), | ||||||||||
| 384 | $self->peerport($wheel) | ||||||||||
| 385 | ); | ||||||||||
| 386 | 0 | delete $self->{clients}->{$wheel}; | |||||||||
| 387 | |||||||||||
| 388 | } | ||||||||||
| 389 | |||||||||||
| 390 | } else { | ||||||||||
| 391 | |||||||||||
| 392 | 0 | $self->log->error_msg('net_client_nowheel', $alias); | |||||||||
| 393 | |||||||||||
| 394 | } | ||||||||||
| 395 | |||||||||||
| 396 | } catch { | ||||||||||
| 397 | |||||||||||
| 398 | 0 | ▶ | my $ex = $_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 399 | |||||||||||
| 400 | 0 | $self->exception_handler($ex); | |||||||||
| 401 | |||||||||||
| 402 | 0 | delete $self->{clients}->{$wheel}; | |||||||||
| 403 | |||||||||||
| 404 | 0 | }; | |||||||||
| 405 | |||||||||||
| 406 | } | ||||||||||
| 407 | |||||||||||
| 408 | sub _client_error { | ||||||||||
| 409 | 0 | ▶ | my ($self, $syscall, $errnum, $errstr, $wheel) = @_[OBJECT,ARG0..ARG3]; | ||||||||
|
Subroutine
_client_error: not called CC=2 SCAR=17.9
| |||||||||||
| 410 | |||||||||||
| 411 | 0 | my $alias = $self->alias; | |||||||||
| 412 | |||||||||||
| 413 | 0 | $self->log->debug("$alias: _client_error()"); | |||||||||
| 414 | |||||||||||
| 415 | 0 | ▶ | if ($errnum == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 416 | |||||||||||
| 417 | 0 | $self->log->info_msg('net_client_disconnect', $alias, $self->peerhost($wheel), $self->peerport($wheel)); | |||||||||
| 418 | |||||||||||
| 419 | } else { | ||||||||||
| 420 | |||||||||||
| 421 | 0 | $self->log->error_msg('net_client_error', $alias, $errnum, $errstr); | |||||||||
| 422 | |||||||||||
| 423 | } | ||||||||||
| 424 | |||||||||||
| 425 | 0 | delete $self->{clients}->{$wheel}; | |||||||||
| 426 | |||||||||||
| 427 | } | ||||||||||
| 428 | |||||||||||
| 429 | sub _client_reaper { | ||||||||||
| 430 | 0 | ▶ | my ($self, $wheel) = @_[OBJECT,ARG0]; | ||||||||
|
Subroutine
_client_reaper: not called CC=2 SCAR=17.9
| |||||||||||
| 431 | |||||||||||
| 432 | 0 | my $timeout = time() - $self->inactivity_timer; | |||||||||
| 433 | |||||||||||
| 434 | 0 | ▶ | if ($self->{clients}->{$wheel}->{active} < $timeout) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 435 | |||||||||||
| 436 | 0 | $self->reaper($wheel); | |||||||||
| 437 | |||||||||||
| 438 | } | ||||||||||
| 439 | |||||||||||
| 440 | } | ||||||||||
| 441 | |||||||||||
| 442 | # ---------------------------------------------------------------------- | ||||||||||
| 443 | # Private Methods | ||||||||||
| 444 | # ---------------------------------------------------------------------- | ||||||||||
| 445 | |||||||||||
| 446 | sub init { | ||||||||||
| 447 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 448 | |||||||||||
| 449 | 0 | my $self = $class->SUPER::init(@_); | |||||||||
| 450 | |||||||||||
| 451 | 0 | ▶ | unless (defined($self->filter)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 452 | |||||||||||
| 453 | 0 | $self->{filter} = POE::Filter::Line->new( | |||||||||
| 454 | InputLiteral => $self->eol, | ||||||||||
| 455 | OutputLiteral => $self->eol, | ||||||||||
| 456 | ); | ||||||||||
| 457 | |||||||||||
| 458 | } | ||||||||||
| 459 | |||||||||||
| 460 | 0 | return $self; | |||||||||
| 461 | |||||||||||
| 462 | } | ||||||||||
| 463 | |||||||||||
| 464 | 1 | 1; | |||||||||
| 465 | |||||||||||
| 466 | __END__ | ||||||||||
| 467 | |||||||||||
| 468 | =head1 NAME | ||||||||||
| 469 | |||||||||||
| 470 | XAS::Lib::Net::Server - A basic network server for the XAS Environment | ||||||||||
| 471 | |||||||||||
| 472 | =head1 SYNOPSIS | ||||||||||
| 473 | |||||||||||
| 474 | my $server = XAS::Lib::Net::Server->new( | ||||||||||
| 475 | -port => 9505, | ||||||||||
| 476 | -host => 'localhost', | ||||||||||
| 477 | -filter => POE::Filter::Line->new(), | ||||||||||
| 478 | -alias => 'server', | ||||||||||
| 479 | -inactivity_timer => 600, | ||||||||||
| 480 | -eol => "\015\012" | ||||||||||
| 481 | } | ||||||||||
| 482 | |||||||||||
| 483 | =head1 DESCRIPTION | ||||||||||
| 484 | |||||||||||
| 485 | This module implements a simple text orientated network protocol. Data is | ||||||||||
| 486 | sent out as delimited strings. Which means every string has a consistent EOL. | ||||||||||
| 487 | These strings may be formatted, such as JSON. This module inherits from | ||||||||||
| 488 | L<XAS::Lib::POE::Session|XAS::Lib::POE::Session>. | ||||||||||
| 489 | |||||||||||
| 490 | =head1 METHODS | ||||||||||
| 491 | |||||||||||
| 492 | =head2 new | ||||||||||
| 493 | |||||||||||
| 494 | This initializes the module and starts listening for requests. There are | ||||||||||
| 495 | five parameters that can be passed. They are the following: | ||||||||||
| 496 | |||||||||||
| 497 | =over 4 | ||||||||||
| 498 | |||||||||||
| 499 | =item B<-alias> | ||||||||||
| 500 | |||||||||||
| 501 | The name of the POE session. | ||||||||||
| 502 | |||||||||||
| 503 | =item B<-port> | ||||||||||
| 504 | |||||||||||
| 505 | The IP port to listen on. | ||||||||||
| 506 | |||||||||||
| 507 | =item B<-address> | ||||||||||
| 508 | |||||||||||
| 509 | The address to bind too. | ||||||||||
| 510 | |||||||||||
| 511 | =item B<-inactivity_timer> | ||||||||||
| 512 | |||||||||||
| 513 | Sets an inactivity timer on clients. When it is surpassed, the method reaper() | ||||||||||
| 514 | is called with the POE wheel id. What reaper() does is application specific. | ||||||||||
| 515 | The default is 600 seconds. | ||||||||||
| 516 | |||||||||||
| 517 | =item B<-filter> | ||||||||||
| 518 | |||||||||||
| 519 | An optional filter to use, defaults to POE::Filter::Line | ||||||||||
| 520 | |||||||||||
| 521 | =item B<-eol> | ||||||||||
| 522 | |||||||||||
| 523 | An optional EOL, defaults to "\015\012"; | ||||||||||
| 524 | |||||||||||
| 525 | =back | ||||||||||
| 526 | |||||||||||
| 527 | =head2 reaper($wheel) | ||||||||||
| 528 | |||||||||||
| 529 | Called when the inactivity timer is triggered. | ||||||||||
| 530 | |||||||||||
| 531 | =over 4 | ||||||||||
| 532 | |||||||||||
| 533 | =item B<$wheel> | ||||||||||
| 534 | |||||||||||
| 535 | The POE wheel that triggered the timer. | ||||||||||
| 536 | |||||||||||
| 537 | =back | ||||||||||
| 538 | |||||||||||
| 539 | =head2 process_request($input, $ctx) | ||||||||||
| 540 | |||||||||||
| 541 | This method will process the input from the client. It takes the | ||||||||||
| 542 | following parameters: | ||||||||||
| 543 | |||||||||||
| 544 | =over 4 | ||||||||||
| 545 | |||||||||||
| 546 | =item B<$input> | ||||||||||
| 547 | |||||||||||
| 548 | The input received from the socket. | ||||||||||
| 549 | |||||||||||
| 550 | =item B<$ctx> | ||||||||||
| 551 | |||||||||||
| 552 | A hash variable to maintain context. This will be initialized with a "wheel" | ||||||||||
| 553 | field. Others fields may be added as needed. | ||||||||||
| 554 | |||||||||||
| 555 | =back | ||||||||||
| 556 | |||||||||||
| 557 | =head2 process_response($output, $ctx) | ||||||||||
| 558 | |||||||||||
| 559 | This method will process the output from the client. It takes the | ||||||||||
| 560 | following parameters: | ||||||||||
| 561 | |||||||||||
| 562 | =over 4 | ||||||||||
| 563 | |||||||||||
| 564 | =item B<$output> | ||||||||||
| 565 | |||||||||||
| 566 | The output to be sent to the socket. | ||||||||||
| 567 | |||||||||||
| 568 | =item B<$ctx> | ||||||||||
| 569 | |||||||||||
| 570 | A hash variable to maintain context. This uses the "wheel" field to direct | ||||||||||
| 571 | output to the correct socket. Others fields may have been added as needed. | ||||||||||
| 572 | |||||||||||
| 573 | =back | ||||||||||
| 574 | |||||||||||
| 575 | =head2 process_errors($output, $ctx) | ||||||||||
| 576 | |||||||||||
| 577 | This method will process the error output from the client. It takes the | ||||||||||
| 578 | following parameters: | ||||||||||
| 579 | |||||||||||
| 580 | =over 4 | ||||||||||
| 581 | |||||||||||
| 582 | =item B<$output> | ||||||||||
| 583 | |||||||||||
| 584 | The output to be sent to the socket. | ||||||||||
| 585 | |||||||||||
| 586 | =item B<$ctx> | ||||||||||
| 587 | |||||||||||
| 588 | A hash variable to maintain context. This uses the "wheel" field to direct | ||||||||||
| 589 | output to the correct socket. Others fields may have been added as needed. | ||||||||||
| 590 | |||||||||||
| 591 | =back | ||||||||||
| 592 | |||||||||||
| 593 | =head2 handle_connection($wheel) | ||||||||||
| 594 | |||||||||||
| 595 | This method is called after the client connects. This is for additional | ||||||||||
| 596 | post connection processing as needed. It takes the following parameters: | ||||||||||
| 597 | |||||||||||
| 598 | =over 4 | ||||||||||
| 599 | |||||||||||
| 600 | =item B<$wheel> | ||||||||||
| 601 | |||||||||||
| 602 | The id of the clients wheel. | ||||||||||
| 603 | |||||||||||
| 604 | =back | ||||||||||
| 605 | |||||||||||
| 606 | =head1 ACCESSORS | ||||||||||
| 607 | |||||||||||
| 608 | =head2 peerport($wheel) | ||||||||||
| 609 | |||||||||||
| 610 | This returns the current port for that wheel. | ||||||||||
| 611 | |||||||||||
| 612 | =over 4 | ||||||||||
| 613 | |||||||||||
| 614 | =item B<$wheel> | ||||||||||
| 615 | |||||||||||
| 616 | The POE wheel to use. | ||||||||||
| 617 | |||||||||||
| 618 | =back | ||||||||||
| 619 | |||||||||||
| 620 | =head2 host($wheel) | ||||||||||
| 621 | |||||||||||
| 622 | This returns the current host name for that wheel. | ||||||||||
| 623 | |||||||||||
| 624 | =over 4 | ||||||||||
| 625 | |||||||||||
| 626 | =item B<$wheel> | ||||||||||
| 627 | |||||||||||
| 628 | The POE wheel to use. | ||||||||||
| 629 | |||||||||||
| 630 | =back | ||||||||||
| 631 | |||||||||||
| 632 | =head2 client($wheel) | ||||||||||
| 633 | |||||||||||
| 634 | This returns the current client for that wheel. | ||||||||||
| 635 | |||||||||||
| 636 | =over 4 | ||||||||||
| 637 | |||||||||||
| 638 | =item B<$wheel> | ||||||||||
| 639 | |||||||||||
| 640 | The POE wheel to use. | ||||||||||
| 641 | |||||||||||
| 642 | =back | ||||||||||
| 643 | |||||||||||
| 644 | =head1 SEE ALSO | ||||||||||
| 645 | |||||||||||
| 646 | =over 4 | ||||||||||
| 647 | |||||||||||
| 648 | =item L<POE::Filter::Line|https://metacpan.org/pod/POE::Filter::Line> | ||||||||||
| 649 | |||||||||||
| 650 | =item L<XAS|XAS> | ||||||||||
| 651 | |||||||||||
| 652 | =back | ||||||||||
| 653 | |||||||||||
| 654 | =head1 AUTHOR | ||||||||||
| 655 | |||||||||||
| 656 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 657 | |||||||||||
| 658 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 659 | |||||||||||
| 660 | Copyright (c) 2014 Kevin L. Esteb | ||||||||||
| 661 | |||||||||||
| 662 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 663 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 664 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 665 | |||||||||||
| 666 | =cut | ||||||||||