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