.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::POE::Client::TCP 3pm" .TH Test::POE::Client::TCP 3pm "2022-12-01" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::POE::Client::TCP \- A POE Component providing TCP client services for test cases .SH "VERSION" .IX Header "VERSION" version 1.26 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 5 \& use strict; \& use Socket; \& use Test::More tests => 15; \& use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); \& use Test::POE::Client::TCP; \& \& my @data = ( \& \*(AqThis is a test\*(Aq, \& \*(AqThis is another test\*(Aq, \& \*(AqThis is the last test\*(Aq, \& ); \& \& POE::Session\->create( \& package_states => [ \& \*(Aqmain\*(Aq => [qw( \& _start \& _accept \& _failed \& _sock_in \& _sock_err \& testc_registered \& testc_connected \& testc_disconnected \& testc_input \& testc_flushed \& )], \& ], \& heap => { data => \e@data, }, \& ); \& \& $poe_kernel\->run(); \& exit 0; \& \& sub _start { \& my ($kernel,$heap) = @_[KERNEL,HEAP]; \& $heap\->{listener} = POE::Wheel::SocketFactory\->new( \& BindAddress => \*(Aq127.0.0.1\*(Aq, \& SuccessEvent => \*(Aq_accept\*(Aq, \& FailureEvent => \*(Aq_failed\*(Aq, \& SocketDomain => AF_INET, # Sets the socket() domain \& SocketType => SOCK_STREAM, # Sets the socket() type \& SocketProtocol => \*(Aqtcp\*(Aq, # Sets the socket() protocol \& Reuse => \*(Aqon\*(Aq, # Lets the port be reused \& ); \& $heap\->{testc} = Test::POE::Client::TCP\->spawn(); \& return; \& } \& \& sub _accept { \& my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; \& my $wheel = POE::Wheel::ReadWrite\->new( \& Handle => $socket, \& InputEvent => \*(Aq_sock_in\*(Aq, \& ErrorEvent => \*(Aq_sock_err\*(Aq, \& ); \& $heap\->{wheels}\->{ $wheel\->ID } = $wheel; \& return; \& } \& \& sub _failed { \& my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; \& die "Wheel $wheel_id generated $operation error $errnum: $errstr\en"; \& return; \& } \& \& sub _sock_in { \& my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; \& pass(\*(AqGot input from client\*(Aq); \& $heap\->{wheels}\->{ $wheel_id }\->put( $input ) if $heap\->{wheels}\->{ $wheel_id }; \& return; \& } \& \& sub _sock_err { \& my ($heap,$wheel_id) = @_[HEAP,ARG3]; \& pass(\*(AqClient disconnected\*(Aq); \& delete $heap\->{wheels}\->{ $wheel_id }; \& return; \& } \& \& sub testc_registered { \& my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; \& pass($_[STATE]); \& my $port = ( sockaddr_in( $_[HEAP]\->{listener}\->getsockname() ) )[0]; \& $kernel\->post( $sender, \*(Aqconnect\*(Aq, { address => \*(Aq127.0.0.1\*(Aq, port => $port } ); \& return; \& } \& \& sub testc_connected { \& my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; \& pass($_[STATE]); \& $kernel\->post( $sender, \*(Aqsend_to_server\*(Aq, $heap\->{data}\->[0] ); \& return; \& } \& \& sub testc_flushed { \& pass($_[STATE]); \& return; \& } \& \& sub testc_input { \& my ($heap,$input) = @_[HEAP,ARG0]; \& pass(\*(AqGot something back from the server\*(Aq); \& my $data = shift @{ $heap\->{data} }; \& ok( $input eq $data, "Data matched: \*(Aq$input\*(Aq" ); \& unless ( scalar @{ $heap\->{data} } ) { \& $heap\->{testc}\->terminate(); \& return; \& } \& $poe_kernel\->post( $_[SENDER], \*(Aqsend_to_server\*(Aq, $heap\->{data}\->[0] ); \& return; \& } \& \& sub testc_disconnected { \& my ($heap,$state) = @_[HEAP,STATE]; \& pass($state); \& delete $heap\->{wheels}; \& delete $heap\->{listener}; \& $heap\->{testc}\->shutdown(); \& return; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Test::POE::Client::TCP is a \s-1POE\s0 component that provides a \s-1TCP\s0 client framework for inclusion in client component test cases, instead of having to roll your own. .PP Once registered with the component, a session will receive events related to connections made, disconnects, flushed output and input from the specified server. .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" .ie n .IP """spawn""" 4 .el .IP "\f(CWspawn\fR" 4 .IX Item "spawn" Takes a number of optional arguments: .Sp .Vb 10 \& \*(Aqalias\*(Aq, set an alias on the component; \& \*(Aqaddress\*(Aq, the remote address to connect to; \& \*(Aqport\*(Aq, the remote port to connect to; \& \*(Aqoptions\*(Aq, a hashref of POE::Session options; \& \*(Aqfilter\*(Aq, specify a POE::Filter to use for client connections, default is POE::Filter::Line; \& \*(Aqinputfilter\*(Aq, specify a POE::Filter for client input; \& \*(Aqoutputfilter\*(Aq, specify a POE::Filter for output to clients; \& \*(Aqlocaladdr\*(Aq, specify that connections be made from a particular local address; \& \*(Aqlocalport\*(Aq, specify that connections be made from a particular port; \& \*(Aqautoconnect\*(Aq, set to a true value to make the poco connect immediately; \& \*(Aqprefix\*(Aq, specify an event prefix other than the default of \*(Aqtestc\*(Aq; \& \*(Aqtimeout\*(Aq, specify number of seconds to wait for socket timeouts; \& \*(Aqcontext\*(Aq, anything that can fit into a scalar, such as a ref, etc. \& \*(Aqusessl\*(Aq, enable SSL/TLS if POE::Component::SSLify is available; \& \*(Aqsslctx\*(Aq, set to a SSL Context to configure the SSL Connection; \& \*(Aqsslcert\*(Aq, set to a x509 Certificate(PEM encoded) to connect using a client cert; \& \*(Aqsslkey\*(Aq, set to a private Key(PEM encoded) to connect using a client cert; .Ve .Sp The semantics for \f(CW\*(C`filter\*(C'\fR, \f(CW\*(C`inputfilter\*(C'\fR and \f(CW\*(C`outputfilter\*(C'\fR are the same as for POE::Component::Server::TCP in that one may provide either a \f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`ARRAYREF\*(C'\fR or an \f(CW\*(C`OBJECT\*(C'\fR. .Sp If the component is \f(CW\*(C`spawn\*(C'\fRed within another session it will automatically \f(CW\*(C`register\*(C'\fR the parent session to receive \f(CW\*(C`all\*(C'\fR events. .Sp \&\f(CW\*(C`address\*(C'\fR and \f(CW\*(C`port\*(C'\fR are optional within \f(CW\*(C`spawn\*(C'\fR, but if they aren't specified they must be provided to subsequent \f(CW\*(C`connect\*(C'\fRs. If \&\f(CW\*(C`autoconnect\*(C'\fR is specified, \f(CW\*(C`address\*(C'\fR and \f(CW\*(C`port\*(C'\fR must also be defined. .Sp \&\f(CW\*(C`usessl\*(C'\fR is dependent on whether POE::Component::SSLify is available. .SH "METHODS" .IX Header "METHODS" .ie n .IP """connect""" 4 .el .IP "\f(CWconnect\fR" 4 .IX Item "connect" Initiates a connection to the given server. Takes a number of parameters: .Sp .Vb 5 \& \*(Aqaddress\*(Aq, the remote address to connect to; \& \*(Aqport\*(Aq, the remote port to connect to; \& \*(Aqlocaladdr\*(Aq, specify that connections be made from a particular local address, optional; \& \*(Aqlocalport\*(Aq, specify that connections be made from a particular port, optional; \& \*(Aqusessl\*(Aq, enable SSL/TLS if POE::Component::SSLify is available; .Ve .Sp \&\f(CW\*(C`address\*(C'\fR and \f(CW\*(C`port\*(C'\fR are optional if they have been already specified during \f(CW\*(C`spawn\*(C'\fR. .ie n .IP """session_id""" 4 .el .IP "\f(CWsession_id\fR" 4 .IX Item "session_id" Returns the POE::Session \s-1ID\s0 of the component. .ie n .IP """shutdown""" 4 .el .IP "\f(CWshutdown\fR" 4 .IX Item "shutdown" Terminates the component. It will terminate any pending connects or connections. .ie n .IP """server_info""" 4 .el .IP "\f(CWserver_info\fR" 4 .IX Item "server_info" Retrieves socket information about the current connection. In a list context it returns a list consisting of, in order, the server address, the server \s-1TCP\s0 port, our address and our \s-1TCP\s0 port. In a scalar context it returns a \s-1HASHREF\s0 with the following keys: .Sp .Vb 4 \& \*(Aqpeeraddr\*(Aq, the server address; \& \*(Aqpeerport\*(Aq, the server TCP port; \& \*(Aqsockaddr\*(Aq, our address; \& \*(Aqsockport\*(Aq, our TCP port; .Ve .ie n .IP """send_to_server""" 4 .el .IP "\f(CWsend_to_server\fR" 4 .IX Item "send_to_server" Send some output to the connected server. The first parameter is a string of text to send. This parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. .ie n .IP """disconnect""" 4 .el .IP "\f(CWdisconnect\fR" 4 .IX Item "disconnect" Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using \f(CW\*(C`send_to_server()\*(C'\fR and the server connection will be terminated. .ie n .IP """terminate""" 4 .el .IP "\f(CWterminate\fR" 4 .IX Item "terminate" Immediately disconnects a server connection. .ie n .IP """wheel""" 4 .el .IP "\f(CWwheel\fR" 4 .IX Item "wheel" Returns the underlying POE::Wheel::ReadWrite object if we are currently connected to a server, \f(CW\*(C`undef\*(C'\fR otherwise. You can use this method to call methods on the wheel object to switch filters, etc. Exercise caution. .ie n .IP """alias""" 4 .el .IP "\f(CWalias\fR" 4 .IX Item "alias" Returns the currently configured alias. .ie n .IP """context""" 4 .el .IP "\f(CWcontext\fR" 4 .IX Item "context" Returns whatever was provided as \f(CW\*(C`context\*(C'\fR when the component was spawned. If nothing was provided then it returns nothing. .SH "INPUT EVENTS" .IX Header "INPUT EVENTS" These are events that the component will accept: .ie n .IP """register""" 4 .el .IP "\f(CWregister\fR" 4 .IX Item "register" Takes N arguments: a list of event names that your session wants to listen for, minus the 'testc_' prefix. .Sp Registering for 'all' will cause it to send all TESTC-related events to you; this is the easiest way to handle it. .ie n .IP """unregister""" 4 .el .IP "\f(CWunregister\fR" 4 .IX Item "unregister" Takes N arguments: a list of event names which you don't want to receive. If you've previously done a 'register' for a particular event which you no longer care about, this event will tell the poco to stop sending them to you. (If you haven't, it just ignores you. No big deal). .ie n .IP """connect""" 4 .el .IP "\f(CWconnect\fR" 4 .IX Item "connect" Initiates a connection to the given server. Takes a number of parameters: .Sp .Vb 4 \& \*(Aqaddress\*(Aq, the remote address to connect to; \& \*(Aqport\*(Aq, the remote port to connect to; \& \*(Aqlocaladdr\*(Aq, specify that connections be made from a particular local address, optional; \& \*(Aqlocalport\*(Aq, specify that connections be made from a particular port, optional; .Ve .Sp \&\f(CW\*(C`address\*(C'\fR and \f(CW\*(C`port\*(C'\fR are optional if they have been already specified during \f(CW\*(C`spawn\*(C'\fR. .ie n .IP """shutdown""" 4 .el .IP "\f(CWshutdown\fR" 4 .IX Item "shutdown" Terminates the component. It will terminate any pending connects or connections. .ie n .IP """send_to_server""" 4 .el .IP "\f(CWsend_to_server\fR" 4 .IX Item "send_to_server" Send some output to the connected server. The first parameter is a string of text to send. This parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. .ie n .IP """disconnect""" 4 .el .IP "\f(CWdisconnect\fR" 4 .IX Item "disconnect" Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using \f(CW\*(C`send_to_server()\*(C'\fR and the server connection will be terminated. .ie n .IP """terminate""" 4 .el .IP "\f(CWterminate\fR" 4 .IX Item "terminate" Immediately disconnects a server connection. .SH "OUTPUT EVENTS" .IX Header "OUTPUT EVENTS" The component sends the following events to registered sessions. If you have changed the \f(CW\*(C`prefix\*(C'\fR option in \f(CW\*(C`spawn\*(C'\fR then substitute \f(CW\*(C`testc\*(C'\fR with the event prefix that you specified. .ie n .IP """testc_registered""" 4 .el .IP "\f(CWtestc_registered\fR" 4 .IX Item "testc_registered" This event is sent to a registering session. \s-1ARG0\s0 is the Test::POE::Client::TCP object. .ie n .IP """testc_socket_failed""" 4 .el .IP "\f(CWtestc_socket_failed\fR" 4 .IX Item "testc_socket_failed" Generated if the component cannot make a socket connection. \&\s-1ARG0\s0 contains the name of the operation that failed. \&\s-1ARG1\s0 and \s-1ARG2\s0 hold numeric and string values for $!, respectively. .ie n .IP """testc_connected""" 4 .el .IP "\f(CWtestc_connected\fR" 4 .IX Item "testc_connected" Generated whenever a connection is established. \s-1ARG0\s0 is the server's \s-1IP\s0 address, \s-1ARG1\s0 is the server's \s-1TCP\s0 port. \&\s-1ARG3\s0 is our \s-1IP\s0 address and \s-1ARG4\s0 is our socket port. .ie n .IP """testc_disconnected""" 4 .el .IP "\f(CWtestc_disconnected\fR" 4 .IX Item "testc_disconnected" Generated whenever we disconnect from the server. .ie n .IP """testc_input""" 4 .el .IP "\f(CWtestc_input\fR" 4 .IX Item "testc_input" Generated whenever the server sends us some traffic. \s-1ARG0\s0 is the data sent ( tokenised by whatever POE::Filter you specified ). .ie n .IP """testc_flushed""" 4 .el .IP "\f(CWtestc_flushed\fR" 4 .IX Item "testc_flushed" Generated whenever anything we send to the server is actually flushed down the 'line'. .SH "KUDOS" .IX Header "KUDOS" Contains code borrowed from POE::Component::Server::TCP by Rocco Caputo, Ann Barcomb and Jos Boumans. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\s-1POE\s0 .PP POE::Component::Server::TCP .SH "AUTHOR" .IX Header "AUTHOR" Chris Williams .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2018 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.