Diff for /loncom/lonmaxima between versions 1.12 and 1.13

version 1.12, 2006/03/08 03:18:42 version 1.13, 2006/03/08 12:57:30
Line 37  use IO::Socket; Line 37  use IO::Socket;
 use IO::File;  use IO::File;
 use Symbol;  use Symbol;
 use POSIX;  use POSIX;
   use Fcntl;
   use Socket;
 use lib '/home/httpd/lib/perl/';  use lib '/home/httpd/lib/perl/';
 use LONCAPA::Configuration;  use LONCAPA::Configuration;
     
Line 57  my $lastlog;                           # Line 59  my $lastlog;                           #
 use vars qw($PREFORK $MAX_CLIENTS_PER_CHILD %children $children %usedmaximaports $status  use vars qw($PREFORK $MAX_CLIENTS_PER_CHILD %children $children %usedmaximaports $status
     $pidfile $port %perlvar $lastlog);      $pidfile $port %perlvar $lastlog);
     
 sub maximareply {  
     my ($cmd,$maximaclient,$maximapid) = @_;  
     my $reply='';  
     while (my $line=<$maximaclient>) {  
        $reply.=$line;  
     }  
     unless ($cmd=~/\;\n$/) { $cmd.=";\n"; }  
     print $maximaclient $cmd;  
     &status("Command sent to $maximapid");  
     while (my $line=<$maximaclient>) {  
        $reply.=$line;  
     }  
     &status("Command processed by $maximapid");  
     return $reply;  
 }  
    
 # ------------------------------------------------------------ Service routines   # ------------------------------------------------------------ Service routines 
 sub REAPER {                        # takes care of dead children   sub REAPER {                        # takes care of dead children 
                                     # and MAXIMA processes                                      # and MAXIMA processes
Line 144  sub catchexception { Line 130  sub catchexception {
     die("Signal abend");      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  # ---------------------------------------------------------------- Main program
 # -------------------------------- Set signal handlers to record abnormal exits  # -------------------------------- Set signal handlers to record abnormal exits
Line 275  sub make_new_child { Line 271  sub make_new_child {
                                               Reuse     => 1,                                                Reuse     => 1,
                                               Listen    => 10 )                                                Listen    => 10 )
                         or die "making socket: $@\n";                          or die "making socket: $@\n";
           &nonblock($maximaserver);
           my $maximaselect=IO::Select->new($maximaserver);
   
         # open MAXIMA to talk to that port          # open MAXIMA to talk to that port
         my ($cmd_in, $cmd_out, $cmd_err);          my ($cmd_in, $cmd_out, $cmd_err);
         my $maximapid = open3($cmd_in, $cmd_out, $cmd_err, "maxima -s $maximaport");          my $maximapid = open3($cmd_in, $cmd_out, $cmd_err, "maxima -s $maximaport");
         $children{$maximapid} = 1;          $children{$maximapid} = 1;
   
         my $prompt=<$cmd_out>;          my $prompt=<$cmd_out>;
         &logthis("Maxima $maximapid: $prompt");          &logthis("Maxima $maximapid: $prompt");
   
           # hopefully, MAXIMA calls us back
         &status("Waiting $maximapid on $maximaport");          &status("Waiting $maximapid on $maximaport");
         # Hopefully, MAXIMA calls us back  
         my $maximaclient=$maximaserver->accept();          my $maximaclient=$maximaserver->accept();
           $maximaselect->add($maximaclient);
           &nonblock($maximaclient);
         &status("$maximapid on $maximaport connected.");          &status("$maximapid on $maximaport connected.");
         &logthis("Maxima $maximapid on port $maximaport connected.");          &logthis("Maxima $maximapid on port $maximaport connected.");
           &logthis('Initial reply: '.&maximareply($maximaselect));
         # Absorb initial prompts               # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
         &logthis(&maximareply("0;\n",$maximaclient,$maximapid));          for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
                &status('Accepting connections for '.$maximapid.' on '.$maximaport);
         # Ready for action                my $client = $server->accept()     or last;
               while (my $cmd=<$client>) {
         &process_requests($server,$maximaclient,$maximapid);                  &status('Processing command by '.$maximapid.' on '.$maximaport);
                   print $maximaclient &unescape($cmd).";\n";
                   print $client &escape(&maximareply($maximaselect))."\n";
               }
           }
   
         # tidy up gracefully and finish          # tidy up gracefully and finish
   
Line 310  sub make_new_child { Line 313  sub make_new_child {
     }      }
 }  }
   
 sub process_requests {  sub maximareply {
     my ($server,$maximaclient,$maximapid) = @_;     my ($maximaselect)=@_;
     # handle connections until we've reached $MAX_CLIENTS_PER_CHILD     my $output='';
     for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {     foreach my $ready ($maximaselect->can_read(1)) {
  &status('Accepting connections for '.$maximapid.' on '.$maximaport);              my $data = '';
  my $client = $server->accept()     or last;         my $rv   = $ready->recv($data, POSIX::BUFSIZ, 0);
  while (my $cmd=<$client>) {         $output.=$data;
     &status('Processing command by '.$maximapid.' on '.$maximaport);     }
     print $client &escape((&maximareply(&unescape($cmd),$maximaclient,$maximapid))[0])."\n";     return $output;
  }  
     }      
 }  }
   

Removed from v.1.12  
changed lines
  Added in v.1.13


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>