always use LCDB_MARC8 database
[ZOOM-Perl-moved-to-github.git] / samples / net-z3950-zoom / async.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 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 Net::Z3950::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 = Net::Z3950::ZOOM::options_create();
19 Net::Z3950::ZOOM::options_set($o, async => 1);
20
21 # Get first 10 records of result set (using piggyback)
22 Net::Z3950::ZOOM::options_set($o, count => 10);
23
24 # Preferred record syntax
25 Net::Z3950::ZOOM::options_set($o, preferredRecordSyntax => "usmarc");
26 Net::Z3950::ZOOM::options_set($o, elementSetName => "B");
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] = Net::Z3950::ZOOM::connection_create($o);
31     Net::Z3950::ZOOM::connection_connect($z[$i], $ARGV[$i], 0);
32 }
33
34 # Search all
35 for (my $i = 0; $i < $n; $i++) {
36     $r[$i] = Net::Z3950::ZOOM::connection_search_pqf($z[$i], $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 = Net::Z3950::ZOOM::event(\@z)) != 0) {
44     my $ev = Net::Z3950::ZOOM::connection_last_event($z[$i-1]);
45     print("connection ", $i-1, ": event $ev (",
46           Net::Z3950::ZOOM::event_str($ev), ")\n");
47     last if $ev == Net::Z3950::ZOOM::EVENT_END;
48 }
49
50 if ($i != 0) {
51     # Not the end of the whole loop; one server is ready to display
52     $i--;
53     my($error, $errmsg, $addinfo) = (undef, "dummy", "dummy");
54     my $tname = $ARGV[$i];
55
56     # Display errors if any
57     $error = Net::Z3950::ZOOM::connection_error($z[$i], $errmsg, $addinfo);
58     if ($error) {
59         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
60         goto MAYBE_AGAIN;
61     }
62
63     # OK, no major errors.  Look at the result count
64     my $size = Net::Z3950::ZOOM::resultset_size($r[$i]);
65     print "$tname: $size hits\n";
66
67     # Go through all records at target
68     $size = 10 if $size > 10;
69     for (my $pos = 0; $pos < $size; $pos++) {
70         print "$tname: fetching ", $pos+1, " of $size\n";
71         my $tmp = Net::Z3950::ZOOM::resultset_record($r[$i], $pos);
72         if (!defined $tmp) {
73             print "$tname: can't get record ", $pos+1, "\n";
74             next;
75         }
76         my $rec = Net::Z3950::ZOOM::record_get($tmp, "render");
77         if (!defined $rec) {
78             print "$tname: can't render record ", $pos+1, "\n";
79             next;
80         }
81         print $pos+1, "\n", $rec, "\n";
82     }
83 }
84
85 MAYBE_AGAIN:
86 if (--$nremaining > 0) {
87     goto AGAIN;
88 }
89
90 # Housekeeping
91 for (my $i = 0; $i < $n; $i++) {
92     Net::Z3950::ZOOM::resultset_destroy($r[$i]);
93     Net::Z3950::ZOOM::connection_destroy($z[$i]);
94 }
95
96 Net::Z3950::ZOOM::options_destroy($o);