98f797c0b4bdea76286989f1c676d6aafe401b58
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /* $Id: zoomtst3.c,v 1.13 2007-01-10 13:25:46 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         int peek = ZOOM_connection_peek_event(z[i-1]);
72         printf ("no = %d peek = %d event = %d\n", i-1,
73                 peek,
74                 ZOOM_connection_last_event(z[i-1]));
75     }
76     
77     /* no more to be done. Inspect results */
78     for (i = 0; i<no; i++)
79     {
80         int error;
81         const char *errmsg, *addinfo;
82         const char *tname = (same_target ? argv[2] : argv[1+i]);
83         /* display errors if any */
84         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
85             fprintf (stderr, "%s error: %s (%d) %s\n", tname, errmsg,
86                      error, addinfo);
87         else
88         {
89             /* OK, no major errors. Look at the result count */
90             int pos;
91             printf ("%s: %ld hits\n", tname, (long) ZOOM_resultset_size(r[i]));
92             /* go through all records at target */
93             for (pos = 0; pos < 10; pos++)
94             {
95                 int len; /* length of buffer rec */
96                 const char *rec =
97                     ZOOM_record_get (
98                         ZOOM_resultset_record (r[i], pos), "render", &len);
99                 /* if rec is non-null, we got a record for display */
100                 if (rec)
101                 {
102                     printf ("%d\n", pos+1);
103                     if (rec)
104                         fwrite (rec, 1, len, stdout);
105                     printf ("\n");
106                 }
107             }
108         }
109     }
110     /* destroy and exit */
111     for (i = 0; i<no; i++)
112     {
113         ZOOM_resultset_destroy (r[i]);
114         ZOOM_connection_destroy (z[i]);
115     }
116     ZOOM_options_destroy(o);
117     exit (0);
118 }
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * indent-tabs-mode: nil
123  * End:
124  * vim: shiftwidth=4 tabstop=8 expandtab
125  */
126