ac3e5f90d30a317a848fe525a98875c82fff43c2
[ZOOM-Perl-moved-to-github.git] / samples / zoom / async.pl
1 # $Id: async.pl,v 1.2 2006-11-02 17:48:26 mike Exp $
2 #
3 # See ../README for a description of this program.
4 # perl -I../../blib/lib -I../../blib/arch async.pl <t1> [...] <tN> <query>
5 # for example:
6 # perl -I../../blib/lib -I../../blib/arch async.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 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 = new ZOOM::Options();
21 $o->option(async => 1);
22
23 # Get first 10 records of result set (using piggyback)
24 $o->option(count => 10);
25
26 # Preferred record syntax
27 $o->option(preferredRecordSyntax => "usmarc");
28 $o->option(elementSetName => "F");
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] = create ZOOM::Connection($o);
33     $z[$i]->connect($ARGV[$i]);
34 }
35
36 # Search all
37 for (my $i = 0; $i < $n; $i++) {
38     $r[$i] = $z[$i]->search_pqf($ARGV[-1]);
39 }
40
41 # Network I/O.  Pass number of connections and array of connections
42 my $nremaining = $n;
43 AGAIN:
44 my $i;
45 while (($i = ZOOM::event(\@z)) != 0) {
46     my $ev = $z[$i-1]->last_event();
47     print("connection ", $i-1, ": event $ev (", ZOOM::event_str($ev), ")\n");
48     last if $ev == ZOOM::Event::ZEND;
49 }
50
51 if ($i != 0) {
52     # Not the end of the whole loop; one server is ready to display
53     $i--;
54     my $tname = $ARGV[$i];
55     my($error, $errmsg, $addinfo, $diagset) = $z[$i]->error_x();
56     if ($error) {
57         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
58         goto MAYBE_AGAIN;
59     }
60
61     # OK, no major errors.  Look at the result count
62     my $size = $r[$i]->size();
63     print "$tname: $size hits\n";
64
65     # Go through all records at target
66     $size = 10 if $size > 10;
67     for (my $pos = 0; $pos < $size; $pos++) {
68         print "$tname: fetching ", $pos+1, " of $size\n";
69         my $tmp = $r[$i]->record($pos);
70         if (!defined $tmp) {
71             print "$tname: can't get record ", $pos+1, "\n";
72             next;
73         }
74         my $rec = $tmp->render();
75         if (!defined $rec) {
76             print "$tname: can't render record ", $pos+1, "\n";
77             next;
78         }
79         print $pos+1, "\n", $rec, "\n";
80     }
81 }
82
83 MAYBE_AGAIN:
84 if (--$nremaining > 0) {
85     goto AGAIN;
86 }
87
88 # Housekeeping
89 for (my $i = 0; $i < $n; $i++) {
90     $r[$i]->destroy();
91     $z[$i]->destroy();
92 }
93
94 $o->destroy();