3 # Run the same way as "test-pod.pl". This is supposed to be an
4 # exactly equivalent program but written using the Net::Z3950::ZOOM
5 # imperative API for asynchronous events directly rather than through
6 # the intermediary of ZOOM::Pod and ZOOM-Perl.
14 printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
18 Net::Z3950::ZOOM::yaz_log_mask_str("appl");
20 my %rs; # maps connection to result-set
21 my %conn2state; # maps connection to app. state structure
22 foreach my $target (@ARGV) {
23 my $options = Net::Z3950::ZOOM::options_create();
24 Net::Z3950::ZOOM::options_set($options, async => 1);
25 Net::Z3950::ZOOM::options_set($options, elementSetName => "b");
26 my $conn = Net::Z3950::ZOOM::connection_create($options);
27 Net::Z3950::ZOOM::connection_connect($conn, $target, 0);
29 $rs{$conn} = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
33 while ((my $i = Net::Z3950::ZOOM::event(\@conn)) != 0) {
34 my $conn = $conn[$i-1];
35 my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
36 my $evstr = Net::Z3950::ZOOM::event_str($ev);
37 Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("pod"),
38 "connection " . ($i-1) . ": event $ev ($evstr)");
40 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
41 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
42 die "error $errcode ($errmsg) [$addinfo]"
45 if ($ev == Net::Z3950::ZOOM::EVENT_RECV_SEARCH) {
46 $res = completed_search($conn, \%conn2state, $rs{$conn}, $ev);
47 die "recieve search failed with error $res" if $res;
48 } elsif ($ev == Net::Z3950::ZOOM::EVENT_RECV_RECORD) {
49 $res = got_record($conn, \%conn2state, $rs{$conn}, $ev);
50 die "recieve record failed with error $res" if $res;
54 sub completed_search {
55 my($conn, $arg, $rs, $event) = @_;
57 my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
58 print "$host : found ", Net::Z3950::ZOOM::resultset_size($rs), " records\n";
59 my %state = (next_to_show => 0, next_to_fetch => 0);
60 request_records($conn, $rs, \%state, 2);
61 $arg->{$host} = \%state;
66 my($conn, $arg, $rs, $event) = @_;
68 my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
69 my $state = $arg->{$host};
72 # Sanity-checking assertions. These should be impossible
73 my $ns = $state->{next_to_show};
74 my $nf = $state->{next_to_fetch};
76 die "next_to_show > next_to_fetch ($ns > $nf)";
77 } elsif ($ns == $nf) {
78 die "next_to_show == next_to_fetch ($ns)";
82 my $i = $state->{next_to_show}++;
83 my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i);
84 print "$host: record $i is ", render_record($rec), "\n";
85 request_records($conn, $rs, $state, 3)
86 if $i == $state->{next_to_fetch}-1;
92 my($conn, $rs, $state, $count) = @_;
94 my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
95 my $i = $state->{next_to_fetch};
96 Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("appl"),
97 "requesting $count records from $i for $host");
99 Net::Z3950::ZOOM::resultset_records($rs, $i, $count, 0);
100 $state->{next_to_fetch} += $count;
106 return "undefined" if !defined $rec;
108 return "'" . Net::Z3950::ZOOM::record_get($rec, "render", $len) . "'";