File Coverage

blib/lib/POE/Component/Server/Chargen.pm
Criterion Covered Total %
statement 58 59 98.3
branch 9 18 50.0
condition 4 12 33.3
subroutine 13 14 92.8
pod 1 1 100.0
total 85 104 81.7


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             $POE::Component::Server::Chargen::VERSION = '1.16';
12             #ABSTRACT: A POE component that implements an RFC 864 Chargen server.
13              
14 2     2   30917 use strict;
  2         3  
  2         49  
15 2     2   5 use warnings;
  2         3  
  2         43  
16 2     2   7 use Carp;
  2         2  
  2         132  
17 2     2   1073 use POE;
  2         75941  
  2         12  
18 2     2   95713 use Socket;
  2         3  
  2         878  
19 2     2   9 use base qw(POE::Component::Server::Echo);
  2         2  
  2         1167  
20              
21 2     2   33518 use constant DATAGRAM_MAXLEN => 1024;
  2         4  
  2         111  
22 2     2   11 use constant DEFAULT_PORT => 19;
  2         3  
  2         1011  
23              
24             sub spawn {
25 2     2 1 43 my $package = shift;
26 2 50       9 croak "$package requires an even number of parameters" if @_ & 1;
27              
28 2         12 my %parms = @_;
29              
30 2 50 33     15 $parms{'Alias'} = 'Chargen-Server' unless defined $parms{'Alias'} and $parms{'Alias'};
31 2 50 33     9 $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0;
32 2 50 33     10 $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0;
33              
34 2         4 my $self = bless { }, $package;
35              
36 2         15 $self->{CONFIG} = \%parms;
37 2         4 $self->{start_ascii} = 32;
38              
39             POE::Session->create(
40             object_states => [
41             $self => { _start => '_server_start',
42             _stop => '_server_stop',
43             shutdown => '_server_close' },
44             $self => [ qw(_accept_new_client _accept_failed _client_input _client_error _client_flushed _get_datagram) ],
45             ],
46 2 50       34 ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
47             );
48              
49 2         2920 return $self;
50             }
51              
52             sub _accept_new_client {
53 1     1   1972 my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
54 1         19 $peeraddr = inet_ntoa($peeraddr);
55              
56 1         10 my $wheel = POE::Wheel::ReadWrite->new (
57             Handle => $socket,
58             Filter => POE::Filter::Line->new(),
59             InputEvent => '_client_input',
60             ErrorEvent => '_client_error',
61             FlushedEvent => '_client_flushed',
62             );
63              
64 1         381 $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport, start_ascii => $self->{start_ascii} };
65 1         7 $wheel->put( _generate_line(\$self->{Clients}->{ $wheel->ID() }->{start_ascii}) );
66 1         98 undef;
67             }
68              
69             sub _client_input {
70 0     0   0 undef;
71             }
72              
73             sub _client_flushed {
74 2     2   1776 my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
75              
76 2 50 33     13 if ( defined $self->{Clients}->{ $wheel_id } and defined $self->{Clients}->{ $wheel_id }->{Wheel} ) {
77 2         12 $self->{Clients}->{ $wheel_id }->{Wheel}->put( _generate_line(\$self->{Clients}->{ $wheel_id }->{start_ascii}) );
78             }
79 2         81 undef;
80             }
81              
82             sub _get_datagram {
83 1     1   1045 my ( $kernel, $self, $socket ) = @_[ KERNEL, OBJECT, ARG0 ];
84              
85 1         12 my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
86 1 50       5 return unless defined $remote_address;
87              
88 1         2 my $start_ascii = $self->{start_ascii};
89              
90 1         2 my $reply = _generate_line( \$start_ascii );
91              
92 1 50       20 send( $socket, $reply, 0, $remote_address ) == length($reply)
93             or warn "Trouble sending response: $!";
94              
95 1         3 undef;
96             }
97              
98             sub _generate_line {
99 4     4   10 my $start_ascii = shift;
100             my $chargen_line = join(
101             '',
102 4         18 map { chr }
  288         278  
103             ($$start_ascii .. $$start_ascii + 71)
104             );
105 4         21 $chargen_line =~ tr[\x7F-\xDD][\x20-\x7E];
106 4 50       12 $$start_ascii = 32 if ++$$start_ascii > 126;
107 4         18 return $chargen_line;
108             }
109              
110             qq[A Gen of chars];
111              
112             __END__