Omit vacuous port-0 argument to zoomtst3.pl
[ZOOM-Perl-moved-to-github.git] / samples / zoom / zoomtst3.pl
1 # $Id: zoomtst3.pl,v 1.2 2006-04-07 11:25:58 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
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 bagel.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.
44 }
45
46 # No more to be done.  Inspect results
47 for (my $i = 0; $i < $n; $i++) {
48     my $tname = $ARGV[$i];
49     my($error, $errmsg, $addinfo, $diagset) = $z[$i]->error_x();
50     if ($error) {
51         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
52         next;
53     }
54
55     # OK, no major errors.  Look at the result count
56     my $size = $r[$i]->size();
57     print "$tname: $size hits\n";
58
59     # Go through all records at target
60     $size = 10 if $size > 10;
61     for (my $pos = 0; $pos < $size; $pos++) {
62         print "$tname: fetching ", $pos+1, " of $size\n";
63         my $tmp = $r[$i]->record($pos);
64         if (!defined $tmp) {
65             print "$tname: can't get record ", $pos+1, "\n";
66             next;
67         }
68         my $rec = $tmp->render();
69         if (!defined $rec) {
70             print "$tname: can't render record ", $pos+1, "\n";
71             next;
72         }
73         print $pos+1, "\n", $rec, "\n";
74     }
75 }
76
77 # Housekeeping
78 for (my $i = 0; $i < $n; $i++) {
79     $r[$i]->destroy();
80     $z[$i]->destroy();
81 }
82
83 $o->destroy();