Changeset 17229


Ignore:
Timestamp:
2008-09-09T19:05:10+12:00 (16 years ago)
Author:
ak19
Message:

Moved code for starting up (including opening connections) and quitting yaz-client into subroutines since the same code was repeated numerous times

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/downloaders/Z3950Download.pm

    r17220 r17229  
    112112    print STDERR "<<Defined Maximum>>\n";
    113113 
    114     print STDERR "Opening connection to $url\n";
    115      
    116     my  $yaz = $self->{'yaz'};
    117  
    118     my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
    119     or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
    120     $self->{'YAZOUT'} = *YAZOUT;
    121     $self->{'YAZIN'} = *YAZIN;
    122 
    123     $strOpen = $self->open_connection("open $url"); 
    124 
    125     if (!$strOpen) {
    126         print STDERR "Cannot connect to $url\n";
    127         print STDERR "<<Finished>>\n"; 
    128     return 0;
    129     }
     114    $strOpen = $self->start_yaz($url);
    130115
    131116    print STDERR "Access database: \"$self->{'database'}\"\n";
     
    148133    $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
    149134    print STDERR "Closing connection...\n";
     135
     136    $self->quit_yaz();
     137    return 1;
     138}
     139
     140
     141sub start_yaz
     142{
     143    my ($self, $url) = @_;   
     144
     145    print STDERR "Opening connection to $url\n";
     146     
     147    my $yaz = $self->{'yaz'};
     148   
     149    my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
     150    or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
     151    $self->{'YAZOUT'} = *YAZOUT;
     152    $self->{'YAZIN'} = *YAZIN;
     153
     154    my $strOpen = $self->open_connection("open $url"); 
     155
     156    if (!$strOpen) {
     157        print STDERR "Cannot connect to $url\n";
     158        print STDERR "<<Finished>>\n"; 
     159    return 0;
     160    }
     161    return $strOpen;
     162}
     163
     164sub quit_yaz
     165{
     166    my ($self) = shift (@_);
     167
    150168    print STDERR "<<Finished>>\n";
    151169
    152170    # need to send the quit command, else yaz-client is still running in the background
    153171    $self->run_command_without_output("quit");
    154     close(YAZOUT);
    155     close(YAZIN);
    156     return 1;
     172    close($self->{'YAZIN'}); # close the input to yaz. It also flushes quit command to yaz.
     173
     174    # make sure nothing is being output by yaz
     175    my $output = $self->{'YAZOUT'};
     176    my $line;
     177    while (defined ($line = <$output>)) {
     178    print STDERR "***### $line";
     179    }
     180
     181    close($self->{'YAZOUT'});
    157182}
    158183
     
    164189  my $out = $self->{'YAZOUT'};
    165190
    166   $_ = <$out>;
     191  my $opening_line = <$out>;
    167192 
    168   return (/Connecting...OK/i)? 1: 0;
     193  return ($opening_line =~ m/Connecting...OK/i)? 1: 0;
    169194 
    170195}
     
    269294   my $readRecord = 0;
    270295   
    271    while (my $strLine = <YAZOUT>)
     296   my $output = $self->{'YAZOUT'};
     297   while (my $strLine = <$output>)
    272298   {
    273299   
     
    344370   $url = $self->{'url'} unless defined $url;
    345371
    346    my  $yaz =  $self->{'yaz'};
    347    
    348    my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
    349        or die "can't open pipe to yaz-client: $!";
    350  
    351    $self->{'YAZOUT'} = *YAZOUT;
    352    $self->{'YAZIN'} = *YAZIN;
    353 
    354    my $strOpen = $self->open_connection("open $url");
    355    
    356     if (!$strOpen) {
    357         print STDERR "Cannot connect to $url\n";
    358         print STDERR "<<Finished>>\n"; 
    359     return 0;
    360     }
    361 
    362    
     372   my $strOpen = $self->start_yaz();
     373
    363374   $strOpen = $self->run_command_with_output("open $url","^Options"); 
    364 
    365375
    366376   $strOpen =~ s/Z> //g;
     
    369379   print STDERR $strOpen;
    370380
    371    print STDERR "<<Finished>>\n";
    372 
    373    # need to send the quit command, else yaz-client is still running in the background
    374    $self->run_command_without_output("quit");
    375    close(YAZOUT);
    376    close(YAZIN);
     381   $self->quit_yaz();
    377382
    378383   return 0;
Note: See TracChangeset for help on using the changeset viewer.