New, derived from test-net-z3950-zoom.pl
[irspy-moved-to-github.git] / bin / test-zoom-c.c
1 /*
2  * $Id: test-zoom-c.c,v 1.1 2006-07-19 16:29:51 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 static const char *render_record(ZOOM_record rec);
27
28
29 int main (int argc, char *argv[]) {
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     n = argc-1;
41     for (i = 0; i < n; i++) {
42         char *target = argv[i+1];
43         ZOOM_options options = ZOOM_options_create();
44         ZOOM_options_set_int(options, "async", 1);
45         ZOOM_options_set(options, "elementSetName", "b");
46         cs[i].conn = zconn[i] = ZOOM_connection_create(options);
47         ZOOM_connection_connect(cs[i].conn, target, 0);
48         cs[i].rs = ZOOM_connection_search_pqf(cs[i].conn, "the");
49         cs[i].next_to_show = 0;
50         cs[i].next_to_fetch = 0;
51     }
52
53     while ((i = ZOOM_event(n, zconn)) != 0) {
54         struct conn_and_state *csp = &cs[i-1];
55         int ev = ZOOM_connection_last_event(csp->conn);
56         int errcode;
57         const char *errmsg;
58         const char *addinfo;
59
60         yaz_log(yaz_log_module_level("pod"),
61                 "connection %d: event %d", i-1, ev);
62
63         errcode = ZOOM_connection_error(csp->conn, &errmsg, &addinfo);
64         if (errcode != 0) {
65             fprintf(stderr, "error %d (%s) [%s]\n", errcode, errmsg, addinfo);
66             return 2;
67         }
68
69         if (ev == ZOOM_EVENT_RECV_SEARCH) {
70             completed_search(csp);
71         } else if (ev == ZOOM_EVENT_RECV_RECORD) {
72             got_record(csp);
73         }
74     }
75
76     return 0;
77 }
78
79
80 static void completed_search(struct conn_and_state *csp) {
81     const char *host = ZOOM_connection_option_get(csp->conn, "host");
82
83     printf("%s: found %d records\n", host, ZOOM_resultset_size(csp->rs));
84     request_records(csp, 2);
85 }
86
87
88 static void got_record(struct conn_and_state *csp) {
89     const char *host = ZOOM_connection_option_get(csp->conn, "host");
90     int i;
91     ZOOM_record rec;
92
93     assert(csp->next_to_show < csp->next_to_fetch);
94     i = csp->next_to_show++;
95     rec = ZOOM_resultset_record(csp->rs, i);
96     printf("%s: record %d is %s\n", host, i, render_record(rec));
97     if (i == csp->next_to_fetch-1) {
98         request_records(csp, 3);
99     }
100 }
101
102
103 static void request_records(struct conn_and_state *csp, int count) {
104     const char *host = ZOOM_connection_option_get(csp->conn, "host");
105     int i = csp->next_to_fetch;
106
107     yaz_log(yaz_log_module_level("appl"),
108             "requesting %d records from %d for %s", count, i, host);
109
110     ZOOM_resultset_records(csp->rs, (ZOOM_record*) 0, i, count);
111     csp->next_to_fetch += count;
112 }
113
114
115 static const char *render_record(ZOOM_record rec) {
116     int len;
117
118     if (rec == 0)
119         return "undefined";
120     else 
121         return ZOOM_record_get(rec, "render", &len);
122 }