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