Changeset 17230


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

SRWDownload now finally quits once it has finished. It's no longer running in the background as shown in the TaskManager. The changes necessary were in the get() subroutine which used to not read most of the last record previously, but now reads all of it. Additional changes required were sending the quit message to SRW (moved into the quit_yaz subroutine found in the superclass Z3950Download.pm) as well as flushing the yaz-client's outputstream (also done in the quit_yaz subroutine).

File:
1 edited

Legend:

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

    r17219 r17230  
    7878    my ($strOpen,$strBase,$strFind,$strResponse,$intAmount,$intMaxRecords,$strRecords);
    7979
     80    # If the url contains just the host and port (as it would for Z39.50), then prepend
     81    # the http protocol. Otherwise the download is stuck in an infinite loop for SRW/SRU
     82    $self->{'url'} = "http://$self->{'url'}"  if $self->{'url'} !~ m/^http/;
    8083    my $url = $self->{'url'};
    8184
    8285    print STDERR "<<Defined Maximum>>\n";
    8386
    84     my  $yaz =  $self->{'yaz'};
    85  
    86     my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
    87     or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
    88 
    89     $self->{'YAZOUT'} = *YAZOUT;
    90     $self->{'YAZIN'} = *YAZIN;
    91 
    92     $strOpen = $self->open_connection("open $url");
    93 
    94     if (!$strOpen) {
    95         print STDERR "Cannot connect to $url\n";
    96         print STDERR "<<Finished>>\n"; 
    97     return 0;
    98     }
    99 
     87    $strOpen = $self->start_yaz($url);
     88   
    10089    print STDERR "Opening connection to \"$self->{'url'}\"\n";
    10190    print STDERR "Access database: \"$self->{'database'}\"\n";
     
    10392    $self->run_command_without_output("querytype prefix");
    10493    print STDERR "Searching for keyword: \"$self->{'find'}\"\n";
     94
    10595    $intAmount =$self->findAmount($self->{'find'});
    10696
     
    10999    ($intAmount == -1)?
    110100        print STDERR "Something wrong with the arguments,downloading can not be performed\n" :
    111         print STDERR "No Record is found\n";
    112         print STDERR "<<Finished>>\n";
     101        print STDERR "No Record is found\n";
     102    print STDERR "<<Finished>>\n";
    113103    return 0;
    114104    }
    115105    $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'};
    116106    print STDERR "<<Total number of record(s):$intMaxRecords>>\n";
    117  
     107   
    118108    $strRecords = $self->getRecords($intMaxRecords);
    119  
     109
    120110    $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
    121111    print STDERR "Closing connection...\n";
    122     print STDERR "<<Finished>>\n";
    123 
    124     # need to send the quit command, else yaz-client is still running in the background
    125     $self->run_command_without_output("quit");
    126     close(YAZOUT);
    127     close(YAZIN);
     112   
     113    $self->quit_yaz();
    128114    return 1;
    129115}
     
    148134    &util::mk_all_dir($strSubDirPath);
    149135
    150    print STDERR "Saving records to \"$strOutputFile\"\n";
     136    print STDERR "Saving records to \"$strOutputFile\"\n";
    151137
    152138    # save record
     
    157143}
    158144
    159 sub get{
     145sub get {
    160146   my ($self,$strShow,$numRecord) = @_; 
    161147
    162    $self->run_command($strShow);
    163    
     148   $self->run_command_without_output($strShow);
     149
    164150   my $strFullOutput="";
    165151   my $count=0;
    166152   my $readRecord = 0;
    167    
    168    while (my $strLine = <YAZOUT>)
     153   my $endRecord = 0;
     154
     155   my $output = $self->{'YAZOUT'};
     156   my $strLine;
     157
     158   while ($strLine = <$output>)    #while (defined ($strLine = <$output>))
    169159   {
    170    
    171        return $strFullOutput if ($count >= $numRecord);
    172 
    173        return $strFullOutput if($strLine =~ m/^HTTP ERROR/i);
    174 
    175        if ($strLine =~ m/pos=[\d]*/i ){
     160       last if ($count >= $numRecord && $endRecord); # done, if we've reached the end of the last record
     161
     162       last if($strLine =~ m/^HTTP ERROR/i);
     163
     164       if ($strLine =~ m/pos=[\d]*/i ) {
    176165           $count++;
    177166       $readRecord = 1;
    178        next; 
     167       $endRecord = 0;
     168       next;
    179169       }
    180      
     170
     171       if ($strLine =~ m/<\/record>/i ) { # end tag of record
     172       $endRecord = 1;
     173       }
     174
    181175       next if(!$readRecord);
    182176
    183177       $strFullOutput .= $strLine;     
    184178   }
    185  
     179
     180   return $strFullOutput;
    186181}
    187182
Note: See TracChangeset for help on using the changeset viewer.