always use LCDB_MARC8 database
[ZOOM-Perl-moved-to-github.git] / samples / zoom / async.pl
1 # See ../README for a description of this program.
2 # perl -I../../blib/lib -I../../blib/arch async.pl <t1> [...] <tN> <query>
3 # for example:
4 # perl -I../../blib/lib -I../../blib/arch async.pl lx2.loc.gov:210/LCDB_MARC8 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 my $nremaining = $n;
41 AGAIN:
42 my $i;
43 while (($i = ZOOM::event(\@z)) != 0) {
44     my $ev = $z[$i-1]->last_event();
45     print("connection ", $i-1, ": event $ev (", ZOOM::event_str($ev), ")\n");
46     last if $ev == ZOOM::Event::ZEND;
47 }
48
49 if ($i != 0) {
50     # Not the end of the whole loop; one server is ready to display
51     $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         goto MAYBE_AGAIN;
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 MAYBE_AGAIN:
82 if (--$nremaining > 0) {
83     goto AGAIN;
84 }
85
86 # Housekeeping
87 for (my $i = 0; $i < $n; $i++) {
88     $r[$i]->destroy();
89     $z[$i]->destroy();
90 }
91
92 $o->destroy();