Thread pool for server
[yaz-moved-to-github.git] / test / tst_srv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/log.h>
10 #include <yaz/test.h>
11 #include <yaz/srv.h>
12 #include <yaz/odr.h>
13 #include <yaz/proto.h>
14
15 struct my_info {
16     int x;
17 };
18
19 static void *create_session(struct cs_session *ses)
20 {
21     struct my_info *my = xmalloc(sizeof(*my));
22     my->x = 42;
23     yaz_log(YLOG_LOG, "create_session");
24     return my;
25 }
26
27 static void gdu_handler(yaz_pkg_t pkg, void *user)
28 {
29     struct my_info *my = user;
30     Z_GDU **gdu = yaz_pkg_get_gdu(pkg);
31     ODR o = odr_createmem(ODR_PRINT);
32
33     yaz_log(YLOG_LOG, "gdu_handler");
34     YAZ_CHECK_EQ(my->x, 42);
35     
36     z_GDU(o, gdu, 0, 0);
37     odr_destroy(o);
38
39     if ((*gdu)->which == Z_GDU_Z3950)
40     {
41         ODR encode = odr_createmem(ODR_ENCODE);
42         Z_APDU *apdu_req = (*gdu)->u.z3950;
43         Z_APDU *apdu_res = 0;
44         int must_close = 0;
45
46         if (apdu_req->which == Z_APDU_close)
47         {
48             apdu_res = zget_APDU(encode, Z_APDU_close);
49             *apdu_res->u.close->closeReason = Z_Close_finished;
50             must_close = 1;
51         }
52         else if (apdu_req->which == Z_APDU_initRequest)
53         {
54             apdu_res = zget_APDU(encode, Z_APDU_initResponse);
55         }
56         else
57         {
58             apdu_res = zget_APDU(encode, Z_APDU_close);
59
60             *apdu_res->u.close->closeReason = Z_Close_unspecified;
61             must_close = 1;
62         }
63         if (apdu_res)
64         {
65             yaz_pkg_t pkg_res = yaz_pkg_create(pkg);
66             *yaz_pkg_get_gdu(pkg) = zget_wrap_APDU(encode, apdu_res);
67             yaz_pkg_send(pkg_res);
68         }
69         if (must_close)
70             yaz_pkg_close(pkg);
71         yaz_pkg_stop_server(pkg);
72     }
73 }
74
75 static void tst_srv(void)
76 {
77     const char *listeners[] = {"unix:socket", 0};
78
79     yaz_srv_t srv = yaz_srv_create(listeners);
80     YAZ_CHECK(srv);
81     if (!srv)
82         return;
83
84     yaz_srv_run(srv, create_session, gdu_handler);
85     yaz_srv_destroy(srv);
86 }
87
88 int main (int argc, char **argv)
89 {
90     YAZ_CHECK_INIT(argc, argv);
91     YAZ_CHECK_LOG();
92     /* tst_srv(); */
93     YAZ_CHECK_TERM;
94 }
95
96 /*
97  * Local variables:
98  * c-basic-offset: 4
99  * c-file-style: "Stroustrup"
100  * indent-tabs-mode: nil
101  * End:
102  * vim: shiftwidth=4 tabstop=8 expandtab
103  */
104