--- loncom/LondConnection.pm 2003/07/02 01:12:35 1.7 +++ loncom/LondConnection.pm 2003/10/07 11:23:26 1.12 @@ -1,7 +1,7 @@ # This module defines and implements a class that represents # a connection to a lond daemon. # -# $Id: LondConnection.pm,v 1.7 2003/07/02 01:12:35 foxr Exp $ +# $Id: LondConnection.pm,v 1.12 2003/10/07 11:23:26 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -27,6 +27,7 @@ # package LondConnection; +use strict; use IO::Socket; use IO::Socket::INET; use IO::Handle; @@ -37,20 +38,30 @@ use Crypt::IDEA; use LONCAPA::Configuration; use LONCAPA::HashIterator; + + + my $DebugLevel=0; +my %hostshash; +my %perlvar; # Read the configuration file for apache to get the perl # variable set. -my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf'); -my %perlvar = %{$perlvarref}; -my $hoststab = - LONCAPA::Configuration::read_hosts( - "$perlvar{'lonTabDir'}/hosts.tab") || - die "Can't read host table!!"; -my %hostshash = %{$hoststab}; +sub ReadConfig { + my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf'); + %perlvar = %{$perlvarref}; + my $hoststab = + LONCAPA::Configuration::read_hosts( + "$perlvar{'lonTabDir'}/hosts.tab") || + die "Can't read host table!!"; + %hostshash = %{$hoststab}; + + close(CONFIG); +} + -close(CONFIG); +ReadConfig; # Make sure it gets read on init. sub Debug { my $level = shift; @@ -64,15 +75,17 @@ sub Debug { =head2 Dump -Dump the internal state of the object: For debugging purposes. +Dump the internal state of the object: For debugging purposes, to stderr. =cut sub Dump { my $self = shift; + my $key; + my $value; print "Dumping LondConnectionObject:\n"; while(($key, $value) = each %$self) { - print "$key -> $value\n"; + print STDERR "$key -> $value\n"; } print "-------------------------------\n"; } @@ -145,15 +158,16 @@ sub new { TimeoutCallback => undef, TransitionCallback => undef, Timeoutable => 0, - TimeoutValue => 60, - TimeoutRemaining => 0, + TimeoutValue => 30, + TimeoutRemaining => 0, CipherKey => "", Cipher => undef}; bless($self, $class); unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host}, PeerPort => $self->{Port}, Type => SOCK_STREAM, - Proto => "tcp")) { + Proto => "tcp", + Timeout => 5)) { return undef; # Inidicates the socket could not be made. } # @@ -161,13 +175,14 @@ sub new { # $self->Transition("Connected"); $self->{InformWritable} = 1; # When socket is writable we send init + $self->{Timeoutable} = 1; # Timeout allowed during startup negotiation. $self->{TransactionRequest} = "init\n"; # # Set socket to nonblocking I/O. # my $socket = $self->{Socket}; - $flags = fcntl($socket->fileno, F_GETFL,0); + my $flags = fcntl($socket->fileno, F_GETFL,0); if($flags == -1) { $socket->close; return undef; @@ -215,11 +230,10 @@ sub Readable { my $rv = $socket->recv($data, POSIX::BUFSIZ, 0); my $errno = $! + 0; # Force numeric context. - unless (defined($rv) && (length($data)> 0)) {# Read failed, + unless (defined($rv) && length $data) {# Read failed, if(($errno == POSIX::EWOULDBLOCK) || ($errno == POSIX::EAGAIN) || - ($errno == POSIX::EINTR) || - ($errno == 0)) { + ($errno == POSIX::EINTR)) { return 0; } @@ -237,7 +251,7 @@ sub Readable { if($self->{TransactionReply} =~ /(.*\n)/) { &Debug(8,"Readable End of line detected"); if ($self->{State} eq "Initialized") { # We received the challenge: - if($self->{TransactionReply} eq "refused") { # Remote doesn't have + if($self->{TransactionReply} eq "refused\n") { # Remote doesn't have $self->Transition("Disconnected"); # in host tables. $socket->close(); @@ -337,7 +351,7 @@ sub Writable { } } - if (($rv >= 0) || + if (($nwritten >= 0) || ($errno == POSIX::EWOULDBLOCK) || ($errno == POSIX::EAGAIN) || ($errno == POSIX::EINTR) || @@ -654,7 +668,7 @@ sub Decrypt { # Decode the data in 8 byte blocks. The string is encoded # as hex digits so there are two characters per byte: - $decrpyted = ""; + my $decrypted = ""; for(my $index = 0; $index < length($EncryptedString); $index += 16) { $decrypted .= $self->{Cipher}->decrypt( @@ -685,7 +699,7 @@ are used as follows: [1] - LonCapa domain name. [2] - Loncapa role (e.g. library or access). [3] - DNS name server hostname. - [4] - IP address (result of e.g. nslooup [3]). + [4] - IP address (result of e.g. nslookup [3]). [5] - Maximum connection count. [6] - Idle timeout for reducing connection count. [7] - Minimum connection count.