File Coverage

blib/lib/POE/Component/Server/Daytime.pm
Criterion Covered Total %
statement 52 53 98.1
branch 7 14 50.0
condition 3 9 33.3
subroutine 13 14 92.8
pod 1 1 100.0
total 76 91 83.5


line stmt bran cond sub pod time code
1             # $Id: Daytime.pm,v 1.1.1.1 2005/01/27 15:03:34 chris Exp $
2             #
3             # POE::Component::Server::Daytime, 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::Daytime;
11             $POE::Component::Server::Daytime::VERSION = '1.16';
12             #ABSTRACT: A POE component that implements an RFC 867 Daytime server.
13              
14 2     2   29442 use strict;
  2         4  
  2         61  
15 2     2   8 use warnings;
  2         3  
  2         61  
16 2     2   8 use Carp;
  2         3  
  2         173  
17 2     2   841 use POE;
  2         64634  
  2         11  
18 2     2   108035 use Socket;
  2         3  
  2         881  
19 2     2   10 use POSIX;
  2         2  
  2         10  
20 2     2   3035 use base qw(POE::Component::Server::Echo);
  2         3  
  2         923  
21              
22 2     2   29496 use constant DATAGRAM_MAXLEN => 1024;
  2         4  
  2         87  
23 2     2   6 use constant DEFAULT_PORT => 13;
  2         3  
  2         772  
24              
25             sub spawn {
26 2     2 1 27 my $package = shift;
27 2 50       7 croak "$package requires an even number of parameters" if @_ & 1;
28              
29 2         9 my %parms = @_;
30              
31 2 50 33     15 $parms{'Alias'} = 'Qotd-Server' unless defined $parms{'Alias'} and $parms{'Alias'};
32 2 50 33     10 $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0;
33 2 50 33     8 $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0;
34              
35 2         5 my $self = bless { }, $package;
36              
37 2         20 $self->{CONFIG} = \%parms;
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       48 ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
47             );
48              
49 2         2592 return $self;
50             }
51              
52             sub _accept_new_client {
53 1     1   1446 my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
54 1         5 $peeraddr = inet_ntoa($peeraddr);
55              
56 1         9 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         272 $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport };
65 1         97 $wheel->put( strftime("%A, %B %d, %Y %X-%Z", localtime) );
66             }
67              
68             sub _client_input {
69 0     0   0 undef;
70             }
71              
72             sub _client_flushed {
73 1     1   1132 my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
74 1         5 delete $self->{Clients}->{ $wheel_id }->{Wheel};
75 1         133 delete $self->{Clients}->{ $wheel_id };
76 1         3 undef;
77             }
78              
79             sub _get_datagram {
80 1     1   1335 my ( $kernel, $self, $socket ) = @_[ KERNEL, OBJECT, ARG0 ];
81              
82 1         11 my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
83 1 50       4 return unless defined $remote_address;
84              
85 1         105 my $output = strftime("%A, %B %d, %Y %X-%Z", localtime);
86 1 50       22 send( $socket, $output, 0, $remote_address ) == length( $output )
87             or warn "Trouble sending response: $!";
88              
89 1         4 undef;
90             }
91              
92             qq[What time is love?];
93              
94             __END__