different irspy home for devel installation
[irspy-moved-to-github.git] / bin / test-zoom-c.c
1 /*
2  * $Id: test-zoom-c.c,v 1.2 2006-07-20 12:01:43 mike Exp $
3  *
4  * Run the same way as "test-pod.pl".  This is supposed to be an
5  * exactly equivalent program but written using the ZOOM-C imperative
6  * API for asynchronous events directly rather than through the
7  * intermediary of Net::Z3950::ZOOM, ZOOM::Pod and ZOOM-Perl.
8  */
9
10 #include <assert.h>
11 #include <stdio.h>
12 #include <yaz/zoom.h>
13 #include <yaz/log.h>
14
15
16 struct conn_and_state {
17     ZOOM_connection conn;
18     ZOOM_resultset rs;
19     int next_to_show, next_to_fetch;
20 };
21
22
23 static void completed_search(struct conn_and_state *csp);
24 static void got_record(struct conn_and_state *csp);
25 static void request_records(struct conn_and_state *csp, int count);
26
27
28 int main (int argc, char *argv[]) {
29     ZOOM_options options;
30     struct conn_and_state cs[100];
31     ZOOM_connection zconn[100];
32     int i, n;
33
34     if (argc == 1) {
35         fprintf(stderr, "Usage: %s <target1> [<target2> ...]\n", argv[0]);
36         return 1;
37     }
38
39     yaz_log_mask_str("appl");
40     options = ZOOM_options_create();
41     ZOOM_options_set_int(options, "async", 1);
42     ZOOM_options_set(options, "elementSetName", "b");
43
44     n = argc-1;
45     for (i = 0; i < n; i++) {
46         char *target = argv[i+1];
47         cs[i].conn = zconn[i] = ZOOM_connection_create(options);
48         ZOOM_connection_connect(cs[i].conn, target, 0);
49         cs[i].rs = ZOOM_connection_search_pqf(cs[i].conn, "the");
50         cs[i].next_to_show = 0;
51         cs[i].next_to_fetch = 0;
52     }
53
54     while ((i = ZOOM_event(n, zconn)) != 0) {
55         struct conn_and_state *csp = &cs[i-1];
56         int ev = ZOOM_connection_last_event(csp->conn);
57         int errcode;
58         const char *errmsg;
59         const char *addinfo;
60
61         yaz_log(yaz_log_module_level("pod"),
62                 "connection %d: event %d", i-1, ev);
63
64         errcode = ZOOM_connection_error(csp->conn, &errmsg, &addinfo);
65         if (errcode != 0) {
66             fprintf(stderr, "error %d (%s) [%s]\n", errcode, errmsg, addinfo);
67             return 2;
68         }
69
70         if (ev == ZOOM_EVENT_RECV_SEARCH) {
71             completed_search(csp);
72         } else if (ev == ZOOM_EVENT_RECV_RECORD) {
73             got_record(csp);
74         }
75     }
76
77     return 0;
78 }
79
80
81 static void completed_search(struct conn_and_state *csp) {
82     const char *host = ZOOM_connection_option_get(csp->conn, "host");
83
84     printf("%s: found %d records\n", host, ZOOM_resultset_size(csp->rs));
85     request_records(csp, 2);
86 }
87
88
89 static void got_record(struct conn_and_state *csp) {
90     const char *host = ZOOM_connection_option_get(csp->conn, "host");
91     int i, len;
92     ZOOM_record rec;
93
94     assert(csp->next_to_show < csp->next_to_fetch);
95     i = csp->next_to_show++;
96     rec = ZOOM_resultset_record(csp->rs, i);
97     printf("%s: record %d is %s\n", host, i,
98            rec == 0 ? "undefined" : ZOOM_record_get(rec, "render", &len));
99     if (i == csp->next_to_fetch-1)
100         request_records(csp, 3);
101 }
102
103
104 static void request_records(struct conn_and_state *csp, int count) {
105     const char *host = ZOOM_connection_option_get(csp->conn, "host");
106     int i = csp->next_to_fetch;
107
108     yaz_log(yaz_log_module_level("appl"),
109             "requesting %d records from %d for %s", count, i, host);
110
111     ZOOM_resultset_records(csp->rs, (ZOOM_record*) 0, i, count);
112     csp->next_to_fetch += count;
113 }