TCP.pm

filter: Statementstmt 86.7% 131 / 151 Branchbran 42.1% 16 / 38 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 79.4% 27 / 34 Podpod 0.0% 0 / 5 totaltotal 71.2% 176 / 247 CC 26 SCAR 26.4
CC
26
Cov
74%
new
31.7
_default_client_error
30.0
__ANON__
2.1
CRAP 14.0
« TCP/PoeDebug.pm ↑ Summary
1 # $Id: TCP.pm,v 1.2 2004/08/02 09:40:41 avpurshottam Exp $
2 # TCP Proxy Component - Andrew v. Purshottam andy@andypurshottam.com 22 Jun 2004
3 # Module structure adapted from PoCo::Server::TCP
4
5 # to do:
6 # - clean up exported logic below
7 # - rationalize and document session aliases
8 # - document the connection between per client server sessions and
9 # per client client [sic!] sessions
10 # - change OrigPort and OrigAddress to RemotePort and RemoteServer
11 # - should the test code get installed and if so where? Study a famous module
12 # for example.
13
14 package POE::Component::Proxy::TCP;
15
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 2 ▶use vars qw(@ISA @EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 @ISA = qw(Exporter);
21
22 2 @EXPORT = qw();
23 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 $VERSION = (qw($Revision: 1.2 $ ))[1];
25
26 2 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Socket qw(INADDR_ANY inet_ntoa);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use POSIX qw(ECONNABORTED ECONNRESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
29
30 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use POE::Component::Client::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use POE::Filter::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use POE::Component::Server::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 2 ▶use POE::Component::Proxy::TCP::PoeDebug;
Subroutine BEGIN: called CC=1 SCAR=0.0
38
39 2 ▶use fields qw(clients alias address
Subroutine BEGIN: called CC=1 SCAR=0.0
40 orig_address port remote_client_filter
41 remote_server_input_filter remote_server_output_filter
42 data_from_client data_from_server
43 session_type session_params args client_connected
44 2 );
45
46 1 ▶sub new {
Subroutine new: called CC=20 SCAR=34.5
Pod undocumented
47 1 my $type = shift;
48
49 # Helper so we don't have to type it all day. $mi is a name I call
50 # myself.
51 1 my $mi = $type . '->new()';
52 # All instance state should now live in the hash.
53 1 my $self = bless {@_},$type;
54
55 # hash mapping server session ids to client session ids.
56 1 $self->{clients} = {};
57
58 # param list is list of name, value pairs so must be even number.
59 1 ▶ croak "$mi requires an even number of parameters." if (@_ & 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 1 1
60 1 my %param = @_;
61
62 # Validate what we're given.
63 1 ▶ croak "$mi needs a Port parameter" unless exists $param{Port};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"Port"} 0 1 1
64
65 # Extract parameters.
66 1 $self->{alias} = delete $param{Alias};
67 1 $self->{address} = delete $param{Address};
68 1 $self->{orig_address} = delete $param{OrigAddress};
69 1 $self->{port} = delete $param{Port};
70 1 $self->{orig_port} = delete $param{OrigPort};
71 1 $self->{remote_client_filter} = delete $param{RemoteClientFilter};
72 1 $self->{remote_server_input_filter} = delete $param{RemoteServerInputFilter};
73 1 $self->{remote_server_output_filter} = delete $param{RemoteServerOutputFilter};
74
75 1 foreach ( qw( DataFromClient DataFromServer)) {
76 croak "$_ must be a coderef"
77 2 ▶ if defined($param{$_}) and ref($param{$_}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{$_} and ref $param{$_} ne "CODE" 0 2 2
Condition33%1/3
defined $param{$_} and ref $param{$_} ne "CODE"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $param{$_} and ref $param{$_} ne "CODE"
defined $param{$_} ref $param{$_} ne "CODE"
Truth table33%1/3
A and B
A= defined $param{$_}B= ref $param{$_} ne "CODE"
ABresult
0X0
100
111
78 }
79
80 1 $self->{data_from_client} = delete $param{DataFromClient};
81 1 $self->{data_from_server} = delete $param{DataFromServer};
82
83 # Defaults.
84 1 ▶ $self->{address} = INADDR_ANY unless defined $self->{address};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"address"} 1 0 1
85 $self->{remote_client_filter} = "POE::Filter::Stream"
86 1 ▶ unless defined $self->{remote_client_filter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"remote_client_filter"} 1 0 1
87 $self->{remote_server_input_filter} = "POE::Filter::Stream"
88 1 ▶ unless defined $self->{remote_server_input_filter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"remote_server_input_filter"} 1 0 1
89 $self->{remote_server_output_filter} = "POE::Filter::Stream"
90 1 ▶ unless defined $self->{remote_server_output_filter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"remote_server_output_filter"} 1 0 1
91
92 1 ▶ $self->{session_type} = 'POE::Session' unless defined $self->{session_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"session_type"} 1 0 1
93 1 ▶ if (defined($self->{session_params}) && ref($self->{session_params})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'session_params'} and ref $self->{'session_params'}) { } 0 1 1
Condition33%1/3
defined $self->{"session_params"} and ref $self->{"session_params"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"session_params"} and ref $self->{"session_params"}
defined $self->{"session_params"} ref $self->{"session_params"}
Truth table33%1/3
A and B
A= defined $self->{"session_params"}B= ref $self->{"session_params"}
ABresult
0X0
100
111
94 0 ▶ if (ref($self->{session_params}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"session_params"} ne "ARRAY") 0 0 0
95 0 croak "SessionParams must be an array reference";
96 }
97 } else {
98 1 $self->{session_params} = [ ];
99 }
100
101 # $self->{client_error} = \&_default_client_error unless defined $self->{client_error};
102 1 ▶ $self->{client_connected} = sub {} unless defined $self->{client_connected};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client_connected"} 1 0 1
103 # $self->{client_disconnected} = sub {} unless defined $self->{client_disconnected};
104 # $self->{client_flushed} = sub {} unless defined $self->{client_flushed};
105
106 1 ▶ $self->{data_from_client} = sub {} unless defined $self->{data_from_client};
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data_from_client"} 0 1 1
107 1 ▶ $self->{data_from_server} = sub {} unless defined $self->{data_from_server};
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data_from_server"} 0 1 1
108
109 1 ▶ $self->{args} = [] unless defined $self->{args};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"args"} 1 0 1
110
111 # Extra states.
112
113
114 1 my $shutdown_on_error = 1;
115 1 ▶ if (exists $param{ClientShutdownOnError}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"ClientShutdownOnError"}) 0 1 1
116 0 $shutdown_on_error = delete $param{ClientShutdownOnError};
117 }
118
119 # Complain about strange things we're given.
120 1 foreach (sort keys %param) {
121 0 carp "$mi doesn't recognize \"$_\" as a parameter";
122 }
123
124 # Server side of proxy, clients connect to this
125 $self->{server_component} = POE::Component::Server::TCP->new
126 (Alias => $self->{alias},
127 Port => $self->{port},
128 32 ▶ InlineStates => { send => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
129 32 my ( $heap, $message ) = @_[ HEAP, ARG0 ];
130 32 dbprint(5, "sending to client:$message");
131 32 $heap->{client}->put($message);
132
133 } },
134 Args => [$self], # so handle_client_connect_to_proxy_server gets $self
135 ClientConnected => \&handle_client_connect_to_proxy_server,
136 ClientError => \&handle_remote_client_error,
137 ClientDisconnected => \&handle_remote_client_disconnect,
138 ClientInput => \&handle_input_from_remote_client,
139 ClientInputFilter => $self->{remote_server_input_filter},
140 ClientOutputFilter => $self->{remote_server_output_filter}
141 1 );
142 1 return $self;
143 }
144
145 0 ▶sub handle_remote_client_error {
Subroutine handle_remote_client_error: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
147 0 my $self = $heap->{self};
148 0 my $session_id = $session->ID;
149 0 dbprint(1, "Client $session_id disconnected due to error .");
150 0 delete $self->{clients}->{$session_id};
151 # XXX delete proxy client on heap???
152 };
153
154 2 ▶sub handle_remote_client_disconnect {
Subroutine handle_remote_client_disconnect: called CC=1 SCAR=0.0
Pod undocumented
155 2 my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];
156 2 my $self = $heap->{self};
157 2 my $session_id = $session->ID;
158 2 delete $heap->{proxy_client};
159 2 delete $self->{clients}->{$session_id};
160 2 dbprint(1, "Client $session_id disconnected.");
161 };
162
163 6 ▶sub handle_input_from_remote_client {
Subroutine handle_input_from_remote_client: called CC=1 SCAR=0.0
Pod undocumented
164 6 my ( $kernel, $session, $heap, $input ) = @_[ KERNEL, SESSION, HEAP, ARG0 ];
165 6 my $self = $heap->{self};
166 6 my $session_id = $session->ID;
167 6 my $proxy_client_session_alias = $self->{clients}->{$session_id};
168
169 dbprint(3, "Input:$input from $session_id sending to ",
170 6 $heap->{proxy_client_session_alias});
171
172 # send the input to the remote server
173 6 $kernel->post($heap->{proxy_client_session_alias}, "send_server", $input);
174 # do whatever application specification processing is called for.
175 6 $self->{data_from_client}->($input);
176 }
177
178 # called in a Proxy Server Connection Session, passed self as parameter,
179 # responsible for setting in session heap.
180 2 ▶sub handle_client_connect_to_proxy_server {
Subroutine handle_client_connect_to_proxy_server: called CC=1 SCAR=0.1
Pod undocumented
181 2 my ( $kernel, $session, $heap, $self ) = @_[ KERNEL, SESSION, HEAP, ARG0 ];
182 # The per client session spawed by PoCo::Server::TCP
183 2 my $session_id = $session->ID;
184 # Standard alias name.
185 # XXX Should use the alias passed to constructor as prefix!
186 2 my $proxy_client_session_alias = $self->{alias} . "client". $session_id;
187 2 $heap->{proxy_client_session_alias} = $proxy_client_session_alias;
188 # Provides access to proxy object in per client connection server component session.
189 2 $heap->{self} = $self;
190 2 $self->{clients}->{$session_id} = $proxy_client_session_alias;
191 2 dbprint(2, "Client $session_id connected.");
192 # invoke the client connected callback.
193 2 $self->{client_connected}->($session_id);
194 # self is passed down to Proxy Client Connection session.
195 # Create the per client connection client component session.
196 $heap->{proxy_client} =
197 POE::Component::Client::TCP->new
198 ( Alias => $proxy_client_session_alias,
199 RemoteAddress => $self->{orig_address},
200 RemotePort => $self->{orig_port},
201 Filter => $self->{remote_client_filter},
202 Args => [$self],
203 2 ▶ Started => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
204 2 my ( $kernel, $heap, $inner_self) = @_[ KERNEL, HEAP, ARG0];
205 2 $heap->{parent_client_session} = $session_id;
206 2 $heap->{self} = $inner_self;
207 2 $heap->{is_connected_to_server} = 0;
208 2 dbprint(3, "connected to $inner_self->{orig_address}:$inner_self->{orig_port}");
209 },
210 2 ▶ Connected => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
211 2 my ( $kernel, $heap) = @_[ KERNEL, HEAP];
212 2 $heap->{is_connected_to_server} = 1;
213 2 $heap->{parent_client_session} = $session_id;
214 2 dbprint(3, "connected to $self->{orig_address}:$self->{orig_port}");
215 },
216
217 # The connection failed.
218 0 ▶ ConnectError => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 dbprint(1, "could not connect to $self->{orig_address}:$self->{orig_port}");
220 0 $heap->{is_connected_to_server} = 0;
221 # XXX do something to shut the system down
222 # no in some applications wish to keep going and wait for new connection.
223 #$_[KERNEL]->yield("shutdown");
224 },
225
226 # The remote server has sent us something,
227 # so send it to the remote client and perform
228 # whatever aookication specific log (eg logging to
229 # screen) is required.
230 32 ▶ ServerInput => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
231 32 my ( $kernel, $heap, $input ) = @_[ KERNEL, HEAP, ARG0 ];
232 32 ▶ if (defined($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 32 0 32
233 32 dbprint(3, "Input from remote server $self->{orig_address} :",
234 "$self->{orig_port}: -$input- sending to",
235 "remote client and any callback");
236 $kernel->post($heap->{parent_client_session},
237 32 "send", $input);
238 32 $self->{data_from_server}->($input);
239 } else {
240 0 dbprint(1, "ServerInput event but no input!");
241 }
242 },
243
244 0 ▶ ConnectError => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
246 0 dbprint(2, "ConnectError from ORIG_SERVER on",
247 " $self->{orig_port} : $self->{orig_address}");
248 },
249
250 2 ▶ Disconnected => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
251 2 dbprint(1, "Disconnected from ORIG_SERVER on",
252 "$self->{orig_port} : $self->{orig_address}");
253 2 $_[KERNEL]->post($session_id, "shutdown");
254 },
255
256 2 ▶ ServerError => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
257 2 my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
258 2 dbprint(1, "ServerError from ORIG_SERVER on $self->{orig_port} :",
259 "$self->{orig_address}");
260 },
261
262 InlineStates =>
263 {
264 # Send data to the server.
265 6 ▶ send_server => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
266 6 my ( $heap, $message ) = @_[ HEAP, ARG0 ];
267 6 dbprint(3, "sending to server:$self->{orig_address}:$self->{orig_port}:",
268 "mess:$message.");
269 6 ▶ if ($heap->{is_connected_to_server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'is_connected_to_server'}) { } 6 0 6
270 6 $heap->{server}->put($message);
271 } else {
272 0 dbprint(1, "send_server error not connected to server.");
273 }
274 },
275 },
276 2 );
277 }
278
279 # The default server error handler logs to STDERR and shuts down the
280 # server connection.
281 # XXX remove both of these somoday, they are not used.
282
283 0 ▶sub _default_server_error {
Subroutine _default_server_error: not called CC=1 SCAR=6.9
284 0 warn( 'Server ', $_[SESSION]->ID,
285 " got $_[ARG0] error $_[ARG1] ($_[ARG2])\n"
286 );
287 # delete $_[HEAP]->{listener};
288 }
289
290 # The default client error handler logs to STDERR and shuts down the
291 # client connection.
292
293 0 ▶sub _default_client_error {
Subroutine _default_client_error: not called CC=5 SCAR=34.0
294 0 my ($syscall, $errno, $error) = @_[ARG0..ARG2];
295 0 ▶ unless ($syscall eq "read" and ($errno == 0 or $errno == ECONNRESET)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syscall eq "read" and $errno == 0 || $errno == 104) 0 0 0
Condition0%0/6
$syscall eq "read" and $errno == 0 || $errno == 104
!ll&&!rl&&r
000
$errno == 0 or $errno == 104
l!l&&r!l&&!r
000
MC/DC0%0/3
$syscall eq "read" and $errno == 0 || $errno == 104
$syscall eq "read" $errno == 0 $errno == 104
Truth table0%0/4
A and B or C
A= $syscall eq "read"B= $errno == 0C= $errno == 104
ABCresult
0XX0
1000
11X1
1011
296 0 ▶ $error = "(no error)" unless $errno;
Branch: 1 branch
BranchTrueFalseTotal
unless $errno 0 0 0
297 0 warn(
298 'Client session ', $_[SESSION]->ID,
299 " got $syscall error $errno ($error)\n"
300 );
301 }
302 }
303
304 2 1;
305
306 __END__
« TCP/PoeDebug.pm ↑ Summary