blib/lib/XAS/Lib/Net/Server.pm untested

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