--- loncom/lonmaxima 2006/03/08 14:22:14 1.14 +++ loncom/lonmaxima 2006/03/08 15:58:03 1.15 @@ -3,7 +3,7 @@ # The LearningOnline Network with CAPA # Connect to MAXIMA CAS # -# $Id: lonmaxima,v 1.14 2006/03/08 14:22:14 www Exp $ +# $Id: lonmaxima,v 1.15 2006/03/08 15:58:03 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -37,8 +37,6 @@ use IO::Socket; use IO::File; use Symbol; use POSIX; -use Fcntl; -use Socket; use lib '/home/httpd/lib/perl/'; use LONCAPA::Configuration; @@ -131,18 +129,6 @@ sub catchexception { die("Signal abend"); } -# -------------------------------------------------- make a socket non-blocking -sub nonblock { - my $socket = shift; - my $flags; - if (ref($socket)) { - $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"; - } -} - # ---------------------------------------------------------------- Main program # -------------------------------- Set signal handlers to record abnormal exits @@ -277,9 +263,8 @@ sub make_new_child { Reuse => 1, Listen => 10 ) or die "making socket: $@\n"; - &nonblock($maximaserver); my $maximaselect=IO::Select->new($maximaserver); - sleep(2); + sleep(1); # open MAXIMA to talk to that port my ($cmd_in, $cmd_out, $cmd_err); @@ -291,10 +276,12 @@ sub make_new_child { # hopefully, MAXIMA calls us back &status("Waiting $maximapid on $maximaport"); my $maximaclient=$maximaserver->accept(); + $maximaclient->blocking(0); $maximaselect->add($maximaclient); - &nonblock($maximaclient); &status("$maximapid on $maximaport connected."); &logthis("Maxima $maximapid on port $maximaport connected."); + sleep(2); + &logthis('Initial reply: '.&maximareply($maximaselect)); # handle connections until we've reached $MAX_CLIENTS_PER_CHILD for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { @@ -302,7 +289,7 @@ sub make_new_child { my $client = $server->accept() or last; while (my $cmd=<$client>) { &status('Processing command by '.$maximapid.' on '.$maximaport); - print $maximaclient &unescape($cmd).";\n"; + &maximawrite($maximaselect,&unescape($cmd).";\n"); print $client &escape(&maximareply($maximaselect))."\n"; } } @@ -323,6 +310,7 @@ sub make_new_child { sub maximareply { my ($maximaselect)=@_; my $output=''; + foreach my $ready ($maximaselect->can_read(1)) { my $data = ''; my $rv = $ready->recv($data, POSIX::BUFSIZ, 0); @@ -331,3 +319,14 @@ sub maximareply { return $output; } +sub maximawrite { + my ($maximaselect,$cmd)=@_; + my $ready=($maximaselect->can_write(1)); + if (ref($ready)) { + print $ready $cmd; + } else { + &logthis("Cannot write: ".&maximareply($maximaselect)); + } +} + +