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.
14 struct conn_and_state {
17 int next_to_show, next_to_fetch;
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);
26 int main (int argc, char *argv[]) {
28 struct conn_and_state cs[100];
29 ZOOM_connection zconn[100];
33 fprintf(stderr, "Usage: %s <target1> [<target2> ...]\n", argv[0]);
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");
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;
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);
59 yaz_log(yaz_log_module_level("pod"),
60 "connection %d: event %d", i-1, ev);
62 errcode = ZOOM_connection_error(csp->conn, &errmsg, &addinfo);
64 fprintf(stderr, "error %d (%s) [%s]\n", errcode, errmsg, addinfo);
68 if (ev == ZOOM_EVENT_RECV_SEARCH) {
69 completed_search(csp);
70 } else if (ev == ZOOM_EVENT_RECV_RECORD) {
79 static void completed_search(struct conn_and_state *csp) {
80 const char *host = ZOOM_connection_option_get(csp->conn, "host");
82 printf("%s: found %d records\n", host, ZOOM_resultset_size(csp->rs));
83 request_records(csp, 2);
87 static void got_record(struct conn_and_state *csp) {
88 const char *host = ZOOM_connection_option_get(csp->conn, "host");
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);
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;
106 yaz_log(yaz_log_module_level("appl"),
107 "requesting %d records from %d for %s", count, i, host);
109 ZOOM_resultset_records(csp->rs, (ZOOM_record*) 0, i, count);
110 csp->next_to_fetch += count;