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