Change Quick test to probe piggybacking.
[irspy-moved-to-github.git] / bin / test-zoom.pl
1 #!/usr/bin/perl
2 #
3 # Run the same way as "test-pod.pl".  This is supposed to be an
4 # exactly equivalent program but written using the ZOOM-Perl
5 # asynchronous-event API directly rather than through the intermediary
6 # of ZOOM::Pod.
7
8 use strict;
9 use warnings;
10
11 use ZOOM;
12
13 if (@ARGV == 0) {
14     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
15     exit 1;
16 }
17
18 ZOOM::Log::mask_str("appl");
19 my @conn;
20 my %rs;                         # maps connection to result-set
21 my %state;                      # maps connection to app. state structure
22 foreach my $target (@ARGV) {
23     my $conn = new ZOOM::Connection($target, 0, async => 1,
24                                     elementSetName => "b");
25     push @conn, $conn;
26     $rs{$conn} = $conn->search_pqf("the");
27 }
28
29 my $res = 0;
30 while ((my $i = ZOOM::event(\@conn)) != 0) {
31     my $conn = $conn[$i-1];
32     my $ev = $conn->last_event();
33     my $evstr = ZOOM::event_str($ev);
34     ZOOM::Log::log("pod", "connection ", $i-1, ": event $ev ($evstr)");
35     $conn->check();             # die if any errors occur
36
37     if ($ev == ZOOM::Event::RECV_SEARCH) {
38         $res = completed_search($conn, \%state, $rs{$conn}, $ev);
39         die "recieve search failed with error $res" if $res;
40     } elsif ($ev == ZOOM::Event::RECV_RECORD) {
41         $res = got_record($conn, \%state, $rs{$conn}, $ev);
42         die "recieve record failed with error $res" if $res;
43     }
44 }
45
46
47
48
49 sub completed_search {
50     my($conn, $arg, $rs, $event) = @_;
51
52     my $host = $conn->option("host");
53     print "$host : found ", $rs->size(), " records\n";
54     my %state = (next_to_show => 0, next_to_fetch => 0);
55     request_records($conn, $rs, \%state, 2);
56     $arg->{$host} = \%state;
57     return 0;
58 }
59
60 sub got_record {
61     my($conn, $arg, $rs, $event) = @_;
62
63     my $host = $conn->option("host");
64     my $state = $arg->{$host};
65
66     {
67         # Sanity-checking assertions.  These should be impossible
68         my $ns = $state->{next_to_show};
69         my $nf = $state->{next_to_fetch};
70         if ($ns > $nf) {
71             die "next_to_show > next_to_fetch ($ns > $nf)";
72         } elsif ($ns == $nf) {
73             die "next_to_show == next_to_fetch ($ns)";
74         }
75     }
76
77     my $i = $state->{next_to_show}++;
78     my $rec = $rs->record($i);
79     print "$host: record $i is ", render_record($rec), "\n";
80     request_records($conn, $rs, $state, 3)
81         if $i == $state->{next_to_fetch}-1;
82
83     return 0;
84 }
85
86 sub request_records {
87     my($conn, $rs, $state, $count) = @_;
88
89     my $i = $state->{next_to_fetch};
90     ZOOM::Log::log("appl", "requesting $count records from $i for ",
91                    $conn->option("host"));
92     $rs->records($i, $count, 0);
93     $state->{next_to_fetch} += $count;
94 }
95
96 sub render_record {
97     my($rec) = @_;
98
99     return "undefined" if !defined $rec;
100     return "'" . $rec->render() . "'";
101 }