rapier1 has asked for the wisdom of the Perl Monks concerning the following question:
I've included by code so you can see exactly what I am trying to do. Perhaps (in fact even likely) I am making some basic mistake. Any input would be appreciated.
The log data indicates that I am, in fact, cycling through the clients as expected. I'm just lost at this point as to how to proceed.
#!/usr/bin/perl -w # nonforker - server who multiplexes without forking use POSIX; use IO::Socket; use IO::Select; use Socket; use Fcntl; use Tie::RefHash; use Carp; BEGIN { $coral_dir = "/usr/local/Coral"; } # This will be edited durin +g build. use lib "$coral_dir/lib"; #coral global libdir# use lib "../../lib"; #coral local libdir# use CRL; use IO::Scalar; $port = 9001; # change this at will # Listen to port. $server = IO::Socket::INET->new(LocalPort => $port, Listen => 10 ) or die "Can't make server socket: $@\n"; %ready = (); tie %ready, 'Tie::RefHash'; nonblock($server); $select = IO::Select->new($server); print "Server started\n"; open LOG, ">./errlog" or die "cannot open errlog $!\n"; print "Starting CoralReef\n"; #Coral::quick_start(0, 0, Coral::API_CELL); Coral::quick_start(); print "Quick start finished\n"; # Main loop: check reads/accepts, check writes, check ready to process while (1) { my $client; my $rv; my $data; # check for new information on the connections we have # anything to read or accept? foreach $client ($select->can_read(0)) { if ($client == $server) { # accept a new connection $client = $server->accept(); $select->add($client); nonblock($client); recv($client, $duration, 10, 0); $ready{$client} = time + $duration; $out_name = time; print LOG "MAIN: $client\n"; &swap_stdout($client); $writer = Coral::write_open("-", undef, "none"); &revert_stdout; if (!defined($writer)) { Coral::puts("Error, could not open output file!"); exit(1); } else { Coral::puts("Writing to file: $out_name"); } Coral::write_init_all($writer); $endtime = time + $duration; Coral::puts("$duration, $endtime, $out_name\n"); } } $iface = Coral::read_cell_all(\$binfo, \$cell); foreach $client (keys %ready) { handle($client); } } # handle($socket) deals with all pending requests for $client sub handle { # requests are in $ready{$client} # send output to $outbuffer{$client} my $client = shift; my $request; print LOG "HANDLE: $client\n"; &swap_stdout($client); if (Coral::write_cell($writer, $iface, $cell) < 0) { Coral::puts("Could not write cell! exiting!"); exit(2); } &revert_stdout; if ($ready{$client} <= time) { &swap_stdout($client); Coral::write_close($writers{$writer}[2]); &revert_stdout; delete $ready{$client}; $select->remove($client); $client->close; next; } } # nonblock($socket) puts socket into nonblocking mode sub nonblock { my $socket = shift; my $flags; $flags = fcntl($socket, F_GETFL, 0) or die "Can't get flags for socket: $!\n"; fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or die "Can't make socket nonblocking: $!\n"; } #bind STDOUT to the client socket and unbuffer sub swap_stdout { my $client = shift; my $fd = fileno($client); print LOG "SWAP: $client == $fd\n"; open(STDSAVE, ">&STDOUT"); open(STDOUT, ">&=$fd"); select(STDOUT); $|=1; } # revert to the default STDOUT sub revert_stdout { close STDOUT; open(STDOUT, ">&STDSAVE"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sending STDOUT to Multiple Sockets
by abstracts (Hermit) on Aug 18, 2001 at 04:06 UTC | |
|
Re: Sending STDOUT to Multiple Sockets
by suaveant (Parson) on Aug 17, 2001 at 23:23 UTC | |
by rapier1 (Novice) on Aug 17, 2001 at 23:27 UTC | |
by rapier1 (Novice) on Aug 18, 2001 at 00:00 UTC | |
by tilly (Archbishop) on Aug 18, 2001 at 23:03 UTC |