Prefix ZOOM_ instead of Z3950_. Documentation updates.
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /*
2  * $Id: zoomtst3.c,v 1.4 2001-11-18 21:14:23 adam Exp $
3  *
4  * Asynchronous multi-target client doing search and piggyback retrieval
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <yaz/xmalloc.h>
12
13 #include <yaz/zoom.h>
14
15 int main(int argc, char **argv)
16 {
17     int i;
18     int no = argc-2;
19     ZOOM_connection z[500]; /* allow at most 500 connections */
20     ZOOM_resultset r[500];  /* and result sets .. */
21     ZOOM_options o = ZOOM_options_create ();
22
23     if (argc < 3)
24     {
25         fprintf (stderr, "usage:\n%s target1 target2 ... targetN query\n",
26                  *argv);
27         exit (1);
28     }
29     if (no > 500)
30         no = 500;
31
32     /* async mode */
33     ZOOM_options_set (o, "async", "1");
34
35     /* get first 10 records of result set (using piggyback) */
36     ZOOM_options_set (o, "count", "10");
37
38     /* preferred record syntax */
39     ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
40     ZOOM_options_set (o, "elementSetName", "F");
41
42     /* connect to all */
43     for (i = 0; i<no; i++)
44     {
45         /* create connection - pass options (they are the same for all) */
46         z[i] = ZOOM_connection_create (o);
47
48         /* connect and init */
49         ZOOM_connection_connect (z[i], argv[1+i], 0);
50     }
51     /* search all */
52     for (i = 0; i<no; i++)
53         r[i] = ZOOM_connection_search_pqf (z[i], argv[argc-1]);
54
55     /* network I/O. pass number of connections and array of connections */
56     while (ZOOM_event (no, z))
57         ;
58     
59     /* no more to be done. Inspect results */
60     for (i = 0; i<no; i++)
61     {
62         int error;
63         const char *errmsg, *addinfo;
64         /* display errors if any */
65         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
66             fprintf (stderr, "%s error: %s (%d) %s\n", argv[i+1], errmsg,
67                      error, addinfo);
68         else
69         {
70             /* OK, no major errors. Look at the result count */
71             int pos;
72             printf ("%s: %d hits\n", argv[i+1], ZOOM_resultset_size(r[i]));
73             /* go through all records at target */
74             for (pos = 0; pos < 10; pos++)
75             {
76                 int len; /* length of buffer rec */
77                 const char *rec =
78                     ZOOM_record_get (
79                         ZOOM_resultset_record (r[i], pos), "render", &len);
80                 /* if rec is non-null, we got a record for display */
81                 if (rec)
82                 {
83                     printf ("%d\n", pos+1);
84                     if (rec)
85                         fwrite (rec, 1, len, stdout);
86                     putchar ('\n');
87                 }
88             }
89         }
90     }
91     /* destroy and exit */
92     for (i = 0; i<no; i++)
93     {
94         ZOOM_resultset_destroy (r[i]);
95         ZOOM_connection_destroy (z[i]);
96     }
97     ZOOM_options_destroy(o);
98     exit (0);
99 }