1Test::POE::Client::TCP(U3s)er Contributed Perl DocumentatTieosnt::POE::Client::TCP(3)
2
3
4

NAME

6       Test::POE::Client::TCP - A POE Component providing TCP client services
7       for test cases
8

SYNOPSIS

10         use strict;
11         use Socket;
12         use Test::More tests => 15;
13         use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line);
14         use Test::POE::Client::TCP;
15
16         my @data = (
17           'This is a test',
18           'This is another test',
19           'This is the last test',
20         );
21
22         POE::Session->create(
23           package_states => [
24               'main' => [qw(
25                               _start
26                               _accept
27                               _failed
28                               _sock_in
29                               _sock_err
30                               testc_registered
31                               testc_connected
32                               testc_disconnected
33                               testc_input
34                               testc_flushed
35               )],
36           ],
37           heap => { data => \@data, },
38         );
39
40         $poe_kernel->run();
41         exit 0;
42
43         sub _start {
44           my ($kernel,$heap) = @_[KERNEL,HEAP];
45           $heap->{listener} = POE::Wheel::SocketFactory->new(
46               BindAddress    => '127.0.0.1',
47               SuccessEvent   => '_accept',
48               FailureEvent   => '_failed',
49               SocketDomain   => AF_INET,             # Sets the socket() domain
50               SocketType     => SOCK_STREAM,         # Sets the socket() type
51               SocketProtocol => 'tcp',               # Sets the socket() protocol
52               Reuse          => 'on',                # Lets the port be reused
53           );
54           $heap->{testc} = Test::POE::Client::TCP->spawn();
55           return;
56         }
57
58         sub _accept {
59           my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0];
60           my $wheel = POE::Wheel::ReadWrite->new(
61               Handle       => $socket,
62               InputEvent   => '_sock_in',
63               ErrorEvent   => '_sock_err',
64           );
65           $heap->{wheels}->{ $wheel->ID } = $wheel;
66           return;
67         }
68
69         sub _failed {
70           my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3];
71           die "Wheel $wheel_id generated $operation error $errnum: $errstr\n";
72           return;
73         }
74
75         sub _sock_in {
76           my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1];
77           pass('Got input from client');
78           $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id };
79           return;
80         }
81
82         sub _sock_err {
83           my ($heap,$wheel_id) = @_[HEAP,ARG3];
84           pass('Client disconnected');
85           delete $heap->{wheels}->{ $wheel_id };
86           return;
87         }
88
89         sub testc_registered {
90           my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0];
91           pass($_[STATE]);
92           my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0];
93           $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } );
94           return;
95         }
96
97         sub testc_connected {
98           my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER];
99           pass($_[STATE]);
100           $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] );
101           return;
102         }
103
104         sub testc_flushed {
105           pass($_[STATE]);
106           return;
107         }
108
109         sub testc_input {
110           my ($heap,$input) = @_[HEAP,ARG0];
111           pass('Got something back from the server');
112           my $data = shift @{ $heap->{data} };
113           ok( $input eq $data, "Data matched: '$input'" );
114           unless ( scalar @{ $heap->{data} } ) {
115             $heap->{testc}->terminate();
116             return;
117           }
118           $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] );
119           return;
120         }
121
122         sub testc_disconnected {
123           my ($heap,$state) = @_[HEAP,STATE];
124           pass($state);
125           delete $heap->{wheels};
126           delete $heap->{listener};
127           $heap->{testc}->shutdown();
128           return;
129         }
130

DESCRIPTION

132       Test::POE::Client::TCP is a POE component that provides a TCP client
133       framework for inclusion in client component test cases, instead of
134       having to roll your own.
135
136       Once registered with the component, a session will receive events
137       related to connections made, disconnects, flushed output and input from
138       the specified server.
139

CONSTRUCTOR

141       "spawn"
142           Takes a number of optional arguments:
143
144             'alias', set an alias on the component;
145             'address', the remote address to connect to;
146             'port', the remote port to connect to;
147             'options', a hashref of POE::Session options;
148             'filter', specify a POE::Filter to use for client connections, default is POE::Filter::Line;
149             'inputfilter', specify a POE::Filter for client input;
150             'outputfilter', specify a POE::Filter for output to clients;
151             'localaddr', specify that connections be made from a particular local address;
152             'localport', specify that connections be made from a particular port;
153             'autoconnect', set to a true value to make the poco connect immediately;
154             'prefix', specify an event prefix other than the default of 'testc';
155
156           The semantics for "filter", "inputfilter" and "outputfilter" are
157           the same as for POE::Component::Server::TCP in that one may provide
158           either a "SCALAR", "ARRAYREF" or an "OBJECT".
159
160           If the component is "spawn"ed within another session it will
161           automatically "register" the parent session to receive "all"
162           events.
163
164           "address" and "port" are optional within "spawn", but if they
165           aren't specified they must be provided to subsequent "connect"s. If
166           "autoconnect" is specified, "address" and "port" must also be
167           defined.
168

METHODS

