--- loncom/LondConnection.pm 2004/02/09 10:57:37 1.24 +++ loncom/LondConnection.pm 2004/06/01 10:05:16 1.30 @@ -1,7 +1,7 @@ # This module defines and implements a class that represents # a connection to a lond daemon. # -# $Id: LondConnection.pm,v 1.24 2004/02/09 10:57:37 foxr Exp $ +# $Id: LondConnection.pm,v 1.30 2004/06/01 10:05:16 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -89,8 +89,8 @@ sub ReadConfig { # to build up the hosts table. # sub ReadForeignConfig { - my $MyHost = shift; - my $Filename = shift; + + my ($MyHost, $Filename) = @_; &Debug(4, "ReadForeignConfig $MyHost $Filename\n"); @@ -108,8 +108,9 @@ sub ReadForeignConfig { } sub Debug { - my $level = shift; - my $message = shift; + + my ($level, $message) = @_; + if ($level < $DebugLevel) { print($message."\n"); } @@ -143,8 +144,9 @@ old state. =cut sub Transition { - my $self = shift; - my $newstate = shift; + + my ($self, $newstate) = @_; + my $oldstate = $self->{State}; $self->{State} = $newstate; $self->{TimeoutRemaining} = $self->{TimeoutValue}; @@ -174,9 +176,8 @@ host the remote lond is on. This host is =cut sub new { - my $class = shift; # class name. - my $Hostname = shift; # Name of host to connect to. - my $Port = shift; # Port to connect + + my ($class, $Hostname, $Port) = @_; if (!$ConfigRead) { ReadConfig(); @@ -279,7 +280,15 @@ sub Readable { 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, @@ -300,7 +309,7 @@ sub Readable { &Debug(9,"Received from host: ".$data); $self->{TransactionReply} .= $data; - if($self->{TransactionReply} =~ /(.*\n)/) { + if($self->{TransactionReply} =~ m/\n$/) { &Debug(8,"Readable End of line detected"); if ($self->{State} eq "Initialized") { # We received the challenge: if($self->{TransactionReply} eq "refused\n") { # Remote doesn't have @@ -317,36 +326,36 @@ sub Readable { $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'}; @@ -413,7 +422,18 @@ Returns 0 if successful, or -1 if not. sub Writable { my $self = shift; # Get reference to the object. my $socket = $self->{Socket}; - my $nwritten = $socket->send($self->{TransactionRequest}, 0); + my $nwritten; + if ($socket) { + 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) { if($errno != POSIX::EINTR) { @@ -516,8 +536,8 @@ timout, and to request writability notif =cut sub InitiateTransaction { - my $self = shift; - my $data = shift; + + my ($self, $data) = @_; Debug(1, "initiating transaction: ".$data); if($self->{State} ne "Idle") { @@ -568,8 +588,9 @@ established callback or undef if there w =cut sub SetTimeoutCallback { - my $self = shift; - my $callback = shift; + + my ($self, $callback) = @_; + my $oldCallback = $self->{TimeoutCallback}; $self->{TimeoutCallback} = $callback; return $oldCallback; @@ -697,8 +718,8 @@ The output string can be directly sent t =cut sub Encrypt { - my $self = shift; # Reference to the object. - my $request = shift; # Text to send. + + my ($self, $request) = @_; # Split the encrypt: off the request and figure out it's length. @@ -740,8 +761,8 @@ Decrypt a response from the server. The =cut sub Decrypt { - my $self = shift; # Recover reference to object - my $encrypted = shift; # This is the encrypted data. + + my ($self, $encrypted) = @_; # Bust up the response into length, and encryptedstring: