Added makefile for MS VS. Compiles OK for Visual Studio 2003 / 7.1.
[metaproxy-moved-to-github.git] / src / filter_z3950_client.cpp
1 /* $Id: filter_z3950_client.cpp,v 1.10 2005-11-03 14:45:16 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 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), m_connected(false),
81        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     yp2::odr odr;
116
117     if (m_package)
118     {
119         m_package->response() = odr.create_close(Z_Close_lackOfActivity, 0);
120         m_package->session().close();
121     }
122 }
123
124 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
125 {
126     m_waiting = false;
127
128     if (m_package)
129         m_package->response() = gdu;
130 }
131
132 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
133     yazpp_1::IPDU_Observable *the_PDU_Observable,
134     int fd)
135 {
136     return 0;
137 }
138
139
140 yf::Z3950Client::Z3950Client() :  m_p(new yf::Z3950Client::Rep)
141 {
142 }
143
144 yf::Z3950Client::~Z3950Client() {
145 }
146
147 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) 
148 {
149     // only one thread messes with the clients list at a time
150     boost::mutex::scoped_lock lock(m_mutex);
151
152     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
153     
154     while(true)
155     {
156         it = m_clients.find(package.session());
157         if (it == m_clients.end())
158             break;
159         
160         if (!it->second->m_in_use)
161         {
162             it->second->m_in_use = true;
163             return it->second;
164         }
165         m_cond_session_ready.wait(lock);
166     }
167
168     // only deal with Z39.50
169     Z_GDU *gdu = package.request().get();
170
171     if (!gdu || gdu->which != Z_GDU_Z3950)
172     {
173         package.move();
174         return 0;
175     }
176     Z_APDU *apdu = gdu->u.z3950;
177
178     // new Z39.50 session ..
179
180     // check that it is init. If not, close
181     if (apdu->which != Z_APDU_initRequest)
182     {
183         yp2::odr odr;
184         
185         package.response() = odr.create_close(Z_Close_protocolError,
186                                               "no init request for session");
187         package.session().close();
188         return 0;
189     }
190     // check virtual host
191     const char *vhost =
192         yaz_oi_get_string_oidval(&apdu->u.initRequest->otherInfo,
193                                  VAL_PROXY, 1, 0);
194     if (!vhost)
195     {
196         yp2::odr odr;
197         package.response() = odr.create_initResponse(
198             YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
199             "Virtual host not given");
200         
201         package.session().close();
202         return 0;
203     }
204     
205     yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
206     yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
207     yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as, vhost);
208     m_clients[package.session()] = as;
209     return as;
210 }
211
212 void yf::Z3950Client::Rep::send_and_receive(Package &package,
213                                             yf::Z3950Client::Assoc *c)
214 {
215     Z_GDU *gdu = package.request().get();
216
217     if (!gdu || gdu->which != Z_GDU_Z3950)
218         return;
219
220     c->m_package = &package;
221     c->m_waiting = true;
222     if (!c->m_connected)
223     {
224         c->client(c->m_host.c_str());
225
226         while (c->m_waiting && c->m_socket_manager->processEvent() > 0)
227             ;
228     }
229     if (!c->m_connected)
230     {
231         return;
232     }
233
234     // prepare response
235     c->m_waiting = true;
236     
237     // relay the package  ..
238     int len;
239     c->send_GDU(gdu, &len);
240     
241     while (c->m_waiting && c->m_socket_manager->processEvent() > 0)
242         ;
243 }
244
245 void yf::Z3950Client::Rep::release_assoc(Package &package)
246 {
247     boost::mutex::scoped_lock lock(m_mutex);
248     std::map<yp2::Session,yf::Z3950Client::Assoc *>::iterator it;
249     
250     it = m_clients.find(package.session());
251     if (it != m_clients.end())
252     {
253         if (package.session().is_closed())
254         {
255             // the Z_Assoc and PDU_Assoc must be destroyed before
256             // the socket manager.. so pull that out.. first..
257             yazpp_1::SocketManager *s = it->second->m_socket_manager;
258             delete it->second;  // destroy Z_Assoc
259             delete s;    // then manager
260             m_clients.erase(it);
261         }
262         else
263         {
264             it->second->m_in_use = false;
265         }
266         m_cond_session_ready.notify_all();
267     }
268 }
269
270 void yf::Z3950Client::process(Package &package) const
271 {
272     yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
273     if (c)
274     {
275         m_p->send_and_receive(package, c);
276     }
277     m_p->release_assoc(package);
278 }
279
280
281 /*
282  * Local variables:
283  * c-basic-offset: 4
284  * indent-tabs-mode: nil
285  * c-file-style: "stroustrup"
286  * End:
287  * vim: shiftwidth=4 tabstop=8 expandtab
288  */