170       "connect"
171           Initiates a connection to the given server. Takes a number of
172           parameters:
173
174             'address', the remote address to connect to;
175             'port', the remote port to connect to;
176             'localaddr', specify that connections be made from a particular local address, optional;
177             'localport', specify that connections be made from a particular port, optional;
178
179           "address" and "port" are optional if they have been already
180           specified during "spawn".
181
182       "session_id"
183           Returns the POE::Session ID of the component.
184
185       "shutdown"
186           Terminates the component. It will terminate any pending connects or
187           connections.
188
189       "server_info"
190           Retrieves socket information about the current connection. In a
191           list context it returns a list consisting of, in order, the server
192           address, the server TCP port, our address and our TCP port. In a
193           scalar context it returns a HASHREF with the following keys:
194
195             'peeraddr', the server address;
196             'peerport', the server TCP port;
197             'sockaddr', our address;
198             'sockport', our TCP port;
199
200       "send_to_server"
201           Send some output to the connected server. The first parameter is a
202           string of text to send. This parameter may also be an arrayref of
203           items to send to the client. If the filter you have used requires
204           an arrayref as input, nest that arrayref within another arrayref.
205
206       "disconnect"
207           Places the server connection into pending disconnect state. Set
208           this, then send an applicable message to the server using
209           "send_to_server()" and the server connection will be terminated.
210
211       "terminate"
212           Immediately disconnects a server conenction.
213
214       "wheel"
215           Returns the underlying POE::Wheel::ReadWrite object if we are
216           currently connected to a server, "undef" otherwise.  You can use
217           this method to call methods on the wheel object to switch filters,
218           etc. Exercise caution.
219
220       "alias"
221           Returns the currently configured alias.
222

INPUT EVENTS

224       These are events that the component will accept:
225
226       "register"
227           Takes N arguments: a list of event names that your session wants to
228           listen for, minus the 'testc_' prefix.
229
230           Registering for 'all' will cause it to send all TESTC-related
231           events to you; this is the easiest way to handle it.
232
233       "unregister"
234           Takes N arguments: a list of event names which you don't want to
235           receive. If you've previously done a 'register' for a particular
236           event which you no longer care about, this event will tell the poco
237           to stop sending them to you. (If you haven't, it just ignores you.
238           No big deal).
239
240       "connect"
241           Initiates a connection to the given server. Takes a number of
242           parameters:
243
244             'address', the remote address to connect to;
245             'port', the remote port to connect to;
246             'localaddr', specify that connections be made from a particular local address, optional;
247             'localport', specify that connections be made from a particular port, optional;
248
249           "address" and "port" are optional if they have been already
250           specified during "spawn".
251
252       "shutdown"
253           Terminates the component. It will terminate any pending connects or
254           connections.
255
256       "send_to_server"
257           Send some output to the connected server. The first parameter is a
258           string of text to send. This parameter may also be an arrayref of
259           items to send to the client. If the filter you have used requires
260           an arrayref as input, nest that arrayref within another arrayref.
261
262       "disconnect"
263           Places the server connection into pending disconnect state. Set
264           this, then send an applicable message to the server using
265           "send_to_server()" and the server connection will be terminated.
266
267       "terminate"
268           Immediately disconnects a server conenction.
269

OUTPUT EVENTS

271       The component sends the following events to registered sessions. If you
272       have changed the "prefix" option in "spawn" then substitute "testc"
273       with the event prefix that you specified.
274
275       "testc_registered"
276           This event is sent to a registering session. ARG0 is the
277           Test::POE::Client::TCP object.
278
279       "testc_socket_failed"
280           Generated if the component cannot make a socket connection.  ARG0
281           contains the name of the operation that failed.  ARG1 and ARG2 hold
282           numeric and string values for $!, respectively.
283
284       "testc_connected"
285           Generated whenever a connection is established. ARG0 is the
286           server's IP address, ARG1 is the server's TCP port.  ARG3 is our IP
287           address and ARG4 is our socket port.
288
289       "testc_disconnected"
290           Generated whenever we disconnect from the server.
291
292       "testc_input"
293           Generated whenever the server sends us some traffic. ARG0 is the
294           data sent ( tokenised by whatever POE::Filter you specified ).
295
296       "testc_flushed"
297           Generated whenever anything we send to the server is actually
298           flushed down the 'line'.
299

AUTHOR

301       Chris "BinGOs" Williams <chris@bingosnet.co.uk>
302
303       with code borrowed from POE::Component::Server::TCP by Rocco Caputo,
304       Ann Barcomb and Jos Boumans.
305

LICENSE

307       Copyright "(c)" Chris Williams, Rocco Caputo, Ann Barcomb and Jos
308       Boumans.
309
310       This module may be used, modified, and distributed under the same terms
311       as Perl itself. Please see the license that came with your Perl
312       distribution for details.
313

SEE ALSO

315       POE
316
317       POE::Component::Server::TCP
318
319
320
321perl v5.12.1                      2010-04-28         Test::POE::Client::TCP(3)
Impressum