Handle close of session in Z39.50 client.
[metaproxy-moved-to-github.git] / src / filter_z3950_client.cpp
1 /* $Id: filter_z3950_client.cpp,v 1.12 2006-01-02 14:33:42 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             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 using namespace yp2;
74
75 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
76                               yazpp_1::IPDU_Observable *PDU_Observable,
77                               std::string host)
78     :  Z_Assoc(PDU_Observable),
79        m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
80        m_package(0), m_in_use(true), m_waiting(false), 
81        m_destroyed(false), m_connected(false), m_host(host)
82 {
83     // std::cout << "create assoc " << this << "\n";
84 }
85
86 yf::Z3950Client::Assoc::~Assoc()
87 {
88     // std::cout << "destroy assoc " << this << "\n";
89 }
90
91 void yf::Z3950Client::Assoc::connectNotify()
92 {
93     m_waiting = false;
94
95     m_connected = true;
96 }
97
98 void yf::Z3950Client::Assoc::failNotify()
99 {
100     m_waiting = false;
101
102     yp2::odr odr;
103
104     if (m_package)
105     {
106         m_package->response() = odr.create_close(Z_Close_peerAbort, 0);
107         m_package->session().close();
108     }
109 }
110
111 void yf::Z3950Client::Assoc::timeoutNotify()
112 {
113     m_waiting = false;
114
115 #if 0
116     yp2::odr odr;
117
118     if (m_package)
119     {
120         m_package->response() = odr.create_close(Z_Close_lackOfActivity, 0);
121         m_package->session().close();
122     }
123 #endif
124 }
125
126 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
127 {
128     m_waiting = false;
129
130     if (m_package)
131         m_package->response() = gdu;
132 }
133
134 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
135     yazpp_1::IPDU_Observable *the_PDU_Observable,
136     int fd)
137 {
138     return 0;
139 }
140
141
142 yf::Z3950Client::Z3950Client() :  m_p(new yf::Z3950Client::Rep)
143 {
144 }
145
146 yf::Z3950Client::~Z3950Client() {
147 }
148
149 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) 
150 {
151     // only one thread messes with the clients list at a time
152     boost::mutex::scoped_lock lock(m_mutex);
153
154     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
155     
156     Z_GDU *gdu = package.request().get();
157     // only deal with Z39.50
158     if (!gdu || gdu->which != Z_GDU_Z3950)
159     {
160         package.move();
161         return 0;
162     }
163     Z_APDU *apdu = gdu->u.z3950;
164     while(true)
165     {
166         it = m_clients.find(package.session());
167         if (it == m_clients.end())
168             break;
169         if (gdu && gdu->which == Z_GDU_Z3950 &&
170             gdu->u.z3950->which == Z_APDU_initRequest)
171         {
172             yazpp_1::SocketManager *s = it->second->m_socket_manager;
173             delete it->second;  // destroy Z_Assoc
174             delete s;    // then manager
175             m_clients.erase(it);
176             break;
177         }
178         if (!it->second->m_in_use)
179         {
180             it->second->m_in_use = true;
181             return it->second;
182         }
183         m_cond_session_ready.wait(lock);
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         
192         package.response() = odr.create_close(Z_Close_protocolError,
193                                               "First PDU was not an "
194                                               "Initialize Request");
195         package.session().close();
196         return 0;
197     }
198     // check virtual host
199     const char *vhost =
200         yaz_oi_get_string_oidval(&apdu->u.initRequest->otherInfo,
201                                  VAL_PROXY, 1, 0);
202     if (!vhost)
203     {
204         yp2::odr odr;
205         package.response() = odr.create_initResponse(
206             YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
207             "Virtual host not given");
208         
209         package.session().close();
210         return 0;
211     }
212     
213     yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
214     yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
215     yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as, vhost);
216     m_clients[package.session()] = as;
217     as->timeout(2);
218     return as;
219 }
220
221 void yf::Z3950Client::Rep::send_and_receive(Package &package,
222                                             yf::Z3950Client::Assoc *c)
223 {
224     Z_GDU *gdu = package.request().get();
225
226     if (!gdu || gdu->which != Z_GDU_Z3950)
227         return;
228
229     c->m_package = &package;
230     c->m_waiting = true;
231     if (!c->m_connected)
232     {
233         c->client(c->m_host.c_str());
234         
235         while (!c->m_destroyed && c->m_waiting 
236                && c->m_socket_manager->processEvent() > 0)
237             ;
238     }
239     if (!c->m_connected)
240     {
241         return;
242     }
243
244     // prepare response
245     c->m_waiting = true;
246     
247     // relay the package  ..
248     int len;
249     c->send_GDU(gdu, &len);
250
251     switch(gdu->u.z3950->which)
252     {
253     case Z_APDU_triggerResourceControlRequest:
254         // request only..
255         break;
256     default:
257         // for the rest: wait for a response PDU
258         std::cout << "WAITING...\n";
259         while (!c->m_destroyed && c->m_waiting
260                && c->m_socket_manager->processEvent() > 0)
261             ;
262         std::cout << "END OF WAITING...\n";
263         break;
264     }
265     
266 }
267
268 void yf::Z3950Client::Rep::release_assoc(Package &package)
269 {
270     boost::mutex::scoped_lock lock(m_mutex);
271     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
272     
273     it = m_clients.find(package.session());
274     if (it != m_clients.end())
275     {
276         if (package.session().is_closed())
277             it->second->m_destroyed = true;
278         
279         if (it->second->m_destroyed && !it->second->m_in_use)
280         {
281             // the Z_Assoc and PDU_Assoc must be destroyed before
282             // the socket manager.. so pull that out.. first..
283             yazpp_1::SocketManager *s = it->second->m_socket_manager;
284             delete it->second;  // destroy Z_Assoc
285             delete s;    // then manager
286             m_clients.erase(it);
287             std::cout << "DESTROY " << package.session().id() << "\n";
288         }
289         else
290         {
291             it->second->m_in_use = false;
292         }
293         m_cond_session_ready.notify_all();
294     }
295 }
296
297 void yf::Z3950Client::process(Package &package) const
298 {
299     yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
300     if (c)
301     {
302         m_p->send_and_receive(package, c);
303     }
304     m_p->release_assoc(package);
305 }
306
307
308 /*
309  * Local variables:
310  * c-basic-offset: 4
311  * indent-tabs-mode: nil
312  * c-file-style: "stroustrup"
313  * End:
314  * vim: shiftwidth=4 tabstop=8 expandtab
315  */