Client prevents "present request out of range".
[yaz-moved-to-github.git] / zoom / zoomtst6.c
1 /*
2  * $Id: zoomtst6.c,v 1.5 2001-11-15 21:59:40 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 < 2; pos++)
20     {
21         Z3950_record rec = Z3950_resultset_record (r, pos);
22
23         /* get database for record and record itself at pos */
24         const char *db = Z3950_record_get (rec, "database", 0);
25         int len;
26         const char *render = Z3950_record_get (rec, "render", &len);
27         /* if rec is non-null, we got a record for display */
28         if (rec)
29         {
30             printf ("%d %s\n", pos+1, (db ? db : "unknown"));
31             if (render)
32                 fwrite (render, 1, len, stdout);
33             putchar ('\n');
34         }
35     }
36 }
37
38 int main(int argc, char **argv)
39 {
40     int i;
41     int no = argc-3;
42     Z3950_connection z[500];  /* allow at most 500 connections */
43     Z3950_resultset r1[500];  /* and result sets .. */
44     Z3950_resultset r2[500];  /* and result sets .. */
45     Z3950_query q;
46     Z3950_options o;
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     q = Z3950_query_create ();
68     if (Z3950_query_prefix (q, 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], q);
79     }
80     if (Z3950_query_prefix (q, 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], q);
88
89
90     /* network I/O */
91     while (Z3950_event (no, z))
92         ;
93
94     for (i = 0; i<no; i++)
95         Z3950_resultset_records (r1[i], 0, 4, 1);
96
97     /* network I/O */
98     while (Z3950_event (no, z))
99         ;
100
101     /* handle errors */
102     for (i = 0; i<no; i++)
103     {
104         int error;
105         const char *errmsg, *addinfo;
106         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
107             fprintf (stderr, "%s error: %s (%d) %s\n",
108                      Z3950_connection_host(z[i]),
109                      errmsg, error, addinfo);
110         else
111         {
112             display_records (Z3950_connection_host(z[i]), r1[i]);
113             display_records (Z3950_connection_host(z[i]), r2[i]);
114         }
115     }
116     /* destroy stuff and exit */
117     Z3950_query_destroy (q);
118     for (i = 0; i<no; i++)
119     {
120         Z3950_connection_destroy (z[i]);
121         Z3950_resultset_destroy (r1[i]);
122         Z3950_resultset_destroy (r2[i]);
123     }
124     Z3950_options_destroy(o);
125     exit (0);
126 }