Scan for ZOOM.
[yaz-moved-to-github.git] / zoom / zoomtst8.c
1 /*
2  * $Id: zoomtst8.c,v 1.1 2001-12-30 22:21:11 adam Exp $
3  *
4  * 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     /* search all */
44     for (i = 0; i<no; i++)
45         s[i] = ZOOM_connection_scan (z[i], argv[argc-1]);
46
47     /* network I/O. pass number of connections and array of connections */
48     while (ZOOM_event (no, z))
49         ;
50
51     for (i = 0; i<no; i++)
52     {
53         int error;
54         const char *errmsg, *addinfo;
55         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
56             fprintf (stderr, "%s error: %s (%d) %s\n",
57                      ZOOM_connection_option_get(z[i], "host"),
58                      errmsg, error, addinfo);
59         else
60         {
61             int j;
62             printf ("%s\n", ZOOM_connection_option_get(z[i], "host"));
63             for (j = 0; j<ZOOM_scanset_size (s[i]); j++)
64             {
65                 int occur, len;
66                 const char *term;
67                 term = ZOOM_scanset_term (s[i], j, &occur, &len);
68                 if (term)
69                     printf ("%d %.*s %d\n", j, len, term, occur);
70                 
71             }
72         }
73     }
74
75     /* destroy and exit */
76     for (i = 0; i<no; i++)
77     {
78         ZOOM_scanset_destroy (s[i]);
79         ZOOM_connection_destroy (z[i]);
80     }
81     ZOOM_options_destroy(o);
82     exit (0);
83 }