File Coverage

blib/lib/POE/Component/Server/Qotd.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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              
12 3     3   108284 use strict;
  3         8  
  3         118  
13 3     3   17 use warnings;
  3         5  
  3         85  
14 3     3   17 use Carp;
  3         9  
  3         289  
15 3     3   1478 use POE;
  0            
  0            
16             use Socket;
17             use base qw(POE::Component::Server::Echo);
18             use vars qw($VERSION);
19              
20             use constant DATAGRAM_MAXLEN => 1024;
21             use constant DEFAULT_PORT => 17;
22              
23             $VERSION = '1.14';
24              
25             sub spawn {
26             my $package = shift;
27             croak "$package requires an even number of parameters" if @_ & 1;
28              
29             my %parms = @_;
30              
31             $parms{'Alias'} = 'Qotd-Server' unless defined $parms{'Alias'} and $parms{'Alias'};
32             $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0;
33             $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0;
34             $parms{'Quote'} = 'Parts that positively cannot be assembled in improper order will be.'
35             unless defined $parms{'Quote'} and length $parms{'Quote'} <= 512;
36              
37             my $self = bless { }, $package;
38              
39             $self->{CONFIG} = \%parms;
40              
41             POE::Session->create(
42             object_states => [
43             $self => { _start => '_server_start',
44             _stop => '_server_stop',
45             shutdown => '_server_close' },
46             $self => [ qw(_accept_new_client _accept_failed _client_input _client_error _client_flushed _get_datagram) ],
47             ],
48             ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ),
49             );
50              
51             return $self;
52             }
53              
54             sub _accept_new_client {
55             my ($kernel,$self,$socket,$peeraddr,$peerport,$wheel_id) = @_[KERNEL,OBJECT,ARG0 .. ARG3];
56             $peeraddr = inet_ntoa($peeraddr);
57              
58             my $wheel = POE::Wheel::ReadWrite->new (
59             Handle => $socket,
60             Filter => POE::Filter::Line->new(),
61             InputEvent => '_client_input',
62             ErrorEvent => '_client_error',
63             FlushedEvent => '_client_flushed',
64             );
65              
66             $self->{Clients}->{ $wheel->ID() } = { Wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport };
67             $wheel->put( $self->{CONFIG}->{Quote} );
68             undef;
69             }
70              
71             sub _client_input {
72             undef;
73             }
74              
75             sub _client_flushed {
76             my ($kernel,$self,$wheel_id) = @_[KERNEL,OBJECT,ARG0];
77             delete $self->{Clients}->{ $wheel_id }->{Wheel};
78             delete $self->{Clients}->{ $wheel_id };
79             undef;
80             }
81              
82             sub _get_datagram {
83             my ( $kernel, $self, $socket ) = @_[ KERNEL, OBJECT, ARG0 ];
84              
85             my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
86             return unless defined $remote_address;
87              
88             send( $socket, $self->{CONFIG}->{Quote}, 0, $remote_address ) == length( $self->{CONFIG}->{Quote} )
89             or warn "Trouble sending response: $!";
90             undef;
91             }
92              
93             1;
94             __END__