Fixed bug #1130: Problem with SRU server closing connection.
[metaproxy-moved-to-github.git] / src / filter_frontend_net.cpp
1 /* $Id: filter_frontend_net.cpp,v 1.25 2008-01-21 16:15:00 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23
24 #include "util.hpp"
25 #include "pipe.hpp"
26 #include "filter.hpp"
27 #include "package.hpp"
28 #include "thread_pool_observer.hpp"
29 #include "filter_frontend_net.hpp"
30 #include <yazpp/z-assoc.h>
31 #include <yazpp/pdu-assoc.h>
32 #include <yazpp/socket-manager.h>
33 #include <yaz/log.h>
34
35 #include <iostream>
36
37 namespace mp = metaproxy_1;
38
39 namespace metaproxy_1 {
40     namespace filter {
41         class FrontendNet::Rep {
42             friend class FrontendNet;
43             int m_no_threads;
44             std::vector<std::string> m_ports;
45             int m_listen_duration;
46             int m_session_timeout;
47         };
48     }
49     class My_Timer_Thread : public yazpp_1::ISocketObserver {
50     private:
51         yazpp_1::ISocketObservable *m_obs;
52         Pipe m_pipe;
53         bool m_timeout;
54     public:
55         My_Timer_Thread(yazpp_1::ISocketObservable *obs, int duration);
56         void socketNotify(int event);
57         bool timeout();
58     };
59     class ZAssocChild : public yazpp_1::Z_Assoc {
60     public:
61         ~ZAssocChild();
62         ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable,
63                           mp::ThreadPoolSocketObserver *m_thread_pool_observer,
64                           const mp::Package *package);
65         int m_no_requests;
66     private:
67         yazpp_1::IPDU_Observer* sessionNotify(
68             yazpp_1::IPDU_Observable *the_PDU_Observable,
69             int fd);
70         void recv_GDU(Z_GDU *apdu, int len);
71         
72         void failNotify();
73         void timeoutNotify();
74         void connectNotify();
75     private:
76         mp::ThreadPoolSocketObserver *m_thread_pool_observer;
77         mp::Session m_session;
78         mp::Origin m_origin;
79         bool m_delete_flag;
80         const mp::Package *m_package;
81     };
82     class ThreadPoolPackage : public mp::IThreadPoolMsg {
83     public:
84         ThreadPoolPackage(mp::Package *package, mp::ZAssocChild *ses) :
85             m_session(ses), m_package(package) { };
86         ~ThreadPoolPackage();
87         IThreadPoolMsg *handle();
88         void result();
89         
90     private:
91         mp::ZAssocChild *m_session;
92         mp::Package *m_package;
93         
94     };
95     class ZAssocServer : public yazpp_1::Z_Assoc {
96     public:
97         ~ZAssocServer();
98         ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
99                      mp::ThreadPoolSocketObserver *m_thread_pool_observer,
100                      const mp::Package *package,
101                      int timeout);
102     private:
103         yazpp_1::IPDU_Observer* sessionNotify(
104             yazpp_1::IPDU_Observable *the_PDU_Observable,
105             int fd);
106         void recv_GDU(Z_GDU *apdu, int len);
107         
108         void failNotify();
109         void timeoutNotify();
110     void connectNotify();
111     private:
112         mp::ThreadPoolSocketObserver *m_thread_pool_observer;
113         const mp::Package *m_package;
114         int m_session_timeout;
115     };
116 }
117
118 mp::ThreadPoolPackage::~ThreadPoolPackage()
119 {
120     delete m_package;
121 }
122
123 void mp::ThreadPoolPackage::result()
124 {
125     m_session->m_no_requests--;
126
127     yazpp_1::GDU *gdu = &m_package->response();
128
129     if (gdu->get())
130     {
131         int len;
132         m_session->send_GDU(gdu->get(), &len);
133     }
134     else if (!m_package->session().is_closed())
135     {
136         // no response package and yet the session is still open..
137         // means that request is unhandled..
138         yazpp_1::GDU *gdu_req = &m_package->request();
139         Z_GDU *z_gdu = gdu_req->get();
140         if (z_gdu && z_gdu->which == Z_GDU_Z3950)
141         {
142             // For Z39.50, response with a Close and shutdown
143             mp::odr odr;
144             int len;
145             Z_APDU *apdu_response = odr.create_close(
146                 z_gdu->u.z3950, Z_Close_systemProblem, 
147                 "unhandled Z39.50 request");
148
149             m_session->send_Z_PDU(apdu_response, &len);
150             m_package->session().close();
151         }
152     }
153
154     if (m_session->m_no_requests == 0 && m_package->session().is_closed())
155     {
156         m_session->close();
157     }
158     delete this;
159 }
160
161 mp::IThreadPoolMsg *mp::ThreadPoolPackage::handle() 
162 {
163     m_package->move();
164     return this;
165 }
166
167
168 mp::ZAssocChild::ZAssocChild(yazpp_1::IPDU_Observable *PDU_Observable,
169                                      mp::ThreadPoolSocketObserver *my_thread_pool,
170                                      const mp::Package *package)
171     :  Z_Assoc(PDU_Observable)
172 {
173     m_thread_pool_observer = my_thread_pool;
174     m_no_requests = 0;
175     m_delete_flag = false;
176     m_package = package;
177     const char *peername = PDU_Observable->getpeername();
178     if (!peername)
179         peername = "unknown";
180     m_origin.set_tcpip_address(std::string(peername), m_session.id());
181 }
182
183
184 yazpp_1::IPDU_Observer *mp::ZAssocChild::sessionNotify(yazpp_1::IPDU_Observable
185                                                   *the_PDU_Observable, int fd)
186 {
187     return 0;
188 }
189
190 mp::ZAssocChild::~ZAssocChild()
191 {
192 }
193
194 void mp::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len)
195 {
196     m_no_requests++;
197
198     mp::Package *p = new mp::Package(m_session, m_origin);
199
200     mp::ThreadPoolPackage *tp = new mp::ThreadPoolPackage(p, this);
201     p->copy_filter(*m_package);
202     p->request() = yazpp_1::GDU(z_pdu);
203     m_thread_pool_observer->put(tp);  
204 }
205
206 void mp::ZAssocChild::failNotify()
207 {
208     // TODO: send Package to signal "close"
209     if (m_session.is_closed())
210     {
211         delete this;
212         return;
213     }
214     m_no_requests++;
215
216     m_session.close();
217
218     mp::Package *p = new mp::Package(m_session, m_origin);
219
220     mp::ThreadPoolPackage *tp = new mp::ThreadPoolPackage(p, this);
221     p->copy_filter(*m_package);
222     m_thread_pool_observer->put(tp);  
223 }
224
225 void mp::ZAssocChild::timeoutNotify()
226 {
227     failNotify();
228 }
229
230 void mp::ZAssocChild::connectNotify()
231 {
232
233 }
234
235 mp::ZAssocServer::ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
236                                mp::ThreadPoolSocketObserver *thread_pool_observer,
237                                const mp::Package *package,
238                                int timeout)
239     :  Z_Assoc(PDU_Observable), m_session_timeout(timeout)
240 {
241     m_thread_pool_observer = thread_pool_observer;
242     m_package = package;
243
244 }
245
246 yazpp_1::IPDU_Observer *mp::ZAssocServer::sessionNotify(yazpp_1::IPDU_Observable
247                                                  *the_PDU_Observable, int fd)
248 {
249     mp::ZAssocChild *my =
250         new mp::ZAssocChild(the_PDU_Observable, m_thread_pool_observer,
251                              m_package);
252     my->timeout(m_session_timeout);
253     return my;
254 }
255
256 mp::ZAssocServer::~ZAssocServer()
257 {
258 }
259
260 void mp::ZAssocServer::recv_GDU(Z_GDU *apdu, int len)
261 {
262 }
263
264 void mp::ZAssocServer::failNotify()
265 {
266 }
267
268 void mp::ZAssocServer::timeoutNotify()
269 {
270 }
271
272 void mp::ZAssocServer::connectNotify()
273 {
274 }
275
276 mp::filter::FrontendNet::FrontendNet() : m_p(new Rep)
277 {
278     m_p->m_no_threads = 5;
279     m_p->m_listen_duration = 0;
280     m_p->m_session_timeout = 300; // 5 minutes
281 }
282
283 mp::filter::FrontendNet::~FrontendNet()
284 {
285 }
286
287 bool mp::My_Timer_Thread::timeout()
288 {
289     return m_timeout;
290 }
291
292 mp::My_Timer_Thread::My_Timer_Thread(yazpp_1::ISocketObservable *obs,
293                                  int duration) : 
294     m_obs(obs), m_pipe(9123), m_timeout(false)
295 {
296     obs->addObserver(m_pipe.read_fd(), this);
297     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
298     obs->timeoutObserver(this, duration);
299 }
300
301 void mp::My_Timer_Thread::socketNotify(int event)
302 {
303     m_timeout = true;
304     m_obs->deleteObserver(this);
305 }
306
307 void mp::filter::FrontendNet::process(Package &package) const
308 {
309     if (m_p->m_ports.size() == 0)
310         return;
311
312     yazpp_1::SocketManager mySocketManager;
313
314     My_Timer_Thread *tt = 0;
315     if (m_p->m_listen_duration)
316         tt = new My_Timer_Thread(&mySocketManager, m_p->m_listen_duration);
317
318     ThreadPoolSocketObserver threadPool(&mySocketManager, m_p->m_no_threads);
319
320     mp::ZAssocServer **az = new mp::ZAssocServer *[m_p->m_ports.size()];
321
322     // Create mp::ZAssocServer for each port
323     size_t i;
324     for (i = 0; i<m_p->m_ports.size(); i++)
325     {
326         // create a PDU assoc object (one per mp::ZAssocServer)
327         yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&mySocketManager);
328
329         // create ZAssoc with PDU Assoc
330         az[i] = new mp::ZAssocServer(as, &threadPool, &package,
331                                      m_p->m_session_timeout);
332         az[i]->server(m_p->m_ports[i].c_str());
333     }
334     while (mySocketManager.processEvent() > 0)
335     {
336         if (tt && tt->timeout())
337             break;
338     }
339     for (i = 0; i<m_p->m_ports.size(); i++)
340         delete az[i];
341
342     delete [] az;
343     delete tt;
344 }
345
346 void mp::filter::FrontendNet::configure(const xmlNode * ptr)
347 {
348     if (!ptr || !ptr->children)
349     {
350         throw mp::filter::FilterException("No ports for Frontend");
351     }
352     std::vector<std::string> ports;
353     for (ptr = ptr->children; ptr; ptr = ptr->next)
354     {
355         if (ptr->type != XML_ELEMENT_NODE)
356             continue;
357         if (!strcmp((const char *) ptr->name, "port"))
358         {
359             std::string port = mp::xml::get_text(ptr);
360             ports.push_back(port);
361             
362         }
363         else if (!strcmp((const char *) ptr->name, "threads"))
364         {
365             std::string threads_str = mp::xml::get_text(ptr);
366             int threads = atoi(threads_str.c_str());
367             if (threads < 1)
368                 throw mp::filter::FilterException("Bad value for threads: " 
369                                                    + threads_str);
370             m_p->m_no_threads = threads;
371         }
372         else if (!strcmp((const char *) ptr->name, "timeout"))
373         {
374             std::string timeout_str = mp::xml::get_text(ptr);
375             int timeout = atoi(timeout_str.c_str());
376             if (timeout < 1)
377                 throw mp::filter::FilterException("Bad value for timeout: " 
378                                                    + timeout_str);
379             m_p->m_session_timeout = timeout;
380         }
381         else
382         {
383             throw mp::filter::FilterException("Bad element " 
384                                                + std::string((const char *)
385                                                              ptr->name));
386         }
387     }
388     m_p->m_ports = ports;
389 }
390
391 std::vector<std::string> &mp::filter::FrontendNet::ports()
392 {
393     return m_p->m_ports;
394 }
395
396 int &mp::filter::FrontendNet::listen_duration()
397 {
398     return m_p->m_listen_duration;
399 }
400
401 static mp::filter::Base* filter_creator()
402 {
403     return new mp::filter::FrontendNet;
404 }
405
406 extern "C" {
407     struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net = {
408         0,
409         "frontend_net",
410         filter_creator
411     };
412 }
413
414 /*
415  * Local variables:
416  * c-basic-offset: 4
417  * indent-tabs-mode: nil
418  * c-file-style: "stroustrup"
419  * End:
420  * vim: shiftwidth=4 tabstop=8 expandtab
421  */