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