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