1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
#!/usr/bin/perl -w
# This program creates a server session and an infinitude of clients
# that connect to it, all in the same process. It's mainly used to
# test for memory leaks, but it's also something of a benchmark.
# It is possible to split this program into two separate processes:
# Change $server_addr to something appropriate.
# Make a second copy of this program.
# In the "server" copy, comment out the call to &pool_create();
# In the "client" copy, comment out th ecall to &server_create();
use strict;
use lib '../lib';
use Socket;
#sub POE::Kernel::ASSERT_DEFAULT () { 1 }
use POE qw(Wheel::ListenAccept Wheel::ReadWrite Driver::SysRW Filter::Line
Wheel::SocketFactory
);
sub MAX_SIMULTANEOUS_CLIENTS () { 5 }
# make 1 to enable output
sub DEBUG () { 0 }
# address and port the server binds to
my $server_addr = '127.0.0.1';
my $server_port = 32100;
###############################################################################
# This is a single client session. It uses two separator wheels: a
# SocketFactory to establish a connection, and a ReadWrite to process
# data once the connection is made
#------------------------------------------------------------------------------
# This is regular Perl sub that helps create new clients. It's not an
# event handler.
sub client_create {
my $serial_number = shift;
# create the session
POE::Session->create(
inline_states => {
_start => \&client_start,
_stop => \&client_stop,
receive => \&client_receive,
error => \&client_error,
connected => \&client_connected,
signals => \&client_signals,
_parent => sub {},
},
# ARG0
args => [ $serial_number ]
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _start event, and create a non-blocking client
# socket.
sub client_start {
my ($kernel, $heap, $serial) = @_[KERNEL, HEAP, ARG0];
DEBUG && print "Client $serial is starting.\n";
# remember this client's serial number
$heap->{'serial'} = $serial;
# watch for SIGINT
$kernel->sig('INT', 'signals');
# create a socket factory
$heap->{'wheel'} = POE::Wheel::SocketFactory->new(
RemoteAddress => $server_addr, # connecting to address $server_addr
RemotePort => $server_port, # connecting to port $server_port
SuccessEvent => 'connected', # generating this event when connected
FailureEvent => 'error', # generating this event upon an error
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _stop event. This normally would clean up the
# session, but this program doesn't keep anything in the heap that
# needs to be cleaned up.
sub client_stop {
my $heap = $_[HEAP];
DEBUG && print "Client $heap->{'serial'} has stopped.\n";
}
#------------------------------------------------------------------------------
# This event handler/state is invoked when a connection has been
# established successfully. It replaces the SocketFactory wheel with
# a ReadWrite wheel. The new wheel generates different events.
sub client_connected {
my ($heap, $socket) = @_[HEAP, ARG0];
die "possible filehandle leak" if fileno($socket) > 63;
DEBUG && print "Client $heap->{'serial'} is connected.\n";
# switch to read/write behavior
$heap->{'wheel'} = POE::Wheel::ReadWrite->new(
Handle => $socket, # read and write on this socket
Driver => POE::Driver::SysRW->new, # using sysread and syswrite
Filter => POE::Filter::Line->new, # and parsing I/O as lines
InputEvent => 'receive', # generating this event on input
ErrorEvent => 'error', # generating this event on error
);
shutdown($socket, 1);
}
#------------------------------------------------------------------------------
# This state is invoked by the ReadWrite wheel to process complete
# chunks of input.
sub client_receive {
my ($heap, $line) = @_[HEAP, ARG0];
DEBUG && print "Client $heap->{'serial'} received: $line\n";
}
#------------------------------------------------------------------------------
# This state is invoked by both the SocketFactory and the ReadWrite
# wheels when an error occurs.
sub client_error {
my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
if (DEBUG) {
if ($errnum) {
print( "Client $heap->{'serial'} encountered ",
"$operation error $errnum: $errstr\n"
);
}
else {
print "Client $heap->{'serial'} the server closed the connection.\n";
}
}
# removing the wheel stops the session
delete $heap->{'wheel'};
}
#------------------------------------------------------------------------------
# Catch and log signals. Never handle them.
sub client_signals {
my ($heap, $signal_name) = @_[HEAP, ARG0];
DEBUG && print "Client $heap->{'serial'} caught SIG$signal_name\n";
# doesn't handle SIGINT, so it can stop
return 0;
}
###############################################################################
# This is a client pool session. It ensures that at least five
# clients are interacting with the server at any given time.
# Actually, there are brief periods where only four clients are
# connected.
#------------------------------------------------------------------------------
# This is a regular Perl sub that helps create new client pools. It's
# not an event handler.
sub pool_create {
# create the server
POE::Session->create(
inline_states => {
_start => \&pool_start,
_stop => \&pool_stop,
signals => \&pool_signals,
_child => \&pool_child,
_parent => sub {},
},
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _start event. Initialize benchmark
# accumulators, and start the first five clients.
sub pool_start {
my ($kernel, $heap) = @_[KERNEL, HEAP];
DEBUG && print "Pool starting.\n";
# watch for SIGINT
$kernel->sig('INT', 'signals');
# keep track of children
$heap->{'children'} = 0;
$heap->{'client serial'} = 0;
$heap->{'state'} = 'running';
# benchmark accumulators
$heap->{'bench start'} = time();
$heap->{'bench count'} = 0;
# Start five clients. NOTE: This would not work if clients used
# IO::Socket to connect to the server, because IO::Socket's connect
# blocks. It would wait for the server to accept a connection
# before continuing, which would never happen since this loop is
# holding up the event queue. The program can only get away with
# this loop because SocketFactory connections do not block.
for (my $i = 0; $i < MAX_SIMULTANEOUS_CLIENTS; $i++) {
&client_create(++$heap->{'client serial'});
}
}
#------------------------------------------------------------------------------
# Accept POE's standard stop event. Also stop the server.
sub pool_stop {
my $kernel = $_[KERNEL];
# send SIGQUIT to the server
$kernel->signal('server', 'QUIT');
DEBUG && print "Pool has stopped.\n";
}
#------------------------------------------------------------------------------
# Catch and log signals, but never handle them.
sub pool_signals {
my ($heap, $signal_name) = @_[HEAP, ARG0];
DEBUG && print "Pool caught SIG$signal_name\n";
# doesn't handle SIGINT, so it can stop
return 0;
}
#------------------------------------------------------------------------------
# Keep track of child sessions, starting new ones to replace old ones
# that are being lost. If debugging, and a time limit has been
# reached, stop creating new clients.
my %english = ( create => 'created', lose => 'lost', gain => 'gained' );
sub pool_child {
my ($heap, $direction, $child) = @_[HEAP, ARG0, ARG1];
# lost a client
if ($direction eq 'lose') {
$heap->{'children'}--;
# create a new one if still running
if ($heap->{'state'} eq 'running') {
&client_create(++$heap->{'client serial'});
}
}
# gained a client; keep track of it
else {
$heap->{'children'}++;
$heap->{'bench count'}++;
}
DEBUG && print( "Pool $english{$direction} a child session ",
"(now has $heap->{'children'}).\n"
);
# track clients/second for benchmark
my $elapsed = time() - $heap->{'bench start'};
if ($elapsed >= 10) {
print "bench: ", $heap->{'bench count'}, ' / ', $elapsed, ' = ',
$heap->{'bench count'} / $elapsed, "\n";
$heap->{'bench count'} = 0;
$heap->{'bench start'} = time();
# limit run to 60 seconds if debugging
if (DEBUG && (time() - $^T >= 60.0)) {
$heap->{'state'} = 'quitting';
}
}
}
###############################################################################
# This is a single server session. It is spawned by the daytime
# server to handle incoming connections.
#------------------------------------------------------------------------------
# This is a regular Perl sub that helps create new sessions. It's not
# an event handler.
sub session_create {
my ($handle, $peer_host, $peer_port) = @_;
# create the session
POE::Session->create(
inline_states => {
_start => \&session_start,
_stop => \&session_stop,
receive => \&session_receive,
flushed => \&session_flushed,
error => \&session_error,
signals => \&session_signals,
_child => sub {},
_parent => sub {},
},
# ARG0, ARG1, ARG2
args => [ $handle, $peer_host, $peer_port ]
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _start event, and start transacting with the
# client.
sub session_start {
my ($kernel, $heap, $handle, $peer_host, $peer_port) =
@_[KERNEL, HEAP, ARG0, ARG1, ARG2];
# make the address printable
$peer_host = inet_ntoa($peer_host);
DEBUG && print "Session with $peer_host $peer_port is starting.\n";
# watch for SIGINT
$kernel->sig('INT', 'signals');
# record the client info for later
$heap->{'host'} = $peer_host;
$heap->{'port'} = $peer_port;
# start reading and writing
$heap->{'wheel'} = POE::Wheel::ReadWrite->new(
Handle => $handle, # on the client's socket
Driver => POE::Driver::SysRW->new, # using sysread and syswrite
Filter => POE::Filter::Line->new, # and parsing I/O as lines
InputEvent => 'receive', # generating this event on input
ErrorEvent => 'error', # generating this event on error
FlushedEvent => 'flushed', # generating this event on flush
);
# give the client the time of day
$heap->{'wheel'}->put(
"Hi, $peer_host $peer_port! The time is: " . gmtime() . " GMT"
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _stop event. This normally would clean up the
# session, but this program doesn't keep anything in the heap that
# needs to be cleaned up.
sub session_stop {
my $heap = $_[HEAP];
DEBUG && print "Session with $heap->{'host'} $heap->{'port'} has stopped.\n";
}
#------------------------------------------------------------------------------
# This state is invoked by the ReadWrite wheel whenever a complete
# request has been received.
sub session_receive {
my ($heap, $line) = @_[HEAP, ARG0];
DEBUG && print "Received from $heap->{'host'} $heap->{'port'}: $line\n";
}
#------------------------------------------------------------------------------
# This state is invoked when the ReadWrite wheel encounters an error.
sub session_error {
my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
DEBUG && print( "Session with $heap->{'host'} $heap->{'port'} ",
"encountered $operation error $errnum: $errstr\n"
);
delete $heap->{'wheel'};
}
#------------------------------------------------------------------------------
# This state is invoked when the ReadWrite wheel's output buffer
# becomes empty. For a daytime server session, a flushed buffer means
# it's okay to close the connection.
sub session_flushed {
my $heap = $_[HEAP];
DEBUG && print "Output to $heap->{'host'} $heap->{'port'} has flushed.\n";
# removing the wheel stops the session
delete $heap->{'wheel'};
}
#------------------------------------------------------------------------------
# Catch and log signals, but never handle them.
sub session_signals {
my ($heap, $signal_name) = @_[HEAP, ARG0];
DEBUG && print( "Session with $heap->{'host'} $heap->{'port'} ",
"has received a SIG$signal_name\n"
);
# doesn't handle SIGINT, so it can stop
return 0;
}
###############################################################################
# This is a generic daytime server. Its only purpose is to listen on
# a socket, accept connections, and spawn daytime sessions to handle
# the connections.
#------------------------------------------------------------------------------
# This is a regular Perl sub that helps create new servers. It's not
# an event handler.
sub server_create {
# create the server
POE::Session->create(
inline_states => {
_start => \&server_start,
_stop => \&server_stop,
accept_success => \&server_accept,
accept_error => \&server_error,
signals => \&server_signals,
_child => sub {},
_parent => sub {},
}
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _start event. Create a non-blocking server.
sub server_start {
my ($kernel, $heap) = @_[KERNEL, HEAP];
DEBUG && print "Daytime server is starting.\n";
# set an alias so pool_stop can signal
$kernel->alias_set('server');
# watch for SIGINT and SIGQUIT
$kernel->sig('INT', 'signals');
$kernel->sig('QUIT', 'signals');
# create a socket factory
$heap->{'wheel'} = POE::Wheel::SocketFactory->new(
BindAddress => $server_addr, # bind the listener to this address
BindPort => $server_port, # bind the listener to this port
Reuse => 'yes', # and reuse the socket right away
SuccessEvent => 'accept_success', # generate this event for connections
FailureEvent => 'accept_error', # generate this event for errors
);
}
#------------------------------------------------------------------------------
# Accept POE's standard _stop event. This normally would clean up the
# session, but this program doesn't keep anything in the heap that
# needs to be cleaned up.
sub server_stop {
my $heap = $_[HEAP];
DEBUG && print "Daytime server has stopped.\n";
}
#------------------------------------------------------------------------------
# This state is invoked by the SocketFactory when an error occurs.
sub server_error {
my ($operation, $errnum, $errstr) = @_[ARG0, ARG1, ARG2];
DEBUG
&& print "Daytime server encountered $operation error $errnum: $errstr\n";
}
#------------------------------------------------------------------------------
# The SocketFactory invokes this state when a new client connection
# has been accepted. The parameters include the client socket,
# address and port.
sub server_accept {
my ($handle, $host, $port) = @_[ARG0, ARG1, ARG2];
# spawn a server session
die "possible filehandle leak" if fileno($handle) > 63;
&session_create($handle, $host, $port);
}
#------------------------------------------------------------------------------
# Catch and log signals, but never handle them.
sub server_signals {
my $signal_name = $_[ARG0];
DEBUG && print "Daytime server caught SIG$signal_name\n";
# doesn't handle SIGINT, so it can stop
return 0;
}
###############################################################################
# Start the daytime server and a pool of clients to transact with it.
&server_create();
&pool_create();
$poe_kernel->run();
exit;
|