942e2a95ef1735ffed708db9d896eb9ccc92d280
[yaz-moved-to-github.git] / zoom / zoomtst11.c
1 /* $Id: zoomtst11.c,v 1.2 2007-03-07 13:12:42 adam Exp $  */
2
3 /** \file zoomtst11.c
4     \brief Asynchronous single-target which tests event/error handling
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <yaz/zoom.h>
11
12 void probe_package(ZOOM_connection z)
13 {
14     int i;
15     for (i = 1; i<10; i++)
16     {
17         ZOOM_package pkg = ZOOM_connection_package(z, 0);
18         ZOOM_package_option_set(pkg, "action", "recordInsert");
19         ZOOM_package_option_set(pkg, "record", "1234");
20         ZOOM_package_send(pkg, "update");
21     }
22 }
23
24 void probe_search(ZOOM_connection z, int start, int error)
25 {
26     char pqf_str[100];
27     ZOOM_resultset set;
28
29     /* provoke error with yaz-ztest */
30     if (error)
31         ZOOM_connection_option_set(z, "databaseName", "x");
32
33     sprintf(pqf_str, "@attr 1=%d water", start);
34     printf("sending search %s\n", pqf_str);
35     set = ZOOM_connection_search_pqf (z, pqf_str);
36     ZOOM_resultset_destroy(set);
37
38     /* restore database */
39     if (error)
40         ZOOM_connection_option_set(z, "databaseName", "Default");
41 }
42
43 int main(int argc, char **argv)
44 {
45     ZOOM_connection z;
46     int error;
47     int use = 0;
48     const char *errmsg, *addinfo, *diagset;
49
50     if (argc < 2)
51     {
52         fprintf (stderr, "usage:\n%s target\n", *argv);
53         fprintf (stderr,
54                  "Verify: asynchronous single-target client\n");
55         exit (1);
56     }
57
58     /* create connection (don't connect yet) */
59     z = ZOOM_connection_create(0);
60
61     /* option: set sru/get operation (only applicable if http: is used) */
62     ZOOM_connection_option_set (z, "sru", "post");
63
64     /* option: set async operation */
65     ZOOM_connection_option_set (z, "async", "1");
66
67     /* connect to target and initialize */
68     ZOOM_connection_connect (z, argv[1], 0);
69
70     probe_search(z, use, 1);
71
72     /* block here: only one connection */
73     while (ZOOM_event (1, &z))
74     {
75         int ev = ZOOM_connection_last_event(z);
76         int idle = ZOOM_connection_is_idle(z);
77
78         /* see if any error occurred */
79         if ((error = ZOOM_connection_error_x(z, &errmsg, &addinfo, &diagset)))
80         {
81             fprintf (stderr, "Error: %s: %s (%d) %s\n", diagset, errmsg, error,
82                      addinfo);
83
84         }
85         if (ev == ZOOM_EVENT_RECV_SEARCH)
86         {
87             if (error == 0)
88                 printf ("Search OK\n");
89             printf("idle=%d\n", idle);
90         }
91         if (idle)
92         {
93             ZOOM_connection_connect(z, 0, 0); /* allow reconnect */
94             
95             if (++use <= 10)
96             {
97                 probe_search(z, use, use&1);
98             }
99             printf("Press enter\n");
100             getchar();
101         }
102     }
103     ZOOM_connection_destroy (z);
104     exit (0);
105 }
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * indent-tabs-mode: nil
110  * End:
111  * vim: shiftwidth=4 tabstop=8 expandtab
112  */
113