Implemented yp2::odr which is a noncopyable wrapper for YAZ' ODR.
[metaproxy-moved-to-github.git] / src / filter_z3950_client.cpp
1 /* $Id: filter_z3950_client.cpp,v 1.8 2005-10-30 17:13:36 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include <map>
10 #include "filter.hpp"
11 #include "router.hpp"
12 #include "package.hpp"
13
14 #include <boost/thread/mutex.hpp>
15 #include <boost/thread/condition.hpp>
16
17 #include "util.hpp"
18 #include "filter_z3950_client.hpp"
19
20 #include <yaz/zgdu.h>
21 #include <yaz/log.h>
22 #include <yaz/otherinfo.h>
23 #include <yaz/diagbib1.h>
24
25 #include <yaz++/socket-manager.h>
26 #include <yaz++/pdu-assoc.h>
27 #include <yaz++/z-assoc.h>
28
29 #include <iostream>
30
31 namespace yf = yp2::filter;
32
33 namespace yp2 {
34     namespace filter {
35         class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
36             friend class Rep;
37         public:
38             Assoc(yazpp_1::SocketManager *socket_manager,
39                   yazpp_1::IPDU_Observable *PDU_Observable,
40                   std::string host);
41             ~Assoc();
42             void connectNotify();
43             void failNotify();
44             void timeoutNotify();
45             void recv_GDU(Z_GDU *gdu, int len);
46             yazpp_1::IPDU_Observer* sessionNotify(
47                 yazpp_1::IPDU_Observable *the_PDU_Observable,
48                 int fd);
49         private:
50             // yp2::Session m_session_id;
51             yazpp_1::SocketManager *m_socket_manager;
52             yazpp_1::IPDU_Observable *m_PDU_Observable;
53             Package *m_package;
54             bool m_in_use;
55             bool m_waiting;
56             bool m_connected;
57             std::string m_host;
58         };
59
60         class Z3950Client::Rep {
61         public:
62             boost::mutex m_mutex;
63             boost::condition m_cond_session_ready;
64             std::map<yp2::Session,Z3950Client::Assoc *> m_clients;
65             Z3950Client::Assoc *get_assoc(Package &package);
66             void send_and_receive(Package &package,
67                                   yf::Z3950Client::Assoc *c);
68             void release_assoc(Package &package);
69         };
70     }
71 }
72
73
74 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
75                               yazpp_1::IPDU_Observable *PDU_Observable,
76                               std::string host)
77     :  Z_Assoc(PDU_Observable),
78        m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
79        m_package(0), m_in_use(true), m_waiting(false), m_connected(false),
80        m_host(host)
81 {
82     // std::cout << "create assoc " << this << "\n";
83 }
84
85 yf::Z3950Client::Assoc::~Assoc()
86 {
87     // std::cout << "destroy assoc " << this << "\n";
88 }
89
90 void yf::Z3950Client::Assoc::connectNotify()
91 {
92     m_waiting = false;
93
94     m_connected = true;
95 }
96
97 void yf::Z3950Client::Assoc::failNotify()
98 {
99     m_waiting = false;
100
101     yp2::odr odr;
102
103     Z_APDU *apdu = zget_APDU(odr, Z_APDU_close);
104
105     *apdu->u.close->closeReason = Z_Close_peerAbort;
106
107     if (m_package)
108     {
109         m_package->response() = apdu;
110         m_package->session().close();
111     }
112 }
113
114 void yf::Z3950Client::Assoc::timeoutNotify()
115 {
116     m_waiting = false;
117
118     yp2::odr odr;
119
120     Z_APDU *apdu = zget_APDU(odr, Z_APDU_close);
121
122     *apdu->u.close->closeReason = Z_Close_lackOfActivity;
123
124     if (m_package)
125     {
126         m_package->response() = apdu;
127         m_package->session().close();
128     }
129 }
130
131 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
132 {
133     m_waiting = false;
134
135     if (m_package)
136         m_package->response() = gdu;
137 }
138
139 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
140     yazpp_1::IPDU_Observable *the_PDU_Observable,
141     int fd)
142 {
143     return 0;
144 }
145
146
147 yf::Z3950Client::Z3950Client() :  m_p(new yf::Z3950Client::Rep)
148 {
149 }
150
151 yf::Z3950Client::~Z3950Client() {
152 }
153
154 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) 
155 {
156     // only one thread messes with the clients list at a time
157     boost::mutex::scoped_lock lock(m_mutex);
158
159     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
160     
161     while(true)
162     {
163         it = m_clients.find(package.session());
164         if (it == m_clients.end())
165             break;
166         
167         if (!it->second->m_in_use)
168         {
169             it->second->m_in_use = true;
170             return it->second;
171         }
172         m_cond_session_ready.wait(lock);
173     }
174
175     // only deal with Z39.50
176     Z_GDU *gdu = package.request().get();
177
178     if (!gdu || gdu->which != Z_GDU_Z3950)
179     {
180         package.move();
181         return 0;
182     }
183     Z_APDU *apdu = gdu->u.z3950;
184
185     // new Z39.50 session ..
186
187     // check that it is init. If not, close
188     if (apdu->which != Z_APDU_initRequest)
189     {
190         yp2::odr odr;
191         Z_APDU *apdu = zget_APDU(odr, Z_APDU_close);
192         
193         *apdu->u.close->closeReason = Z_Close_protocolError;
194         apdu->u.close->diagnosticInformation =
195             odr_strdup(odr, "no init request for session");
196
197         package.response() = apdu;
198         
199         package.session().close();
200         return 0;
201     }
202     // check virtual host
203     const char *vhost =
204         yaz_oi_get_string_oidval(&apdu->u.initRequest->otherInfo,
205                                  VAL_PROXY, 1, 0);
206     if (!vhost)
207     {
208         yp2::odr odr;
209         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
210         
211         apdu->u.initResponse->userInformationField =
212             zget_init_diagnostics(odr, 
213                                   YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
214                                   "Virtual host not given");
215         package.response() = apdu;
216             
217         package.session().close();
218         return 0;
219     }
220     
221     yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
222     yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
223     yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as, vhost);
224     m_clients[package.session()] = as;
225     return as;
226 }
227
228 void yf::Z3950Client::Rep::send_and_receive(Package &package,
229                                             yf::Z3950Client::Assoc *c)
230 {
231     Z_GDU *gdu = package.request().get();
232
233     if (!gdu || gdu->which != Z_GDU_Z3950)
234         return;
235
236     c->m_package = &package;
237     c->m_waiting = true;
238     if (!c->m_connected)
239     {
240         c->client(c->m_host.c_str());
241
242         while (c->m_waiting && c->m_socket_manager->processEvent() > 0)
243             ;
244     }
245     if (!c->m_connected)
246     {
247         return;
248     }
249
250     // prepare response
251     c->m_waiting = true;
252     
253     // relay the package  ..
254     int len;
255     c->send_GDU(gdu, &len);
256     
257     while (c->m_waiting && c->m_socket_manager->processEvent() > 0)
258         ;
259 }
260
261 void yf::Z3950Client::Rep::release_assoc(Package &package)
262 {
263     boost::mutex::scoped_lock lock(m_mutex);
264     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
265     
266     it = m_clients.find(package.session());
267     if (it != m_clients.end())
268     {
269         if (package.session().is_closed())
270         {
271             // the Z_Assoc and PDU_Assoc must be destroyed before
272             // the socket manager.. so pull that out.. first..
273             yazpp_1::SocketManager *s = it->second->m_socket_manager;
274             delete it->second;  // destroy Z_Assoc
275             delete s;    // then manager
276             m_clients.erase(it);
277         }
278         else
279         {
280             it->second->m_in_use = false;
281         }
282         m_cond_session_ready.notify_all();
283     }
284 }
285
286 void yf::Z3950Client::process(Package &package) const
287 {
288     yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
289     if (c)
290     {
291         m_p->send_and_receive(package, c);
292     }
293     m_p->release_assoc(package);
294 }
295
296
297 /*
298  * Local variables:
299  * c-basic-offset: 4
300  * indent-tabs-mode: nil
301  * c-file-style: "stroustrup"
302  * End:
303  * vim: shiftwidth=4 tabstop=8 expandtab
304  */