Diff for /loncom/lonnet/perl/lonnet.pm between versions 1.850 and 1.854

version 1.850, 2007/03/27 19:38:39 version 1.854, 2007/03/28 21:44:13
Line 53  use Digest::MD5; Line 53  use Digest::MD5;
 use Math::Random;  use Math::Random;
 use LONCAPA qw(:DEFAULT :match);  use LONCAPA qw(:DEFAULT :match);
 use LONCAPA::Configuration;  use LONCAPA::Configuration;
   use Apache::lonhosts;
   
 my $readit;  my $readit;
 my $max_connection_retries = 10;     # Or some such value.  my $max_connection_retries = 10;     # Or some such value.
Line 145  sub logperm { Line 146  sub logperm {
 }  }
   
 sub create_connection {  sub create_connection {
     my ($server) = @_;      my ($hostname,$lonid) = @_;
     my $client=IO::Socket::UNIX->new(Peer    =>"/home/httpd/sockets/common",      my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
      Type    => SOCK_STREAM,       Type    => SOCK_STREAM,
      Timeout => 10);       Timeout => 10);
     return 0 if (!$client);      return 0 if (!$client);
     print $client ("$server\n");      print $client (join(':',$hostname,$lonid,&machine_ids($lonid))."\n");
     my $result = <$client>;      my $result = <$client>;
     chomp($result);      chomp($result);
     return 1 if ($result eq 'done');      return 1 if ($result eq 'done');
Line 185  sub subreply { Line 186  sub subreply {
  if($client) {   if($client) {
     last; # Connected!      last; # Connected!
  } else {   } else {
     &create_connection(&hostname($server));      &create_connection(&hostname($server),$server);
  }   }
         sleep(1); # Try again later if failed connection.          sleep(1); # Try again later if failed connection.
     }      }
Line 7426  sub hreflocation { Line 7427  sub hreflocation {
 }  }
   
 sub current_machine_domains {  sub current_machine_domains {
     my $hostname=&hostname($perlvar{'lonHostID'});      return &machine_domains(&hostname($perlvar{'lonHostID'}));
   }
   
   sub machine_domains {
       my ($hostname) = @_;
     my @domains;      my @domains;
     my %hostname = &all_hostnames();      my %hostname = &all_hostnames();
     while( my($id, $name) = each(%hostname)) {      while( my($id, $name) = each(%hostname)) {
Line 7439  sub current_machine_domains { Line 7444  sub current_machine_domains {
 }  }
   
 sub current_machine_ids {  sub current_machine_ids {
     my $hostname=&hostname($perlvar{'lonHostID'});      return &machine_ids(&hostname($perlvar{'lonHostID'}));
   }
   
   sub machine_ids {
       my ($hostname) = @_;
       $hostname ||= &hostname($perlvar{'lonHostID'});
     my @ids;      my @ids;
     my %hostname = &all_hostnames();      my %hostname = &all_hostnames();
     while( my($id, $name) = each(%hostname)) {      while( my($id, $name) = each(%hostname)) {
Line 7587  BEGIN { Line 7597  BEGIN {
     %perlvar = (%perlvar,%{$configvars});      %perlvar = (%perlvar,%{$configvars});
 }  }
   
   sub get_dns {
       my ($url,$func) = @_;
       open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
       foreach my $dns (<$config>) {
    next if ($dns !~ /^\^(\S*)/x);
    $dns = $1;
    my $ua=new LWP::UserAgent;
    my $request=new HTTP::Request('GET',"http://$dns$url");
    my $response=$ua->request($request);
    next if ($response->is_error());
    my @content = split("\n",$response->content);
    &$func(\@content);
       }
       close($config);
   }
 # ------------------------------------------------------------ Read domain file  # ------------------------------------------------------------ Read domain file
 {  {
       my $loaded;
     my %domain;      my %domain;
   
     my $fh;      sub parse_domain_tab {
     if (open($fh,"<".$Apache::lonnet::perlvar{'lonTabDir'}.'/domain.tab')) {   my ($lines) = @_;
  while (my $line = <$fh>) {   foreach my $line (@$lines) {
     next if ($line =~ /^(\#|\s*$ )/);      next if ($line =~ /^(\#|\s*$ )/x);
   
     chomp($line);      chomp($line);
     my ($name,@elements) =  split(/:/,$line,9);      my ($name,@elements) = split(/:/,$line,9);
     my %this_domain;      my %this_domain;
     foreach my $field ('description', 'auth_def', 'auth_arg_def',      foreach my $field ('description', 'auth_def', 'auth_arg_def',
        'lang_def', 'city', 'longi', 'lati',         'lang_def', 'city', 'longi', 'lati',
Line 7605  BEGIN { Line 7631  BEGIN {
  $this_domain{$field} = shift(@elements);   $this_domain{$field} = shift(@elements);
     }      }
     $domain{$name} = \%this_domain;      $domain{$name} = \%this_domain;
 #          &logthis("Domain.tab: $domain ".$domaindescription{$domain} );      &logthis("Domain.tab: $name ".$domain{$name}{'description'} );
  }   }
     }      }
     close ($fh);      
       sub load_domain_tab {
    &get_dns('/adm/dns/domain',\&parse_domain_tab);
    my $fh;
    if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
       my @lines = <$fh>;
       &parse_domain_tab(\@lines);
    }
    close($fh);
    $loaded = 1;
       }
   
     sub domain {      sub domain {
    &load_domain_tab() if (!$loaded);
   
  my ($name,$what) = @_;   my ($name,$what) = @_;
  return if ( !exists($domain{$name}) );   return if ( !exists($domain{$name}) );
   
Line 7627  BEGIN { Line 7665  BEGIN {
     my %hostname;      my %hostname;
     my %hostdom;      my %hostdom;
     my %libserv;      my %libserv;
     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");      my $loaded;
   
     while (my $configline=<$config>) {      sub parse_hosts_tab {
        next if ($configline =~ /^(\#|\s*$)/);   my ($file) = @_;
        chomp($configline);   foreach my $configline (@$file) {
        my ($id,$domain,$role,$name)=split(/:/,$configline);      next if ($configline =~ /^(\#|\s*$ )/x);
        $name=~s/\s//g;      next if ($configline =~ /^\^/);
        if ($id && $domain && $role && $name) {      chomp($configline);
  $hostname{$id}=$name;      my ($id,$domain,$role,$name)=split(/:/,$configline);
  $hostdom{$id}=$domain;      $name=~s/\s//g;
  if ($role eq 'library') { $libserv{$id}=$name; }      if ($id && $domain && $role && $name) {
        }   $hostname{$id}=$name;
    $hostdom{$id}=$domain;
    if ($role eq 'library') { $libserv{$id}=$name; }
       }
       &logthis("Hosts.tab: $name ".$id );
    }
     }      }
     close($config);  
       sub load_hosts_tab {
    &get_dns('/adm/dns/hosts',\&parse_hosts_tab);
    open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
    my @config = <$config>;
    &parse_hosts_tab(\@config);
    close($config);
    $loaded=1;
       }
   
     # FIXME: dev server don't want this, production servers _do_ want this      # FIXME: dev server don't want this, production servers _do_ want this
     #&get_iphost();      #&get_iphost();
   
     sub hostname {      sub hostname {
    &load_hosts_tab() if (!$loaded);
   
  my ($lonid) = @_;   my ($lonid) = @_;
  return $hostname{$lonid};   return $hostname{$lonid};
     }      }
   
     sub all_hostnames {      sub all_hostnames {
    &load_hosts_tab() if (!$loaded);
   
  return %hostname;   return %hostname;
     }      }
   
     sub is_library {      sub is_library {
    &load_hosts_tab() if (!$loaded);
   
  return exists($libserv{$_[0]});   return exists($libserv{$_[0]});
     }      }
   
     sub all_library {      sub all_library {
    &load_hosts_tab() if (!$loaded);
   
  return %libserv;   return %libserv;
     }      }
   
     sub get_servers {      sub get_servers {
    &load_hosts_tab() if (!$loaded);
   
  my ($domain,$type) = @_;   my ($domain,$type) = @_;
  my %possible_hosts = ($type eq 'library') ? %libserv   my %possible_hosts = ($type eq 'library') ? %libserv
                                           : %hostname;                                            : %hostname;
Line 7683  BEGIN { Line 7745  BEGIN {
     }      }
   
     sub host_domain {      sub host_domain {
    &load_hosts_tab() if (!$loaded);
   
  my ($lonid) = @_;   my ($lonid) = @_;
  return $hostdom{$lonid};   return $hostdom{$lonid};
     }      }
   
     sub all_domains {      sub all_domains {
    &load_hosts_tab() if (!$loaded);
   
  my %seen;   my %seen;
  my @uniq = grep(!$seen{$_}++, values(%hostdom));   my @uniq = grep(!$seen{$_}++, values(%hostdom));
  return @uniq;   return @uniq;

Removed from v.1.850  
changed lines
  Added in v.1.854


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