e3ea841a61764862fdcf4cd22b3b298b8068a0d4
[yaz-moved-to-github.git] / zoom / zoomtst6.c
1 /*
2  * $Id: zoomtst6.c,v 1.1 2001-10-23 21:00:20 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_search s;
44     Z3950_options o;
45
46     nmem_init ();
47
48     o = Z3950_options_create ();
49     if (argc < 4)
50     {
51         fprintf (stderr, "usage:\n%s target1 .. targetN query1 query2\n",
52                  *argv);
53         exit (1);
54     }
55     if (no > 500)
56         no = 500;
57
58     Z3950_options_set (o, "async", "1");
59
60     /* get 3 (at most) records from beginning */
61     Z3950_options_set (o, "count", "3");
62
63     Z3950_options_set (o, "preferredRecordSyntax", "sutrs");
64     Z3950_options_set (o, "elementSetName", "B");
65
66     /* create query */
67     s = Z3950_search_create ();
68     if (Z3950_search_prefix (s, argv[argc-2]))
69     {
70         printf ("bad PQF: %s\n", argv[argc-2]);
71         exit (2);
72     }
73     /* connect - and search all */
74     for (i = 0; i<no; i++)
75     {
76         z[i] = Z3950_connection_create (o);
77         Z3950_connection_connect (z[i], argv[i+1], 0);
78         r1[i] = Z3950_connection_search (z[i], s);
79     }
80     if (Z3950_search_prefix (s, argv[argc-1]))
81     {
82         printf ("bad sort spec: %s\n", argv[argc-1]);
83         exit (2);
84     }
85     /* queue second search */
86     for (i = 0; i<no; i++)
87         r2[i] = Z3950_connection_search (z[i], s);
88
89     /* network I/O */
90     while (Z3950_event (no, z))
91         ;
92
93     /* handle errors */
94     for (i = 0; i<no; i++)
95     {
96         int error;
97         const char *errmsg, *addinfo;
98         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
99             fprintf (stderr, "%s error: %s (%d) %s\n",
100                      Z3950_connection_host(z[i]),
101                      errmsg, error, addinfo);
102         else
103         {
104             display_records (Z3950_connection_host(z[i]), r1[i]);
105             display_records (Z3950_connection_host(z[i]), r2[i]);
106         }
107     }
108     /* destroy stuff and exit */
109     Z3950_search_destroy (s);
110     for (i = 0; i<no; i++)
111     {
112         Z3950_connection_destroy (z[i]);
113         Z3950_resultset_destroy (r1[i]);
114         Z3950_resultset_destroy (r2[i]);
115     }
116     Z3950_options_destroy(o);
117     nmem_exit ();
118     xmalloc_trav("");
119     exit (0);
120 }