a2812f9161e49bcd3aea28412bbdf0a7d088f2ec
[yazpp-moved-to-github.git] / src / yaz-z-server.cpp
1 /*
2  * Copyright (c) 2000-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-server.cpp,v $
6  * Revision 1.9  2001-03-29 15:14:26  adam
7  * Minor updates.
8  *
9  * Revision 1.8  2001/03/27 14:47:45  adam
10  * New server facility scheme.
11  *
12  * Revision 1.7  2001/03/26 14:43:49  adam
13  * New threaded PDU association.
14  *
15  * Revision 1.6  2001/01/29 11:18:24  adam
16  * Server sets OPTIONS search and present.
17  *
18  * Revision 1.5  2000/10/24 12:29:57  adam
19  * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
20  * two Yaz_Proxy's (fatal).
21  *
22  * Revision 1.4  2000/10/11 11:58:17  adam
23  * Moved header files to include/yaz++. Switched to libtool and automake.
24  * Configure script creates yaz++-config script.
25  *
26  * Revision 1.3  2000/09/21 21:43:20  adam
27  * Better high-level server API.
28  *
29  * Revision 1.2  2000/09/12 12:09:53  adam
30  * More work on high-level server.
31  *
32  * Revision 1.1  2000/09/08 10:23:42  adam
33  * Added skeleton of yaz-z-server.
34  *
35  */
36
37 #include <yaz/log.h>
38 #include <yaz++/yaz-z-server.h>
39
40 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
41     : Yaz_Z_Assoc(the_PDU_Observable)
42 {
43     m_facilities = 0;
44 }
45
46 Yaz_Z_Server::~Yaz_Z_Server()
47 {
48     facility_reset();
49 }
50
51 void Yaz_Z_Server::facility_reset ()
52 {
53     Yaz_Z_Server_Facility_Info *p = m_facilities;
54     while (p)
55     {
56         Yaz_Z_Server_Facility_Info *p_next = p->m_next;
57
58         delete [] p->m_name;
59         delete p;
60         p = p_next;
61     }
62     m_facilities = 0;
63 }
64
65 void Yaz_Z_Server::facility_add(IYaz_Server_Facility *facility,
66                                 const char *name)
67 {
68     Yaz_Z_Server_Facility_Info **p = &m_facilities;
69     while (*p)
70         p = &(*p)->m_next;
71
72     *p = new Yaz_Z_Server_Facility_Info;
73
74     (*p)->m_next = 0;
75     (*p)->m_name = new char [strlen(name)+1];
76     strcpy ((*p)->m_name, name);
77     (*p)->m_facility = facility;
78 }
79
80 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
81 {   
82     Yaz_Z_Server_Facility_Info *f = m_facilities;
83     
84     if (apdu_request->which == Z_APDU_initRequest)
85     {
86         Z_APDU *apdu_response = create_Z_PDU(Z_APDU_initResponse);
87
88         Z_InitRequest *req = apdu_request->u.initRequest;
89         Z_InitResponse *resp = apdu_response->u.initResponse;
90         
91         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
92         {
93             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
94         }
95         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
96         {
97             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
98         }
99         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
100         {
101             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
102         }
103         while (f)
104         {
105             f->m_facility->init(this, req, resp);
106             f = f->m_next;
107         }
108         send_Z_PDU(apdu_response);
109     }
110     else
111     {
112         f = m_facilities;
113         int taken = 0;
114         while (f)
115         {
116             taken = f->m_facility->recv(this, apdu_request);
117             if (taken)
118                 break;
119             f = f->m_next;
120         }
121         if (!taken)
122         {
123             yaz_log (LOG_LOG, "got request = %d", apdu_request->which);
124             delete this;
125         }
126     }
127 }