File Coverage

blib/lib/POE/Component/Server/Qotd.pm
Criterion Covered Total %
statement 50 51 98.0
branch 8 16 50.0
condition 4 12 33.3
subroutine 12 13 92.3
pod 1 1 100.0
total 75 93 80.6


line stmt bran cond sub pod time code
1             # $Id: Qotd.pm,v 1.1.1.1 2005/01/27 14:15:42 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::Qotd;
11             $POE::Component::Server::Qotd::VERSION = '1.16';
12             #ABSTRACT: A POE component that implements an RFC 865 QotD server.
13              
14 2     2   28832 use strict;
  2         3  
  2         51  
15 2     2   6 use warnings;
  2         2  
  2         45  
16 2     2   6 use Carp;
  2         3  
  2         139  
17 2     2   913 use POE;
  2         66769  
  2         11  
18 2     2   97658 use Socket;
  2         3  
  2         791  
19 2     2   10 use base qw(POE::Component::Server::Echo);
  2         2  
  2         1012  
20              
21 2     2   32091 use constant DATAGRAM_MAXLEN => 1024;
  2         4  
  2         86  
22 2     2   8 use constant DEFAULT_PORT => 17;
  2         2  
  2         752  
23              
24             sub spawn {
25 2     2 1 26 my $package = shift;
26 2 50       7 croak "$package requires an even number of parameters" if @_ & 1;
27              
28 2         8 my %parms = @_;
29              
30 2 50 33     14 $parms{'Alias'} = 'Qotd-Server' unless defined $parms{'Alias'} and $parms{'Alias'};
31 2 50 33     8 $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0;
32 2 50 33     8 $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0;
33             $parms{'Quote'} = 'Parts that positively cannot be assembled in improper order will be.'
34 2 50 33     9 unless defined $parms{'Quote'} and length $parms{'Quote'} <= 512;
35              
36 2         4 my $self = bless { }, $package;
37              
38 2         14 $self->{CONFIG} = \%parms;
39              
40             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 2 50       27 ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
48             );
49              
50 2         2567 return $self;
51             }
52              
53             sub _accept_new_client {
54 1     1   1147 my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
55 1         7 $peeraddr = inet_ntoa($peeraddr);
56              
57 1         10 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         256 $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport };
66 1         6 $wheel->put( $self->{CONFIG}->{Quote} );
67 1         62 undef;
68             }
69              
70             sub _client_input {
71 0     0   0 undef;
72             }
73              
74             sub _client_flushed {
75 1     1   930 my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
76 1         5 delete $self->{Clients}->{ $wheel_id }->{Wheel};
77 1         130 delete $self->{Clients}->{ $wheel_id };
78 1         2 undef;
79             }
80              
81             sub _get_datagram {
82 1     1   1389 my ( $kernel, $self, $socket ) = @_[ KERNEL, OBJECT, ARG0 ];
83              
84 1         11 my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
85 1 50       4 return unless defined $remote_address;
86              
87             send( $socket, $self->{CONFIG}->{Quote}, 0, $remote_address ) == length( $self->{CONFIG}->{Quote} )
88 1 50       21 or warn "Trouble sending response: $!";
89 1         23 undef;
90             }
91              
92             qq[What is the frequency, Kenneth?];
93              
94             __END__