File Coverage

blib/lib/POE/Component/Server/Time.pm
Criterion Covered Total %
statement 50 51 98.0
branch 7 14 50.0
condition 3 9 33.3
subroutine 12 13 92.3
pod 1 1 100.0
total 73 88 82.9


line stmt bran cond sub pod time code
1             # $Id: Time.pm,v 1.1.1.1 2005/01/27 15:36:15 chris Exp $
2             #
3             # POE::Component::Server::Time, 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::Time;
11             $POE::Component::Server::Time::VERSION = '1.16';
12             #ABSTRACT: A POE component that implements an RFC 868 Time server.
13              
14 2     2   28710 use strict;
  2         3  
  2         50  
15 2     2   5 use warnings;
  2         3  
  2         43  
16 2     2   6 use Carp;
  2         3  
  2         128  
17 2     2   902 use POE;
  2         79708  
  2         13  
18 2     2   93807 use Socket;
  2         3  
  2         783  
19 2     2   8 use base qw(POE::Component::Server::Echo);
  2         2  
  2         986  
20              
21 2     2   36905 use constant DATAGRAM_MAXLEN => 1024;
  2         2  
  2         86  
22 2     2   7 use constant DEFAULT_PORT => 37;
  2         3  
  2         736  
23              
24             sub spawn {
25 2     2 1 27 my $package = shift;
26 2 50       7 croak "$package requires an even number of parameters" if @_ & 1;
27              
28 2         9 my %parms = @_;
29              
30 2 50 33     16 $parms{'Alias'} = 'Time-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     9 $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              
38             POE::Session->create(
39             object_states => [
40             $self => { _start => '_server_start',
41             _stop => '_server_stop',
42             shutdown => '_server_close' },
43             $self => [ qw(_accept_new_client _accept_failed _client_input _client_error _client_flushed _get_datagram) ],
44             ],
45 2 50       33 ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
46             );
47              
48 2         2665 return $self;
49             }
50              
51             sub _accept_new_client {
52 1     1   1348 my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
53 1         6 $peeraddr = inet_ntoa($peeraddr);
54              
55 1         8 my $wheel = POE::Wheel::ReadWrite->new (
56             Handle => $socket,
57             Filter => POE::Filter::Line->new(),
58             InputEvent => '_client_input',
59             ErrorEvent => '_client_error',
60             FlushedEvent => '_client_flushed',
61             );
62              
63 1         279 $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport };;
64 1         10 $wheel->put( time );
65 1         59 undef;
66             }
67              
68             sub _client_input {
69 0     0   0 undef;
70             }
71              
72             sub _client_flushed {
73 1     1   1038 my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
74 1         5 delete $self->{Clients}->{ $wheel_id }->{Wheel};
75 1         132 delete $self->{Clients}->{ $wheel_id };
76 1         2 undef;
77             }
78              
79             sub _get_datagram {
80 1     1   1354 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         5 my $output = time();
86 1 50       19 send( $socket, $output, 0, $remote_address ) == length( $output )
87             or warn "Trouble sending response: $!";
88 1         3 undef;
89             }
90              
91             qq[Mind is perpetual motion. Its symbol is the sphere.];
92              
93             __END__