3 # $Id: test-net-z3950-zoom.pl,v 1.2 2006-07-19 16:28:59 mike Exp $
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.
16 printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
20 Net::Z3950::ZOOM::yaz_log_mask_str("appl");
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);
31 $rs{$conn} = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
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)");
42 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
43 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
44 die "error $errcode ($errmsg) [$addinfo]"
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;
56 sub completed_search {
57 my($conn, $arg, $rs, $event) = @_;
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;
68 my($conn, $arg, $rs, $event) = @_;
70 my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
71 my $state = $arg->{$host};
74 # Sanity-checking assertions. These should be impossible
75 my $ns = $state->{next_to_show};
76 my $nf = $state->{next_to_fetch};
78 die "next_to_show > next_to_fetch ($ns > $nf)";
79 } elsif ($ns == $nf) {
80 die "next_to_show == next_to_fetch ($ns)";
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;
94 my($conn, $rs, $state, $count) = @_;
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");
101 Net::Z3950::ZOOM::resultset_records($rs, $i, $count, 0);
102 $state->{next_to_fetch} += $count;
108 return "undefined" if !defined $rec;
110 return "'" . Net::Z3950::ZOOM::record_get($rec, "render", $len) . "'";