2 * $Id: test-zoom-c.c,v 1.1 2006-07-19 16:29:51 mike Exp $
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.
16 struct conn_and_state {
19 int next_to_show, next_to_fetch;
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);
29 int main (int argc, char *argv[]) {
30 struct conn_and_state cs[100];
31 ZOOM_connection zconn[100];
35 fprintf(stderr, "Usage: %s <target1> [<target2> ...]\n", argv[0]);
39 yaz_log_mask_str("appl");
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;
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);
60 yaz_log(yaz_log_module_level("pod"),
61 "connection %d: event %d", i-1, ev);
63 errcode = ZOOM_connection_error(csp->conn, &errmsg, &addinfo);
65 fprintf(stderr, "error %d (%s) [%s]\n", errcode, errmsg, addinfo);
69 if (ev == ZOOM_EVENT_RECV_SEARCH) {
70 completed_search(csp);
71 } else if (ev == ZOOM_EVENT_RECV_RECORD) {
80 static void completed_search(struct conn_and_state *csp) {
81 const char *host = ZOOM_connection_option_get(csp->conn, "host");
83 printf("%s: found %d records\n", host, ZOOM_resultset_size(csp->rs));
84 request_records(csp, 2);
88 static void got_record(struct conn_and_state *csp) {
89 const char *host = ZOOM_connection_option_get(csp->conn, "host");
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);
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;
107 yaz_log(yaz_log_module_level("appl"),
108 "requesting %d records from %d for %s", count, i, host);
110 ZOOM_resultset_records(csp->rs, (ZOOM_record*) 0, i, count);
111 csp->next_to_fetch += count;
115 static const char *render_record(ZOOM_record rec) {
121 return ZOOM_record_get(rec, "render", &len);