GPL v2.
[metaproxy-moved-to-github.git] / src / filter_frontend_net.cpp
1 /* $Id: filter_frontend_net.cpp,v 1.24 2007-05-09 21:23:09 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         delete m_session;
156     delete this;
157 }
158
159 mp::IThreadPoolMsg *mp::ThreadPoolPackage::handle() 
160 {
161     m_package->move();
162     return this;
163 }
164
165
166 mp::ZAssocChild::ZAssocChild(yazpp_1::IPDU_Observable *PDU_Observable,
167                                      mp::ThreadPoolSocketObserver *my_thread_pool,
168                                      const mp::Package *package)
169     :  Z_Assoc(PDU_Observable)
170 {
171     m_thread_pool_observer = my_thread_pool;
172     m_no_requests = 0;
173     m_delete_flag = false;
174     m_package = package;
175     const char *peername = PDU_Observable->getpeername();
176     if (!peername)
177         peername = "unknown";
178     m_origin.set_tcpip_address(std::string(peername), m_session.id());
179 }
180
181
182 yazpp_1::IPDU_Observer *mp::ZAssocChild::sessionNotify(yazpp_1::IPDU_Observable
183                                                   *the_PDU_Observable, int fd)
184 {
185     return 0;
186 }
187
188 mp::ZAssocChild::~ZAssocChild()
189 {
190 }
191
192 void mp::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len)
193 {
194     m_no_requests++;
195
196     mp::Package *p = new mp::Package(m_session, m_origin);
197
198     mp::ThreadPoolPackage *tp = new mp::ThreadPoolPackage(p, this);
199     p->copy_filter(*m_package);
200     p->request() = yazpp_1::GDU(z_pdu);
201     m_thread_pool_observer->put(tp);  
202 }
203
204 void mp::ZAssocChild::failNotify()
205 {
206     // TODO: send Package to signal "close"
207     if (m_session.is_closed())
208         return;
209     m_no_requests++;
210
211     m_session.close();
212
213     mp::Package *p = new mp::Package(m_session, m_origin);
214
215     mp::ThreadPoolPackage *tp = new mp::ThreadPoolPackage(p, this);
216     p->copy_filter(*m_package);
217     m_thread_pool_observer->put(tp);  
218 }
219
220 void mp::ZAssocChild::timeoutNotify()
221 {
222     failNotify();
223 }
224
225 void mp::ZAssocChild::connectNotify()
226 {
227
228 }
229
230 mp::ZAssocServer::ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
231                                mp::ThreadPoolSocketObserver *thread_pool_observer,
232                                const mp::Package *package,
233                                int timeout)
234     :  Z_Assoc(PDU_Observable), m_session_timeout(timeout)
235 {
236     m_thread_pool_observer = thread_pool_observer;
237     m_package = package;
238
239 }
240
241 yazpp_1::IPDU_Observer *mp::ZAssocServer::sessionNotify(yazpp_1::IPDU_Observable
242                                                  *the_PDU_Observable, int fd)
243 {
244     mp::ZAssocChild *my =
245         new mp::ZAssocChild(the_PDU_Observable, m_thread_pool_observer,
246                              m_package);
247     my->timeout(m_session_timeout);
248     return my;
249 }
250
251 mp::ZAssocServer::~ZAssocServer()
252 {
253 }
254
255 void mp::ZAssocServer::recv_GDU(Z_GDU *apdu, int len)
256 {
257 }
258
259 void mp::ZAssocServer::failNotify()
260 {
261 }
262
263 void mp::ZAssocServer::timeoutNotify()
264 {
265 }
266
267 void mp::ZAssocServer::connectNotify()
268 {
269 }
270
271 mp::filter::FrontendNet::FrontendNet() : m_p(new Rep)
272 {
273     m_p->m_no_threads = 5;
274     m_p->m_listen_duration = 0;
275     m_p->m_session_timeout = 300; // 5 minutes
276 }
277
278 mp::filter::FrontendNet::~FrontendNet()
279 {
280 }
281
282 bool mp::My_Timer_Thread::timeout()
283 {
284     return m_timeout;
285 }
286
287 mp::My_Timer_Thread::My_Timer_Thread(yazpp_1::ISocketObservable *obs,
288                                  int duration) : 
289     m_obs(obs), m_pipe(9123), m_timeout(false)
290 {
291     obs->addObserver(m_pipe.read_fd(), this);
292     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
293     obs->timeoutObserver(this, duration);
294 }
295
296 void mp::My_Timer_Thread::socketNotify(int event)
297 {
298     m_timeout = true;
299     m_obs->deleteObserver(this);
300 }
301
302 void mp::filter::FrontendNet::process(Package &package) const
303 {
304     if (m_p->m_ports.size() == 0)
305         return;
306
307     yazpp_1::SocketManager mySocketManager;
308
309     My_Timer_Thread *tt = 0;
310     if (m_p->m_listen_duration)
311         tt = new My_Timer_Thread(&mySocketManager, m_p->m_listen_duration);
312
313     ThreadPoolSocketObserver threadPool(&mySocketManager, m_p->m_no_threads);
314
315     mp::ZAssocServer **az = new mp::ZAssocServer *[m_p->m_ports.size()];
316
317     // Create mp::ZAssocServer for each port
318     size_t i;
319     for (i = 0; i<m_p->m_ports.size(); i++)
320     {
321         // create a PDU assoc object (one per mp::ZAssocServer)
322         yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&mySocketManager);
323
324         // create ZAssoc with PDU Assoc
325         az[i] = new mp::ZAssocServer(as, &threadPool, &package,
326                                      m_p->m_session_timeout);
327         az[i]->server(m_p->m_ports[i].c_str());
328     }
329     while (mySocketManager.processEvent() > 0)
330     {
331         if (tt && tt->timeout())
332             break;
333     }
334     for (i = 0; i<m_p->m_ports.size(); i++)
335         delete az[i];
336
337     delete [] az;
338     delete tt;
339 }
340
341 void mp::filter::FrontendNet::configure(const xmlNode * ptr)
342 {
343     if (!ptr || !ptr->children)
344     {
345         throw mp::filter::FilterException("No ports for Frontend");
346     }
347     std::vector<std::string> ports;
348     for (ptr = ptr->children; ptr; ptr = ptr->next)
349     {
350         if (ptr->type != XML_ELEMENT_NODE)
351             continue;
352         if (!strcmp((const char *) ptr->name, "port"))
353         {
354             std::string port = mp::xml::get_text(ptr);
355             ports.push_back(port);
356             
357         }
358         else if (!strcmp((const char *) ptr->name, "threads"))
359         {
360             std::string threads_str = mp::xml::get_text(ptr);
361             int threads = atoi(threads_str.c_str());
362             if (threads < 1)
363                 throw mp::filter::FilterException("Bad value for threads: " 
364                                                    + threads_str);
365             m_p->m_no_threads = threads;
366         }
367         else if (!strcmp((const char *) ptr->name, "timeout"))
368         {
369             std::string timeout_str = mp::xml::get_text(ptr);
370             int timeout = atoi(timeout_str.c_str());
371             if (timeout < 1)
372                 throw mp::filter::FilterException("Bad value for timeout: " 
373                                                    + timeout_str);
374             m_p->m_session_timeout = timeout;
375         }
376         else
377         {
378             throw mp::filter::FilterException("Bad element " 
379                                                + std::string((const char *)
380                                                              ptr->name));
381         }
382     }
383     m_p->m_ports = ports;
384 }
385
386 std::vector<std::string> &mp::filter::FrontendNet::ports()
387 {
388     return m_p->m_ports;
389 }
390
391 int &mp::filter::FrontendNet::listen_duration()
392 {
393     return m_p->m_listen_duration;
394 }
395
396 static mp::filter::Base* filter_creator()
397 {
398     return new mp::filter::FrontendNet;
399 }
400
401 extern "C" {
402     struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net = {
403         0,
404         "frontend_net",
405         filter_creator
406     };
407 }
408
409 /*
410  * Local variables:
411  * c-basic-offset: 4
412  * indent-tabs-mode: nil
413  * c-file-style: "stroustrup"
414  * End:
415  * vim: shiftwidth=4 tabstop=8 expandtab
416  */