Move test-net-z3950-zoom.pl to bin/test-net-z3950-zoom.pl
[irspy-moved-to-github.git] / bin / test-net-z3950-zoom.pl
1 #!/usr/bin/perl -w
2
3 # $Id: test-net-z3950-zoom.pl,v 1.1 2006-07-19 11:41:05 mike Exp $
4 #
5 # Run the same way as "test-pod.pl".  This is supposed to be an
6 # exactly equivalent program but written using the Net::Z3950::ZOOM
7 # imperative API for asynchronous events directly rather than through
8 # the intermediary of ZOOM::Pod and ZOOM-Perl.
9
10 use strict;
11 use warnings;
12
13 use Net::Z3950::ZOOM;
14
15 if (@ARGV == 0) {
16     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
17     exit 1;
18 }
19
20 Net::Z3950::ZOOM::yaz_log_mask_str("appl");
21 my @conn;
22 my %rs;                         # maps connection to result-set
23 my %conn2state;                 # maps connection to app. state structure
24 foreach my $target (@ARGV) {
25     my $options = Net::Z3950::ZOOM::options_create();
26     Net::Z3950::ZOOM::options_set($options, async => 1);
27     Net::Z3950::ZOOM::options_set($options, elementSetName => "b");
28     my $conn = Net::Z3950::ZOOM::connection_create($options);
29     Net::Z3950::ZOOM::connection_connect($conn, $target, 0);
30     push @conn, $conn;
31     $rs{$conn} = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
32 }
33
34 my $res = 0;
35 while ((my $i = Net::Z3950::ZOOM::event(\@conn)) != 0) {
36     my $conn = $conn[$i-1];
37     my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
38     my $evstr = Net::Z3950::ZOOM::event_str($ev);
39     Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("pod"),
40                               "connection " . ($i-1) . ": event $ev ($evstr)");
41
42     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
43     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
44     die "error $errcode ($errmsg) [$addinfo]"
45         if $errcode != 0;
46
47     if ($ev == Net::Z3950::ZOOM::EVENT_RECV_SEARCH) {
48         $res = completed_search($conn, \%conn2state, $rs{$conn}, $ev);
49         die "recieve search failed with error $res" if $res;
50     } elsif ($ev == Net::Z3950::ZOOM::EVENT_RECV_RECORD) {
51         $res = got_record($conn, \%conn2state, $rs{$conn}, $ev);
52         die "recieve record failed with error $res" if $res;
53     }
54 }
55
56 sub completed_search {
57     my($conn, $arg, $rs, $event) = @_;
58
59     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
60     print "$host : found ", Net::Z3950::ZOOM::resultset_size($rs), " records\n";
61     my %state = (next_to_show => 0, next_to_fetch => 0);
62     request_records($conn, $rs, \%state, 2);
63     $arg->{$host} = \%state;
64     return 0;
65 }
66
67 sub got_record {
68     my($conn, $arg, $rs, $event) = @_;
69
70     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
71     my $state = $arg->{$host};
72
73     {
74         # Sanity-checking assertions.  These should be impossible
75         my $ns = $state->{next_to_show};
76         my $nf = $state->{next_to_fetch};
77         if ($ns > $nf) {
78             die "next_to_show > next_to_fetch ($ns > $nf)";
79         } elsif ($ns == $nf) {
80             die "next_to_show == next_to_fetch ($ns)";
81         }
82     }
83
84     my $i = $state->{next_to_show}++;
85     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i);
86     print "$host: record $i is ", render_record($rec), "\n";
87     request_records($conn, $rs, $state, 3)
88         if $i == $state->{next_to_fetch}-1;
89
90     return 0;
91 }
92
93 sub request_records {
94     my($conn, $rs, $state, $count) = @_;
95
96     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
97     my $i = $state->{next_to_fetch};
98     Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("appl"),
99                               "requesting $count records from $i for $host");
100
101     Net::Z3950::ZOOM::resultset_records($rs, $i, $count, 0);
102     $state->{next_to_fetch} += $count;
103 }
104
105 sub render_record {
106     my($rec) = @_;
107
108     return "undefined" if !defined $rec;
109     my $len;
110     return "'" . Net::Z3950::ZOOM::record_get($rec, "render", $len) . "'";
111 }