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