| 1 | # $Id: Echo.pm,v 1.3 2005/01/27 08:37:22 chris Exp $ | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # POE::Component::Server::Echo, by Chris 'BinGOs' Williams <chris@bingosnet.co.uk> | ||||||||||||||||||||||||||||
| 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::Echo; | ||||||||||||||||||||||||||||
| 11 | 3 | $POE::Component::Server::Echo::VERSION = '1.66'; | |||||||||||||||||||||||||||
| 12 | #ABSTRACT: A POE component that implements an RFC 862 Echo server. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use POE qw( Wheel::SocketFactory Wheel::ReadWrite Driver::SysRW | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | Filter::Line ); | |||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use constant DATAGRAM_MAXLEN => 1024; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant DEFAULT_PORT => 7; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub spawn { | ||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=9 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 2 | my $package = shift; | |||||||||||||||||||||||||||
| 27 | 2 | ▶ | croak "$package requires an even number of parameters" if @_ & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | my %parms = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $parms{'Alias'} = 'Echo-Server' unless defined $parms{'Alias'} and $parms{'Alias'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $parms{"Alias"} and $parms{"Alias"}
MC/DC0%0/2
defined $parms{"Alias"} and $parms{"Alias"}
defined $parms{"Alias"}
$parms{"Alias"}
Truth table33%1/3
A and B
A= defined $parms{"Alias"}B= $parms{"Alias"}
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | $parms{'tcp'} = 1 unless defined $parms{'tcp'} and $parms{'tcp'} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $parms{"tcp"} and $parms{"tcp"} == 0
MC/DC0%0/2
defined $parms{"tcp"} and $parms{"tcp"} == 0
defined $parms{"tcp"}
$parms{"tcp"} == 0
Truth table33%1/3
A and B
A= defined $parms{"tcp"}B= $parms{"tcp"} == 0
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | $parms{'udp'} = 1 unless defined $parms{'udp'} and $parms{'udp'} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $parms{"udp"} and $parms{"udp"} == 0
MC/DC0%0/2
defined $parms{"udp"} and $parms{"udp"} == 0
defined $parms{"udp"}
$parms{"udp"} == 0
Truth table33%1/3
A and B
A= defined $parms{"udp"}B= $parms{"udp"} == 0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | my $self = bless( { }, $package ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | $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 _get_datagram) ], | ||||||||||||||||||||||||||||
| 45 | ], | ||||||||||||||||||||||||||||
| 46 | 2 | ▶ | ( ref $parms{'options'} eq 'HASH' ? ( options => $parms{'options'} ) : () ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | sub _server_start { | ||||||||||||||||||||||||||
|
Subroutine
_server_start: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||
| 53 | 2 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | $kernel->alias_set( $self->{CONFIG}->{Alias} ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | if ( $self->{CONFIG}->{tcp} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | $self->{Listener} = POE::Wheel::SocketFactory->new( | ||||||||||||||||||||||||||||
| 59 | ( defined ( $self->{CONFIG}->{BindAddress} ) ? ( BindAddress => $self->{CONFIG}->{BindAddress} ) : () ), | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | ( defined ( $self->{CONFIG}->{BindPort} ) ? ( BindPort => $self->{CONFIG}->{BindPort} ) : ( BindPort => DEFAULT_PORT ) ), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | SuccessEvent => '_accept_new_client', | ||||||||||||||||||||||||||||
| 62 | FailureEvent => '_accept_failed', | ||||||||||||||||||||||||||||
| 63 | SocketDomain => AF_INET, # Sets the socket() domain | ||||||||||||||||||||||||||||
| 64 | SocketType => SOCK_STREAM, # Sets the socket() type | ||||||||||||||||||||||||||||
| 65 | SocketProtocol => 'tcp', # Sets the socket() protocol | ||||||||||||||||||||||||||||
| 66 | Reuse => 'on', # Lets the port be reused | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 2 | ▶ | if ( $self->{CONFIG}->{udp} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | my $proto = getprotobyname('udp'); | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | my $port = defined ( $self->{CONFIG}->{BindPort} ) ? $self->{CONFIG}->{BindPort} : DEFAULT_PORT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2 | my $paddr = sockaddr_in($port, INADDR_ANY); | |||||||||||||||||||||||||||
| 73 | 2 | ▶ | socket( my $socket, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | bind( $socket, $paddr) || die "bind: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | $self->{udp_socket} = $socket; | |||||||||||||||||||||||||||
| 76 | 2 | $kernel->select_read( $self->{udp_socket}, "_get_datagram" ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 2 | undef; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | sub _server_stop { | ||||||||||||||||||||||||||
|
Subroutine
_server_stop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 2 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 83 | 2 | undef; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub _server_close { | ||||||||||||||||||||||||||
|
Subroutine
_server_close: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 2 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | delete $self->{Listener}; | |||||||||||||||||||||||||||
| 90 | 2 | delete $self->{Clients}; | |||||||||||||||||||||||||||
| 91 | 2 | $kernel->select( $self->{udp_socket} ); | |||||||||||||||||||||||||||
| 92 | 2 | delete $self->{udp_socket}; | |||||||||||||||||||||||||||
| 93 | 2 | $kernel->alias_remove( $self->{CONFIG}->{Alias} ); | |||||||||||||||||||||||||||
| 94 | 2 | undef; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub _accept_new_client { | ||||||||||||||||||||||||||
|
Subroutine
_accept_new_client: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 1 | my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0 .. ARG2]; | |||||||||||||||||||||||||||
| 99 | 1 | $peeraddr = inet_ntoa($peeraddr); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my $wheel = POE::Wheel::ReadWrite->new ( | |||||||||||||||||||||||||||
| 102 | Handle => $socket, | ||||||||||||||||||||||||||||
| 103 | Filter => POE::Filter::Line->new(), | ||||||||||||||||||||||||||||
| 104 | InputEvent => '_client_input', | ||||||||||||||||||||||||||||
| 105 | ErrorEvent => '_client_error', | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | my $wheel_id = $wheel->ID(); | |||||||||||||||||||||||||||
| 109 | 1 | $self->{Clients}->{ $wheel_id }->{Wheel} = $wheel; | |||||||||||||||||||||||||||
| 110 | 1 | $self->{Clients}->{ $wheel_id }->{peeraddr} = $peeraddr; | |||||||||||||||||||||||||||
| 111 | 1 | $self->{Clients}->{ $wheel_id }->{peerport} = $peerport; | |||||||||||||||||||||||||||
| 112 | 1 | undef; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub _accept_failed { | ||||||||||||||||||||||||||
|
Subroutine
_accept_failed: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 117 | 0 | $kernel->yield( 'shutdown' ); | |||||||||||||||||||||||||||
| 118 | 0 | undef; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub _client_input { | ||||||||||||||||||||||||||
|
Subroutine
_client_input: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 122 | 1 | my ($kernel,$self,$input,$wheel_id) = @_[KERNEL,OBJECT,ARG0,ARG1]; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | if ( defined ( $self->{Clients}->{ $wheel_id } ) and defined ( $self->{Clients}->{ $wheel_id }->{Wheel} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"Clients"}{$wheel_id} and defined $self->{"Clients"}{$wheel_id}{"Wheel"}
MC/DC0%0/2
defined $self->{"Clients"}{$wheel_id} and defined $self->{"Clients"}{$wheel_id}{"Wheel"}
defined $self->{"Clients"}{$wheel_id}
defined $self->{"Clients"}{$wheel_id}{"Wheel"}
Truth table33%1/3
A and B
A= defined $self->{"Clients"}{$wheel_id}B= defined $self->{"Clients"}{$wheel_id}{"Wheel"}
| |||||||||||||||||||||||||||||
| 125 | 1 | $self->{Clients}->{ $wheel_id }->{Wheel}->put($input); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 1 | undef; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | sub _client_error { | ||||||||||||||||||||||||||
|
Subroutine
_client_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 1 | my ($self,$wheel_id) = @_[OBJECT,ARG3]; | |||||||||||||||||||||||||||
| 132 | 1 | delete $self->{Clients}->{ $wheel_id }; | |||||||||||||||||||||||||||
| 133 | 1 | undef; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub _get_datagram { | ||||||||||||||||||||||||||
|
Subroutine
_get_datagram: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 137 | 1 | my ( $kernel, $socket ) = @_[ KERNEL, ARG0 ]; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 ); | |||||||||||||||||||||||||||
| 140 | 1 | ▶ | return unless defined $remote_address; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | send( $socket, $message, 0, $remote_address ) == length($message) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | or warn "Trouble sending response: $!"; | ||||||||||||||||||||||||||||
| 144 | 1 | undef; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub sockname_tcp { | ||||||||||||||||||||||||||
|
Subroutine
sockname_tcp: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | 1 | my $name; | |||||||||||||||||||||||||||
| 150 | 1 | ▶ | $name = $self->{Listener}->getsockname() if $self->{CONFIG}->{tcp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | return unless $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 1 | return sockaddr_in($name); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | sub sockname_udp { | ||||||||||||||||||||||||||
|
Subroutine
sockname_udp: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 1 | ▶ | return unless $self->{CONFIG}->{udp} and $self->{udp_socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"CONFIG"}{"udp"} and $self->{"udp_socket"}
MC/DC0%0/2
$self->{"CONFIG"}{"udp"} and $self->{"udp_socket"}
$self->{"CONFIG"}{"udp"}
$self->{"udp_socket"}
Truth table33%1/3
A and B
A= $self->{"CONFIG"}{"udp"}B= $self->{"udp_socket"}
| |||||||||||||||||||||||||||||
| 158 | 1 | return sockaddr_in( getsockname $self->{udp_socket} ); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | qq[ECHO! ECHO...ECHO...ECHO...ECHO...ECHO...ECHO...ECHo...ECho...Echo...echo]; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||