[LON-CAPA-cvs] cvs: loncom / LondConnection.pm

foxr lon-capa-cvs@mail.lon-capa.org
Tue, 02 Mar 2004 11:14:38 -0000


foxr		Tue Mar  2 06:14:38 2004 EDT

  Modified files:              
    /loncom	LondConnection.pm 
  Log:
  - Return an error condition on  Readable and Writable if socket is not defined...
  - Do unto recv with eval as was done unto send in Writable.
  
  
Index: loncom/LondConnection.pm
diff -u loncom/LondConnection.pm:1.26 loncom/LondConnection.pm:1.27
--- loncom/LondConnection.pm:1.26	Fri Feb 27 13:32:21 2004
+++ loncom/LondConnection.pm	Tue Mar  2 06:14:38 2004
@@ -1,7 +1,7 @@
 #   This module defines and implements a class that represents
 #   a connection to a lond daemon.
 #
-# $Id: LondConnection.pm,v 1.26 2004/02/27 18:32:21 albertel Exp $
+# $Id: LondConnection.pm,v 1.27 2004/03/02 11:14:38 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -216,10 +216,10 @@
                      Cipher             => undef};
     bless($self, $class);
     unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host},
-						    PeerPort => $self->{Port},
-						    Type     => SOCK_STREAM,
-						    Proto    => "tcp",
-						    Timeout  => 3)) {
+					       PeerPort => $self->{Port},
+					       Type     => SOCK_STREAM,
+					       Proto    => "tcp",
+					       Timeout  => 3)) {
 	return undef;		# Inidicates the socket could not be made.
     }
     #
@@ -279,7 +279,15 @@
     my $self    = shift;
     my $socket  = $self->{Socket};
     my $data    = '';
-    my $rv      = $socket->recv($data, POSIX::BUFSIZ,  0);
+    my $rv;
+    if ($socket) {
+	eval {
+	    $rv = $socket->recv($data, POSIX::BUFSIZ, 0);
+	}
+    } else {
+	$self->Transition("Disconnected");
+	return -1;
+    }
     my $errno   = $! + 0;	             # Force numeric context.
 
     unless (defined($rv) && length $data) {# Read failed,
@@ -309,7 +317,7 @@
 		$socket->close();
 		return -1;
 	    }
-	    
+
 	    &Debug(8," Transition out of Initialized");
 	    $self->{TransactionRequest} = $self->{TransactionReply};
 	    $self->{InformWritable}     = 1;
@@ -317,36 +325,36 @@
 	    $self->Transition("ChallengeReceived");
 	    $self->{TimeoutRemaining}   = $self->{TimeoutValue};
 	    return 0;
-	}  elsif ($self->{State} eq "ChallengeReplied") {
-	    if($self->{TransactionReply} ne "ok\n") {
-		$self->Transition("Disconnected");
-		$socket->close();
-		return -1;
-	    }
-	    $self->Transition("RequestingVersion");
-	    $self->{InformReadable}   = 0;
-	    $self->{InformWritable}   = 1;
-	    $self->{TransactionRequest} = "version\n";
-	    return 0;
-	} elsif ($self->{State} eq "ReadingVersionString") {
-	    $self->{LondVersion}       = chomp($self->{TransactionReply});
-	    $self->Transition("SetHost");
-	    $self->{InformReadable}    = 0;
-	    $self->{InformWritable}    = 1;
-	    my $peer = $self->{LoncapaHim};
-	    $self->{TransactionRequest}= "sethost:$peer\n";
-	    return 0;
+   }  elsif ($self->{State} eq "ChallengeReplied") {
+      if($self->{TransactionReply} ne "ok\n") {
+         $self->Transition("Disconnected");
+         $socket->close();
+         return -1;
+      }
+      $self->Transition("RequestingVersion");
+      $self->{InformReadable}   = 0;
+      $self->{InformWritable}   = 1;
+      $self->{TransactionRequest} = "version\n";
+      return 0;
+   } elsif ($self->{State} eq "ReadingVersionString") {
+      $self->{LondVersion}       = chomp($self->{TransactionReply});
+      $self->Transition("SetHost");
+      $self->{InformReadable}    = 0;
+      $self->{InformWritable}    = 1;
+      my $peer = $self->{LoncapaHim};
+      $self->{TransactionRequest}= "sethost:$peer\n";
+      return 0;
 	} elsif ($self->{State} eq "HostSet") { # should be ok.
-	    if($self->{TransactionReply} ne "ok\n") {
-		$self->Transition("Disconnected");
-		$socket->close();
-		return -1;
-	    }
-	    $self->Transition("RequestingKey");
-	    $self->{InformReadable}  = 0;
-	    $self->{InformWritable}  = 1;
-	    $self->{TransactionRequest} = "ekey\n";
-	    return 0;
+      if($self->{TransactionReply} ne "ok\n") {
+         $self->Transition("Disconnected");
+         $socket->close();
+         return -1;
+      }
+	   $self->Transition("RequestingKey");
+	   $self->{InformReadable}  = 0;
+	   $self->{InformWritable}  = 1;
+	   $self->{TransactionRequest} = "ekey\n";
+	   return 0;
 	} elsif ($self->{State}  eq "ReceivingKey") {
 	    my $buildkey = $self->{TransactionReply};
 	    my $key = $self->{LoncapaHim}.$perlvar{'lonHostID'};
@@ -395,7 +403,7 @@
     }
 
     return 0;
