b52d1d1e2b45e2b54a261d10646501ecd6fd8ee4
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /*
2  * $Id: zoomtst3.c,v 1.2 2001-10-24 12:24:43 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     Z3950_connection z[500]; /* allow at most 500 connections */
20     Z3950_resultset r[500];  /* and result sets .. */
21     Z3950_options o = Z3950_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     Z3950_options_set (o, "async", "1");
34
35     /* get first 10 records of result set (using piggyback) */
36     Z3950_options_set (o, "count", "10");
37
38     /* preferred record syntax */
39     Z3950_options_set (o, "preferredRecordSyntax", "usmarc");
40     Z3950_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] = Z3950_connection_create (o);
47
48         /* connect and init */
49         Z3950_connection_connect (z[i], argv[1+i], 0);
50     }
51     /* search all */
52     for (i = 0; i<no; i++)
53         r[i] = Z3950_connection_search_pqf (z[i], argv[argc-1]);
54
55     /* network I/O. pass number of connections and array of connections */
56     while (Z3950_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 = Z3950_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], Z3950_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                     Z3950_resultset_get (r[i], pos, "render", &len);
79                 /* if rec is non-null, we got a record for display */
80                 if (rec)
81                 {
82                     printf ("%d\n", pos+1);
83                     if (rec)
84                         fwrite (rec, 1, len, stdout);
85                     putchar ('\n');
86                 }
87             }
88         }
89     }
90     /* destroy and exit */
91     for (i = 0; i<no; i++)
92     {
93         Z3950_resultset_destroy (r[i]);
94         Z3950_connection_destroy (z[i]);
95     }
96     Z3950_options_destroy(o);
97     exit (0);
98 }