Allow this update test to read record data from standard input.
[yaz-moved-to-github.git] / zoom / zoomtst6.c
1 /* $Id: zoomtst6.c,v 1.12 2006-04-21 10:28:08 adam Exp $  */
2
3 /** \file zoomtst6.c
4     \brief Asynchronous multi-target client with 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, ZOOM_resultset r)
14 {
15     /* OK, no major errors. Look at the result count */
16     int pos;
17     printf ("%s: %ld hits\n", tname, (long) ZOOM_resultset_size(r));
18     /* go through all records at target */
19     for (pos = 0; pos < 4; pos++)
20     {
21         ZOOM_record rec = ZOOM_resultset_record (r, pos);
22         if (rec)
23         {
24             /* get database for record and record itself at pos */
25             const char *db = ZOOM_record_get (rec, "database", 0);
26             int len;
27             const char *render = ZOOM_record_get (rec, "render", &len);
28             /* if rec is non-null, we got a record for display */
29             if (rec)
30             {
31                 printf ("%d %s\n", pos+1, (db ? db : "unknown"));
32                 if (render)
33                     fwrite (render, 1, len, stdout);
34                 printf ("\n");
35             }
36         }
37     }
38 }
39
40 int main(int argc, char **argv)
41 {
42     int i;
43     int no = argc-3;
44     ZOOM_connection z[500];  /* allow at most 500 connections */
45     ZOOM_resultset r1[500];  /* and result sets .. */
46     ZOOM_resultset r2[500];  /* and result sets .. */
47     ZOOM_query q;
48     ZOOM_options o;
49
50     o = ZOOM_options_create ();
51     if (argc < 4)
52     {
53         fprintf (stderr, "usage:\n%s target1 .. targetN query1 query2\n",
54                  *argv);
55         exit (1);
56     }
57     if (no > 500)
58         no = 500;
59
60     ZOOM_options_set (o, "async", "1");
61
62     /* get 3 (at most) records from beginning */
63     ZOOM_options_set (o, "count", "3");
64
65     ZOOM_options_set (o, "preferredRecordSyntax", "sutrs");
66     ZOOM_options_set (o, "elementSetName", "B");
67
68     /* create query */
69     q = ZOOM_query_create ();
70     if (ZOOM_query_prefix (q, argv[argc-2]))
71     {
72         printf ("bad PQF: %s\n", argv[argc-2]);
73         exit (2);
74     }
75     /* connect - and search all */
76     for (i = 0; i<no; i++)
77     {
78         z[i] = ZOOM_connection_create (o);
79         ZOOM_connection_connect (z[i], argv[i+1], 0);
80         r1[i] = ZOOM_connection_search (z[i], q);
81     }
82     if (ZOOM_query_prefix (q, argv[argc-1]))
83     {
84         printf ("bad prefix query: %s\n", argv[argc-1]);
85         exit (2);
86     }
87     /* queue second search */
88     for (i = 0; i<no; i++)
89         r2[i] = ZOOM_connection_search (z[i], q);
90
91     /* fetch 5th record from first result set as well */
92     for (i = 0; i<no; i++)
93         ZOOM_resultset_records (r1[i], 0, 4, 1);
94
95     /* network I/O */
96     while (ZOOM_event (no, z))
97         ;
98
99     /* handle errors */
100     for (i = 0; i<no; i++)
101     {
102         int error;
103         const char *errmsg, *addinfo;
104         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
105             fprintf (stderr, "%s error: %s (%d) %s\n",
106                      ZOOM_connection_option_get(z[i], "host"),
107                      errmsg, error, addinfo);
108         else
109         {
110             display_records (ZOOM_connection_option_get(z[i], "host"), r1[i]);
111             display_records (ZOOM_connection_option_get(z[i], "host"), r2[i]);
112         }
113     }
114     /* destroy stuff and exit */
115     ZOOM_query_destroy (q);
116     for (i = 0; i<no; i++)
117     {
118         ZOOM_connection_destroy (z[i]);
119         ZOOM_resultset_destroy (r1[i]);
120         ZOOM_resultset_destroy (r2[i]);
121     }
122     ZOOM_options_destroy(o);
123     exit (0);
124 }
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132