-
+    
 }
 
 
@@ -410,7 +418,6 @@
 Returns  0 if successful, or -1 if not.
 
 =cut
-
 sub Writable {
     my $self     = shift;		# Get reference to the object.
     my $socket   = $self->{Socket};
@@ -419,6 +426,12 @@
 	eval {
 	    $nwritten = $socket->send($self->{TransactionRequest}, 0);
 	}
+    } else {
+	# For whatever reason, there's no longer a socket left.
+
+
+	$self->Transition("Disconnected");
+	return -1;
     }
     my $errno    = $! + 0;
     unless (defined $nwritten) {
@@ -434,41 +447,41 @@
 	($errno == POSIX::EINTR)          ||
 	($errno ==  0)) {
 	substr($self->{TransactionRequest}, 0, $nwritten) = ""; # rmv written part
-	if(length $self->{TransactionRequest} == 0) {
-	    $self->{InformWritable} = 0;
-	    $self->{InformReadable} = 1;
-	    $self->{TransactionReply} = '';
-	    #
-	    # Figure out the next state:
-	    #
-	    if($self->{State} eq "Connected") {
-		$self->Transition("Initialized");
-	    } elsif($self->{State} eq "ChallengeReceived") {
-		$self->Transition("ChallengeReplied");
-	    } elsif($self->{State} eq "RequestingVersion") {
-		$self->Transition("ReadingVersionString");
-	    } elsif ($self->{State} eq "SetHost") {
-		$self->Transition("HostSet");
-	    } elsif($self->{State} eq "RequestingKey") {
-		$self->Transition("ReceivingKey");
+      if(length $self->{TransactionRequest} == 0) {
+         $self->{InformWritable} = 0;
+         $self->{InformReadable} = 1;
+         $self->{TransactionReply} = '';
+         #
+         # Figure out the next state:
+         #
+         if($self->{State} eq "Connected") {
+            $self->Transition("Initialized");
+         } elsif($self->{State} eq "ChallengeReceived") {
+            $self->Transition("ChallengeReplied");
+         } elsif($self->{State} eq "RequestingVersion") {
+            $self->Transition("ReadingVersionString");
+         } elsif ($self->{State} eq "SetHost") {
+            $self->Transition("HostSet");
+         } elsif($self->{State} eq "RequestingKey") {
+            $self->Transition("ReceivingKey");
 #            $self->{InformWritable} = 0;
 #            $self->{InformReadable} = 1;
 #            $self->{TransactionReply} = '';
-	    } elsif ($self->{State} eq "SendingRequest") {
-		$self->Transition("ReceivingReply");
-		$self->{TimeoutRemaining} = $self->{TimeoutValue};
-	    } elsif ($self->{State} eq "Disconnected") {
-		return -1;
-	    }
-	    return 0;
-	}
-    } else {			# The write failed (e.g. partner disconnected).
-	$self->Transition("Disconnected");
-	$socket->close();
-	return -1;
-    }
+         } elsif ($self->{State} eq "SendingRequest") {
+            $self->Transition("ReceivingReply");
+            $self->{TimeoutRemaining} = $self->{TimeoutValue};
+         } elsif ($self->{State} eq "Disconnected") {
+            return -1;
+         }
+         return 0;
+      }
+   } else {			# The write failed (e.g. partner disconnected).
+      $self->Transition("Disconnected");
+      $socket->close();
+      return -1;
+   }
+	
 }
-
 =pod
 
 =head2 Tick