Use z3950.indexdata.com instead of bagel.indexdata.com everywhere
[ZOOM-Perl-moved-to-github.git] / samples / net-z3950-zoom / zoomtst3.pl
1 # $Id: zoomtst3.pl,v 1.8 2006-11-02 17:48:25 mike Exp $
2 #
3 # See ../README for a description of this program.
4 # perl -I../../blib/lib -I../../blib/arch zoomtst3.pl <t1> [...] <tN> <query>
5 # for example:
6 # perl -I../../blib/lib -I../../blib/arch zoomtst3.pl z3950.loc.gov:7090/Voyager z3950.indexdata.com:210/gils endeavor.flo.org:7090/Voyager mineral
7
8 use strict;
9 use warnings;
10 use Net::Z3950::ZOOM;
11
12 if (@ARGV < 2) {
13     print STDERR "Usage: $0 target1 target2 ... targetN query\n";
14     print STDERR "      eg. $0 z3950.indexdata.dk/gils localhost:9999 fish\n";
15     exit 1;
16 }
17
18 my $n = @ARGV-1;
19 my(@z, @r);                     # connections, result sets
20 my $o = Net::Z3950::ZOOM::options_create();
21 Net::Z3950::ZOOM::options_set($o, async => 1);
22
23 # Get first 10 records of result set (using piggyback)
24 Net::Z3950::ZOOM::options_set($o, count => 10);
25
26 # Preferred record syntax
27 Net::Z3950::ZOOM::options_set($o, preferredRecordSyntax => "usmarc");
28 Net::Z3950::ZOOM::options_set($o, elementSetName => "B");
29
30 # Connect to all targets: options are the same for all of them
31 for (my $i = 0; $i < $n; $i++) {
32     $z[$i] = Net::Z3950::ZOOM::connection_create($o);
33     Net::Z3950::ZOOM::connection_connect($z[$i], $ARGV[$i], 0);
34 }
35
36 # Search all
37 for (my $i = 0; $i < $n; $i++) {
38     $r[$i] = Net::Z3950::ZOOM::connection_search_pqf($z[$i], $ARGV[-1]);
39 }
40
41 # Network I/O.  Pass number of connections and array of connections
42 while ((my $i = Net::Z3950::ZOOM::event(\@z)) != 0) {
43     my $ev = Net::Z3950::ZOOM::connection_last_event($z[$i-1]);
44     print("connection ", $i-1, ": event $ev (",
45           Net::Z3950::ZOOM::event_str($ev), ")\n");
46     # It would be nice to display results as they come in, but the
47     # ability to do so is dependent on the END event, which was
48     # introduced only in YAZ 2.1.17.  If you have a sufficiently new
49     # YAZ, please use the alternative "async.pl", which is similar to
50     # this program except in its asynchronous display.
51 }
52
53 # No more to be done.  Inspect results
54 for (my $i = 0; $i < $n; $i++) {
55     my($error, $errmsg, $addinfo) = (undef, "dummy", "dummy");
56     my $tname = $ARGV[$i];
57
58     # Display errors if any
59     $error = Net::Z3950::ZOOM::connection_error($z[$i], $errmsg, $addinfo);
60     if ($error) {
61         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
62         next;
63     }
64
65     # OK, no major errors.  Look at the result count
66     my $size = Net::Z3950::ZOOM::resultset_size($r[$i]);
67     print "$tname: $size hits\n";
68
69     # Go through all records at target
70     $size = 10 if $size > 10;
71     for (my $pos = 0; $pos < $size; $pos++) {
72         my $len = 0; # length of buffer rec
73         print "$tname: fetching ", $pos+1, " of $size\n";
74         my $tmp = Net::Z3950::ZOOM::resultset_record($r[$i], $pos);
75         if (!defined $tmp) {
76             print "$tname: can't get record ", $pos+1, "\n";
77             next;
78         }
79         my $rec = Net::Z3950::ZOOM::record_get($tmp, "render", $len);
80         if (!defined $rec) {
81             print "$tname: can't render record ", $pos+1, "\n";
82             next;
83         }
84         print $pos+1, "\n", $rec, "\n";
85     }
86 }
87
88 # Housekeeping
89 for (my $i = 0; $i < $n; $i++) {
90     Net::Z3950::ZOOM::resultset_destroy($r[$i]);
91     Net::Z3950::ZOOM::connection_destroy($z[$i]);
92 }
93
94 Net::Z3950::ZOOM::options_destroy($o);