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

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 09 Feb 2004 10:57:37 -0000


foxr		Mon Feb  9 05:57:37 2004 EDT

  Modified files:              
    /loncom	LondConnection.pm 
  Log:
  Add a pile of new states at connection setup to:
  - Ask for the version of the remote lond.
  - Set the domain of the remote lond to whatever we were asked toconnect to.
  
  
  
Index: loncom/LondConnection.pm
diff -u loncom/LondConnection.pm:1.23 loncom/LondConnection.pm:1.24
--- loncom/LondConnection.pm:1.23	Tue Jan  6 04:35:22 2004
+++ loncom/LondConnection.pm	Mon Feb  9 05:57:37 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.23 2004/01/06 09:35:22 foxr Exp $
+# $Id: LondConnection.pm,v 1.24 2004/02/09 10:57:37 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -199,20 +199,21 @@
     Debug(5, "Connecting to ".$DnsName);
     # Now create the object...
     my $self     = { Host               => $DnsName,
-		     LoncapaHim         => $Hostname,
-	             Port               => $Port,
-	             State              => "Initialized",
-	             TransactionRequest => "",
-	             TransactionReply   => "",
-	             InformReadable     => 0,
-	             InformWritable     => 0,
-		     TimeoutCallback    => undef,
-		     TransitionCallback => undef,
-	             Timeoutable        => 0,
-	             TimeoutValue       => 30,
-		     TimeoutRemaining   => 0,
-		     CipherKey          => "",
-		     Cipher             => undef};
+                     LoncapaHim         => $Hostname,
+                     Port               => $Port,
+                     State              => "Initialized",
+                     TransactionRequest => "",
+                     TransactionReply   => "",
+                     InformReadable     => 0,
+                     InformWritable     => 0,
+                     TimeoutCallback    => undef,
+                     TransitionCallback => undef,
+                     Timeoutable        => 0,
+                     TimeoutValue       => 30,
+                     TimeoutRemaining   => 0,
+                     CipherKey          => "",
+                     LondVersion        => "Unknown",
+                     Cipher             => undef};
     bless($self, $class);
     unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host},
 					       PeerPort => $self->{Port},
@@ -316,17 +317,36 @@
 	    $self->Transition("ChallengeReceived");
 	    $self->{TimeoutRemaining}   = $self->{TimeoutValue};
 	    return 0;
-	} elsif ($self->{State} eq "ChallengeReplied") { # should be ok.
-	    if($self->{TransactionReply} != "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 "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;
 	} elsif ($self->{State}  eq "ReceivingKey") {
 	    my $buildkey = $self->{TransactionReply};
 	    my $key = $self->{LoncapaHim}.$perlvar{'lonHostID'};
@@ -408,35 +428,39 @@
 	($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 "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;
-    }
+      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;
+   }
 	
 }
 =pod
@@ -856,7 +880,17 @@
     my $hostref = \%HostsTab;
     return ($hostref);
 }
-
+#
+#   Get the version of our peer.  Note that this is only well
+#   defined if the state machine has hit the idle state at least
+#   once (well actually if it has transitioned out of 
+#   ReadingVersionString   The member data LondVersion is returned.
+#
+sub PeerVersion {
+   my $self = shift;
+   
+   return $self->{LondVersion};
+}
 
 1;