blib/lib/POE/Component/Server/Echo.pm

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