| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Exercise Server::TCP and later, when it's available, Client::TCP. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw(./mylib ../mylib); | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | unless (-f "run_network_tests") { | ||
| 11 | print "1..0 # Skip Network access (and permission) required to run this test\n"; | ||
| 12 | CORE::exit(); | ||
| 13 | } | ||
| 14 | if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 15 | print "1..0 # Skip Windows sockets aren't as concurrent as those on Unix\n"; | ||
| 16 | CORE::exit(); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | my $NUM_CLIENTS; | ||
| 21 | BEGIN { $NUM_CLIENTS = 9 } # rt.cpan.org 32034 | ||
| 22 | |||
| 23 | use Test::More tests => $NUM_CLIENTS * 2; | ||
| 24 | |||
| 25 | diag( "You might see a 'disconnect' error during this test." ); | ||
| 26 | diag( "It may be ignored." ); | ||
| 27 | |||
| 28 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 29 | sub POE::Kernel::TRACE_DEFAULT () { 0 } | ||
| 30 | |||
| 31 | use POE qw( Component::Server::TCP Wheel::ReadWrite Component::Client::TCP ); | ||
| 32 | |||
| 33 | #use POE::API::Peek; | ||
| 34 | |||
| 35 | my ($acceptor_port, $callback_port); | ||
| 36 | |||
| 37 | sub DEBUG () { 0 } | ||
| 38 | |||
| 39 | do_servers(); | ||
| 40 | do_clients(); | ||
| 41 | |||
| 42 | # Run the tests. | ||
| 43 | |||
| 44 | POE::Kernel->run(); | ||
| 45 | |||
| 46 | sub do_servers { | ||
| 47 | my($acceptorN, $callbackN)=(0,0); | ||
| 48 | |||
| 49 | my(%connected); | ||
| 50 | |||
| 51 | ###################################################################### | ||
| 52 | # Create a server. This one uses Acceptor to create a session of the | ||
| 53 | # program's devising. | ||
| 54 | POE::Component::Server::TCP->new( | ||
| 55 | Port => 0, | ||
| 56 | Address => '127.0.0.1', | ||
| 57 | Alias => 'acceptor_server', | ||
| 58 | Concurrency => 1, | ||
| 59 | Started => sub { | ||
| 60 | use Socket qw(sockaddr_in); | ||
| 61 | $acceptor_port = ( | ||
| 62 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 63 | )[0]; | ||
| 64 | }, | ||
| 65 | Acceptor => sub { | ||
| 66 | my ($socket, $peer_addr, $peer_port) = @_[ARG0..ARG2]; | ||
| 67 | |||
| 68 | if( $connected{acceptor} ) { | ||
| 69 | fail("acceptor server got simultaneous connections"); | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | pass("acceptor server : one connection open"); | ||
| 73 | } | ||
| 74 | $connected{acceptor} = 1; | ||
| 75 | |||
| 76 | POE::Session->create( | ||
| 77 | inline_states => { | ||
| 78 | _start => sub { | ||
| 79 | my $heap = $_[HEAP]; | ||
| 80 | $heap->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 81 | Handle => $socket, | ||
| 82 | InputEvent => 'got_input', | ||
| 83 | ErrorEvent => 'got_error', | ||
| 84 | FlushedEvent => 'got_flush', | ||
| 85 | ); | ||
| 86 | $heap->{tcp_server} = $_[SENDER]->ID; | ||
| 87 | DEBUG and warn("$$: acceptor server got client connection"); | ||
| 88 | }, | ||
| 89 | _stop => sub { | ||
| 90 | DEBUG and warn("$$: acceptor server stopped the client session"); | ||
| 91 | $connected{acceptor} = 0; | ||
| 92 | }, | ||
| 93 | got_input => sub { | ||
| 94 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 95 | $acceptorN++; | ||
| 96 | DEBUG and warn( | ||
| 97 | "$$: acceptor server received input ($input) ", | ||
| 98 | "acceptorN=$acceptorN" | ||
| 99 | ); | ||
| 100 | $heap->{wheel}->put("echo: $input #$acceptorN"); | ||
| 101 | if($input eq "quit") { | ||
| 102 | DEBUG and warn("$$: accept_server quit"); | ||
| 103 | $heap->{shutdown} = 1; | ||
| 104 | $_[KERNEL]->post( $heap->{tcp_server} => 'shutdown' ); | ||
| 105 | } | ||
| 106 | }, | ||
| 107 | got_error => sub { | ||
| 108 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 109 | if($operation eq 'read' and $errnum==0) { | ||
| 110 | DEBUG and warn("$$: acceptor server disconnect error"); | ||
| 111 | $heap->{shutdown} = 1; | ||
| 112 | $_[KERNEL]->post( $heap->{tcp_server} => 'disconnected' ); | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | warn( | ||
| 116 | "$$: acceptor server got $operation error $errnum: $errstr\n" | ||
| 117 | ); | ||
| 118 | } | ||
| 119 | delete $heap->{wheel}; | ||
| 120 | }, | ||
| 121 | got_flush => sub { | ||
| 122 | my $heap = $_[HEAP]; | ||
| 123 | DEBUG and warn("$$: acceptor server flushed output"); | ||
| 124 | if($heap->{shutdown}) { | ||
| 125 | delete $heap->{wheel}; | ||
| 126 | DEBUG and warn "$$: acceptor server disconnected"; | ||
| 127 | $_[KERNEL]->post( $heap->{tcp_server} => 'disconnected' ); | ||
| 128 | } | ||
| 129 | }, | ||
| 130 | }, | ||
| 131 | ); | ||
| 132 | }, | ||
| 133 | ); | ||
| 134 | |||
| 135 | |||
| 136 | ###################################################################### | ||
| 137 | # Create a server. This one uses ClientXyz to process clients instead | ||
| 138 | # of a user-defined session. | ||
| 139 | POE::Component::Server::TCP->new( | ||
| 140 | Port => 0, | ||
| 141 | Address => '127.0.0.1', | ||
| 142 | Alias => 'callback_server', | ||
| 143 | Started => sub { | ||
| 144 | use Socket qw(sockaddr_in); | ||
| 145 | $callback_port = ( | ||
| 146 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 147 | )[0]; | ||
| 148 | }, | ||
| 149 | Concurrency => 4, | ||
| 150 | # ClientShutdownOnError => 0, | ||
| 151 | |||
| 152 | ClientInput => sub { | ||
| 153 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 154 | $callbackN++; | ||
| 155 | DEBUG and warn( | ||
| 156 | "$$: callback server received input ($input) callbackN=$callbackN" | ||
| 157 | ); | ||
| 158 | if($input eq "quit") { | ||
| 159 | DEBUG and warn("$$: callback_server quit"); | ||
| 160 | $_[KERNEL]->post( callback_server => 'shutdown' ); | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | $heap->{client}->put("echo: $input #$callbackN"); | ||
| 164 | } | ||
| 165 | }, | ||
| 166 | ClientError => sub { | ||
| 167 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 168 | unless( $operation eq 'read' and $errnum == 0 ) { | ||
| 169 | warn "$$: callback server got $operation error $errnum: $errstr\n"; | ||
| 170 | } | ||
| 171 | $_[KERNEL]->yield('shutdown'); | ||
| 172 | }, | ||
| 173 | ClientFlushed => sub { | ||
| 174 | DEBUG and warn("$$: callback server flushed output"); | ||
| 175 | }, | ||
| 176 | ClientConnected => sub { | ||
| 177 | $connected{callback} ++; | ||
| 178 | if( $connected{callback} > 4 ) { | ||
| 179 | fail( | ||
| 180 | "callback server got $connected{callback} simultaneous connections" | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | pass("callback server : $connected{callback} connections open"); | ||
| 185 | } | ||
| 186 | |||
| 187 | DEBUG and | ||
| 188 | warn("$$: callback server got client connection"); | ||
| 189 | }, | ||
| 190 | ClientDisconnected => sub { | ||
| 191 | DEBUG and | ||
| 192 | warn("$$: callback server got client disconnected"); | ||
| 193 | $connected{callback} --; | ||
| 194 | }, | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub do_clients { | ||
| 199 | foreach my $N (1..$NUM_CLIENTS) { | ||
| 200 | DEBUG and warn "$$: SPAWN\n"; | ||
| 201 | two_clients($N); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | sub two_clients { | ||
| 206 | my($N) = @_; | ||
| 207 | |||
| 208 | my $parent=0; | ||
| 209 | |||
| 210 | ###################################################################### | ||
| 211 | # A client to connect to acceptor_server. | ||
| 212 | POE::Component::Client::TCP->new( | ||
| 213 | RemoteAddress => '127.0.0.1', | ||
| 214 | RemotePort => $acceptor_port, | ||
| 215 | Alias => "acceptor client $N", | ||
| 216 | |||
| 217 | Connected => sub { | ||
| 218 | DEBUG and warn("$$: acceptor client $N connected"); | ||
| 219 | $_[HEAP]->{server}->put( "hello $N" ); | ||
| 220 | }, | ||
| 221 | |||
| 222 | ConnectError => sub { | ||
| 223 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 224 | warn "$$: acceptor client $N got $operation error $errnum: $errstr\n"; | ||
| 225 | }, | ||
| 226 | |||
| 227 | Disconnected => sub { | ||
| 228 | DEBUG and warn("$$: acceptor client $N disconnected"); | ||
| 229 | }, | ||
| 230 | |||
| 231 | ServerInput => sub { | ||
| 232 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 233 | DEBUG and warn("$$: acceptor client $N got input ($input)"); | ||
| 234 | if( $input =~ /#$NUM_CLIENTS$/ ) { | ||
| 235 | $_[HEAP]->{server}->put( 'quit' ); | ||
| 236 | } | ||
| 237 | |||
| 238 | $_[KERNEL]->yield('shutdown'); | ||
| 239 | }, | ||
| 240 | |||
| 241 | ServerError => sub { | ||
| 242 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 243 | ok( | ||
| 244 | ($operation eq "read") && ($errnum == 0), | ||
| 245 | "acceptor client $N got read error 0 (EOF)" | ||
| 246 | ); | ||
| 247 | }, | ||
| 248 | |||
| 249 | ServerFlushed => sub { | ||
| 250 | DEBUG and warn("$$: acceptor client $N flushed output"); | ||
| 251 | }, | ||
| 252 | ); | ||
| 253 | |||
| 254 | ###################################################################### | ||
| 255 | # A client to connect to callback_server. | ||
| 256 | |||
| 257 | POE::Component::Client::TCP->new( | ||
| 258 | RemoteAddress => '127.0.0.1', | ||
| 259 | RemotePort => $callback_port, | ||
| 260 | Alias => "callback client $N", | ||
| 261 | |||
| 262 | Connected => sub { | ||
| 263 | DEBUG and warn("$$: callback client $N connected"); | ||
| 264 | $_[HEAP]->{server}->put( "hello $N" ); | ||
| 265 | }, | ||
| 266 | |||
| 267 | ConnectError => sub { | ||
| 268 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 269 | warn "$$: callback client $N got $operation error $errnum: $errstr\n"; | ||
| 270 | }, | ||
| 271 | |||
| 272 | Disconnected => sub { | ||
| 273 | DEBUG and warn("$$: callback client $N disconnected"); | ||
| 274 | }, | ||
| 275 | |||
| 276 | ServerInput => sub { | ||
| 277 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 278 | DEBUG and warn("$$: callback client $N got input ($input)"); | ||
| 279 | if( $input =~ /#$NUM_CLIENTS$/ ) { | ||
| 280 | $_[HEAP]->{server}->put( 'quit' ); | ||
| 281 | } | ||
| 282 | $_[KERNEL]->yield('shutdown'); | ||
| 283 | }, | ||
| 284 | |||
| 285 | ServerError => sub { | ||
| 286 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 287 | ok( | ||
| 288 | ($operation eq "read") && ($errnum == 0), | ||
| 289 | "callback client $N got $operation error $errnum (EOF)" | ||
| 290 | ); | ||
| 291 | }, | ||
| 292 | |||
| 293 | ServerFlushed => sub { | ||
| 294 | DEBUG and warn("$$: callback client $N flushed output"); | ||
| 295 | }, | ||
| 296 | ); | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; |