ZOOM changes.
[yaz-moved-to-github.git] / zoom / zoomtst6.c
1 /*
2  * $Id: zoomtst6.c,v 1.3 2001-11-06 17:05:19 adam Exp $
3  *
4  * Asynchronous multi-target client doing two searches
5  */
6
7 #include <stdio.h>
8 #include <yaz/nmem.h>
9 #include <yaz/xmalloc.h>
10
11 #include <yaz/zoom.h>
12
13 static void display_records (const char *tname, Z3950_resultset r)
14 {
15     /* OK, no major errors. Look at the result count */
16     int pos;
17     printf ("%s: %d hits\n", tname, Z3950_resultset_size(r));
18     /* go through all records at target */
19     for (pos = 0; pos < 20; pos++)
20     {
21         /* get database for record and record itself at pos */
22         const char *db = Z3950_resultset_get (r, pos, "database", 0);
23         int len;
24         const char *rec = Z3950_resultset_get (r, pos, "render", &len);
25         /* if rec is non-null, we got a record for display */
26         if (rec)
27         {
28             printf ("%d %s\n", pos+1, (db ? db : "unknown"));
29             if (rec)
30                 fwrite (rec, 1, len, stdout);
31             putchar ('\n');
32         }
33     }
34 }
35
36 int main(int argc, char **argv)
37 {
38     int i;
39     int no = argc-3;
40     Z3950_connection z[500];  /* allow at most 500 connections */
41     Z3950_resultset r1[500];  /* and result sets .. */
42     Z3950_resultset r2[500];  /* and result sets .. */
43     Z3950_query q;
44     Z3950_options o;
45
46     o = Z3950_options_create ();
47     if (argc < 4)
48     {
49         fprintf (stderr, "usage:\n%s target1 .. targetN query1 query2\n",
50                  *argv);
51         exit (1);
52     }
53     if (no > 500)
54         no = 500;
55
56     Z3950_options_set (o, "async", "1");
57
58     /* get 3 (at most) records from beginning */
59     Z3950_options_set (o, "count", "3");
60
61     Z3950_options_set (o, "preferredRecordSyntax", "sutrs");
62     Z3950_options_set (o, "elementSetName", "B");
63
64     /* create query */
65     q = Z3950_query_create ();
66     if (Z3950_query_prefix (q, argv[argc-2]))
67     {
68         printf ("bad PQF: %s\n", argv[argc-2]);
69         exit (2);
70     }
71     /* connect - and search all */
72     for (i = 0; i<no; i++)
73     {
74         z[i] = Z3950_connection_create (o);
75         Z3950_connection_connect (z[i], argv[i+1], 0);
76         r1[i] = Z3950_connection_search (z[i], q);
77     }
78     if (Z3950_query_prefix (q, argv[argc-1]))
79     {
80         printf ("bad sort spec: %s\n", argv[argc-1]);
81         exit (2);
82     }
83     /* queue second search */
84     for (i = 0; i<no; i++)
85         r2[i] = Z3950_connection_search (z[i], q);
86
87     /* network I/O */
88     while (Z3950_event (no, z))
89         ;
90
91     /* handle errors */
92     for (i = 0; i<no; i++)
93     {
94         int error;
95         const char *errmsg, *addinfo;
96         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
97             fprintf (stderr, "%s error: %s (%d) %s\n",
98                      Z3950_connection_host(z[i]),
99                      errmsg, error, addinfo);
100         else
101         {
102             display_records (Z3950_connection_host(z[i]), r1[i]);
103             display_records (Z3950_connection_host(z[i]), r2[i]);
104         }
105     }
106     /* destroy stuff and exit */
107     Z3950_query_destroy (q);
108     for (i = 0; i<no; i++)
109     {
110         Z3950_connection_destroy (z[i]);
111         Z3950_resultset_destroy (r1[i]);
112         Z3950_resultset_destroy (r2[i]);
113     }
114     Z3950_options_destroy(o);
115     exit (0);
116 }