File Coverage

blib/lib/POE/Component/Server/Chargen.pm
Criterion Covered Total %
statement 61 62 98.3
branch 9 18 50.0
condition 4 12 33.3
subroutine 14 15 93.3
pod 1 1 100.0
total 89 108 82.4


line stmt bran cond sub pod time code
1             # $Id: Chargen.pm,v 1.3 2005/01/27 13:48:24 chris Exp $
2             #
3             # POE::Component::Server::Echo, by Chris 'BinGOs' Williams
4             #
5             # This module may be used, modified, and distributed under the same
6             # terms as Perl itself. Please see the license that came with your Perl
7             # distribution for details.
8             #
9              
10             package POE::Component::Server::Chargen;
11              
12 3     3   118699 use strict;
  3         7  
  3         117  
13 3     3   13 use warnings;
  3         5  
  3         86  
14 3     3   15 use Carp;
  3         10  
  3         288  
15 3     3   3547 use POE;
  3         304715  
  3         22  
16 3     3   396201 use Socket;
  3         7  
  3         2186  
17 3     3   20 use base qw(POE::Component::Server::Echo);
  3         5  
  3         3345  
18 3     3   80663 use vars qw($VERSION);
  3         9  
  3         119  
19              
20 3     3   16 use constant DATAGRAM_MAXLEN => 1024;
  3         5  
  3         138  
21 3     3   17 use constant DEFAULT_PORT => 19;
  3         6  
  3         2330  
22              
23             $VERSION = '1.14';
24              
25             sub spawn {
26 2     2 1 73 my $package = shift;
27 2 50       10 croak "$package requires an even number of parameters" if @_ & 1;
28              
29 2         14 my %parms = @_;
30              
31 2 50 33     27 $parms{'Alias'} = 'Chargen-Server' unless defined $parms{'Alias'} and $parms{'Alias'};
32 2 50 33     16 $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0;
33 2 50 33     14 $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0;
34              
35 2         8 my $self = bless { }, $package;
36              
37 2         25 $self->{CONFIG} = \%parms;
38 2         6 $self->{start_ascii} = 32;
39              
40 2 50       44 POE::Session->create(
41             object_states => [
42             $self => { _start => '_server_start',
43             _stop => '_server_stop',
44             shutdown => '_server_close' },
45             $self => [ qw(_accept_new_client _accept_failed _client_input _client_error _client_flushed _get_datagram) ],
46             ],
47             ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
48             );
49              
50 2         5253 return $self;
51             }
52              
53             sub _accept_new_client {
54 1     1   2464 my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
55 1         9 $peeraddr = inet_ntoa($peeraddr);
56              
57 1         12 my $wheel = POE::Wheel::ReadWrite->new (
58             Handle => $socket,
59             Filter => POE::Filter::Line->new(),
60             InputEvent => '_client_input',
61             ErrorEvent => '_client_error',
62             FlushedEvent => '_client_flushed',
63             );
64              
65 1         403 $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport, start_ascii => $self->{start_ascii} };
66 1         9 $wheel->put( _generate_line(\$self->{Clients}->{ $wheel->ID() }->{start_ascii}) );
67 1         163 undef;
68             }
69              
70             sub _client_input {
71 0     0   0 undef;
72             }
73              
74             sub _client_flushed {
75 2     2   3666 my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
76              
77 2 50 33     21 if ( defined $self->{Clients}->{ $wheel_id } and defined $self->{Clients}->{ $wheel_id }->{Wheel} ) {
78 2         11 $self->{Clients}->{ $wheel_id }->{Wheel}->put( _generate_line(\$self->{Clients}->{ $wheel_id }->{start_ascii}) );
79             }
80 2         143 undef;
81             }
82              
83             sub _get_datagram {
84 1     1   1825 my ( $kernel, $self, $socket ) = @_[ KERNEL, OBJECT, ARG0 ];
85              
86 1         22 my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
87 1 50       6 return unless defined $remote_address;
88              
89 1         4 my $start_ascii = $self->{start_ascii};
90              
91 1         6 my $reply = _generate_line( \$start_ascii );
92              
93 1 50       24 send( $socket, $reply, 0, $remote_address ) == length($reply)
94             or warn "Trouble sending response: $!";
95              
96 1         6 undef;
97             }
98              
99             sub _generate_line {
100 4     4   13 my $start_ascii = shift;
101 288         620 my $chargen_line = join(
102             '',
103 4         30 map { chr }
104             ($$start_ascii .. $$start_ascii + 71)
105             );
106 4         39 $chargen_line =~ tr[\x7F-\xDD][\x20-\x7E];
107 4 50       19 $$start_ascii = 32 if ++$$start_ascii > 126;
108 4         25 return $chargen_line;
109             }
110              
111             1;
112             __END__