Fix compilation error
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /* $Id: zoomtst3.c,v 1.11 2006-04-21 10:28:08 adam Exp $  */
2
3 /** \file zoomtst3.c
4     \brief Asynchronous multi-target client
5     
6     Performs search and piggyback retrieval of records
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13
14 #include <yaz/xmalloc.h>
15
16 #include <yaz/zoom.h>
17
18 int main(int argc, char **argv)
19 {
20     int i;
21     int same_target = 0;
22     int no = argc-2;
23     ZOOM_connection z[500]; /* allow at most 500 connections */
24     ZOOM_resultset r[500];  /* and result sets .. */
25     ZOOM_options o = ZOOM_options_create ();
26
27     if (argc < 3)
28     {
29         fprintf (stderr, "usage:\n%s target1 target2 ... targetN query\n"
30                          "%s number target query\n", *argv, *argv);
31         exit (1);
32     }
33     if (argc == 4 && isdigit(argv[1][0]) && !strchr(argv[1],'.'))
34     {
35         no = atoi(argv[1]);
36         same_target = 1;
37     }
38
39     if (no > 500)
40         no = 500;
41
42     /* async mode */
43     ZOOM_options_set (o, "async", "1");
44
45     /* get first 10 records of result set (using piggyback) */
46     ZOOM_options_set (o, "count", "10");
47
48     /* preferred record syntax */
49     ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
50     ZOOM_options_set (o, "elementSetName", "F");
51
52     /* connect to all */
53     for (i = 0; i<no; i++)
54     {
55         /* create connection - pass options (they are the same for all) */
56         z[i] = ZOOM_connection_create (o);
57
58         /* connect and init */
59         if (same_target)
60             ZOOM_connection_connect (z[i], argv[2], 0);
61         else
62             ZOOM_connection_connect (z[i], argv[1+i], 0);
63     }
64     /* search all */
65     for (i = 0; i<no; i++)
66         r[i] = ZOOM_connection_search_pqf (z[i], argv[argc-1]);
67
68     /* network I/O. pass number of connections and array of connections */
69     while ((i = ZOOM_event (no, z)))
70     {
71         printf ("no = %d event = %d\n", i-1,
72                 ZOOM_connection_last_event(z[i-1]));
73     }
74     
75     /* no more to be done. Inspect results */
76     for (i = 0; i<no; i++)
77     {
78         int error;
79         const char *errmsg, *addinfo;
80         const char *tname = (same_target ? argv[2] : argv[1+i]);
81         /* display errors if any */
82         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
83             fprintf (stderr, "%s error: %s (%d) %s\n", tname, errmsg,
84                      error, addinfo);
85         else
86         {
87             /* OK, no major errors. Look at the result count */
88             int pos;
89             printf ("%s: %ld hits\n", tname, (long) ZOOM_resultset_size(r[i]));
90             /* go through all records at target */
91             for (pos = 0; pos < 10; pos++)
92             {
93                 int len; /* length of buffer rec */
94                 const char *rec =
95                     ZOOM_record_get (
96                         ZOOM_resultset_record (r[i], pos), "render", &len);
97                 /* if rec is non-null, we got a record for display */
98                 if (rec)
99                 {
100                     printf ("%d\n", pos+1);
101                     if (rec)
102                         fwrite (rec, 1, len, stdout);
103                     printf ("\n");
104                 }
105             }
106         }
107     }
108     /* destroy and exit */
109     for (i = 0; i<no; i++)
110     {
111         ZOOM_resultset_destroy (r[i]);
112         ZOOM_connection_destroy (z[i]);
113     }
114     ZOOM_options_destroy(o);
115     exit (0);
116 }
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * indent-tabs-mode: nil
121  * End:
122  * vim: shiftwidth=4 tabstop=8 expandtab
123  */
124