Check for errors at all times in this event test
[yaz-moved-to-github.git] / zoom / zoomtst8.c
1 /* $Id: zoomtst8.c,v 1.6 2007-01-03 08:42:17 adam Exp $  */
2
3 /** \file zoomtst8.c
4     \brief Asynchronous multi-target client doing scan
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include <yaz/nmem.h>
11 #include <yaz/xmalloc.h>
12 #include <yaz/zoom.h>
13
14 int main(int argc, char **argv)
15 {
16     int i;
17     int no = argc-2;
18     ZOOM_connection z[500]; /* allow at most 500 connections */
19     ZOOM_scanset s[500];  /* and scan sets .. */
20     ZOOM_options o = ZOOM_options_create ();
21
22     if (argc < 3)
23     {
24         fprintf (stderr, "usage:\n%s target1 target2 ... targetN scan\n",
25                  *argv);
26         exit (1);
27     }
28     if (no > 500)
29         no = 500;
30
31     /* async mode */
32     ZOOM_options_set (o, "async", "1");
33
34     /* connect to all */
35     for (i = 0; i<no; i++)
36     {
37         /* create connection - pass options (they are the same for all) */
38         z[i] = ZOOM_connection_create (o);
39
40         /* connect and init */
41         ZOOM_connection_connect (z[i], argv[1+i], 0);
42         
43     }
44     /* scan all */
45     for (i = 0; i<no; i++)
46     {
47         /* set number of scan terms to be returned. */
48         ZOOM_connection_option_set (z[i], "number", "7");
49         /* and perform scan */
50         s[i] = ZOOM_connection_scan (z[i], argv[argc-1]);
51     }
52
53     /* network I/O. pass number of connections and array of connections */
54     while (ZOOM_event (no, z))
55         ;
56
57     for (i = 0; i<no; i++)
58     {
59         int error;
60         const char *errmsg, *addinfo;
61         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
62             fprintf (stderr, "%s error: %s (%d) %s\n",
63                      ZOOM_connection_option_get(z[i], "host"),
64                      errmsg, error, addinfo);
65         else
66         {
67             int j;
68             printf ("%s\n", ZOOM_connection_option_get(z[i], "host"));
69             for (j = 0; j<ZOOM_scanset_size (s[i]); j++)
70             {
71                 int occur, len;
72                 const char *term;
73                 term = ZOOM_scanset_term (s[i], j, &occur, &len);
74                 if (term)
75                     printf ("%d %.*s %d\n", j, len, term, occur);
76             }
77         }
78     }
79
80     /* destroy and exit */
81     for (i = 0; i<no; i++)
82     {
83         ZOOM_scanset_destroy (s[i]);
84         ZOOM_connection_destroy (z[i]);
85     }
86     ZOOM_options_destroy(o);
87     exit (0);
88 }
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * indent-tabs-mode: nil
93  * End:
94  * vim: shiftwidth=4 tabstop=8 expandtab
95  */
96