1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <metaproxy/util.hpp>
25 #include <metaproxy/filter.hpp>
26 #include <metaproxy/package.hpp>
27 #include "thread_pool_observer.hpp"
28 #include "filter_frontend_net.hpp"
29 #include <yazpp/z-assoc.h>
30 #include <yazpp/pdu-assoc.h>
31 #include <yazpp/socket-manager.h>
32 #include <yazpp/limit-connect.h>
33 #include <yaz/timing.h>
35 #include <yaz/daemon.h>
36 #include "gduutil.hpp"
40 namespace mp = metaproxy_1;
41 namespace yf = metaproxy_1::filter;
43 namespace metaproxy_1 {
45 class FrontendNet::Port {
47 friend class FrontendNet;
50 std::string cert_fname;
53 class FrontendNet::Rep {
54 friend class FrontendNet;
57 std::vector<Port> m_ports;
58 int m_listen_duration;
59 int m_session_timeout;
61 std::string m_msg_config;
62 std::string m_stat_req;
63 yazpp_1::SocketManager mySocketManager;
65 int m_duration_freq[22];
66 double m_duration_lim[22];
67 double m_duration_max;
68 double m_duration_min;
69 double m_duration_total;
75 class FrontendNet::My_Timer_Thread : public yazpp_1::ISocketObserver {
77 yazpp_1::ISocketObservable *m_obs;
81 My_Timer_Thread(yazpp_1::ISocketObservable *obs, int duration);
82 void socketNotify(int event);
85 class FrontendNet::ZAssocChild : public yazpp_1::Z_Assoc {
88 ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable,
89 mp::ThreadPoolSocketObserver *m_thread_pool_observer,
90 const mp::Package *package,
96 yazpp_1::IPDU_Observer* sessionNotify(
97 yazpp_1::IPDU_Observable *the_PDU_Observable,
99 void recv_GDU(Z_GDU *apdu, int len);
100 void report(Z_HTTP_Request *hreq);
102 void timeoutNotify();
103 void connectNotify();
105 mp::ThreadPoolSocketObserver *m_thread_pool_observer;
106 mp::Session m_session;
109 const mp::Package *m_package;
112 class FrontendNet::ThreadPoolPackage : public mp::IThreadPoolMsg {
114 ThreadPoolPackage(mp::Package *package,
115 yf::FrontendNet::ZAssocChild *ses,
117 ~ThreadPoolPackage();
118 IThreadPoolMsg *handle();
119 void result(const char *t_info);
120 bool cleanup(void *info);
123 ZAssocChild *m_assoc_child;
124 mp::Package *m_package;
127 class FrontendNet::ZAssocServer : public yazpp_1::Z_Assoc {
130 ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
133 void set_package(const mp::Package *package);
134 void set_thread_pool(ThreadPoolSocketObserver *observer);
136 yazpp_1::IPDU_Observer* sessionNotify(
137 yazpp_1::IPDU_Observable *the_PDU_Observable,
139 void recv_GDU(Z_GDU *apdu, int len);
142 void timeoutNotify();
143 void connectNotify();
145 mp::ThreadPoolSocketObserver *m_thread_pool_observer;
146 const mp::Package *m_package;
147 yazpp_1::LimitConnect limit_connect;
154 yf::FrontendNet::ThreadPoolPackage::ThreadPoolPackage(mp::Package *package,
157 m_assoc_child(ses), m_package(package), m_p(rep)
159 timer = yaz_timing_create();
162 yf::FrontendNet::ThreadPoolPackage::~ThreadPoolPackage()
164 yaz_timing_destroy(&timer); // timer may be NULL
168 bool yf::FrontendNet::ThreadPoolPackage::cleanup(void *info)
170 mp::Session *ses = (mp::Session *) info;
172 return *ses == m_package->session();
175 void yf::FrontendNet::ThreadPoolPackage::result(const char *t_info)
177 m_assoc_child->m_no_requests--;
179 yazpp_1::GDU *gdu = &m_package->response();
184 m_assoc_child->send_GDU(gdu->get(), &len);
186 yaz_timing_stop(timer);
187 double duration = yaz_timing_get_real(timer);
190 while (m_p->m_duration_lim[ent] != 0.0 && duration > m_p->m_duration_lim[ent])
192 m_p->m_duration_freq[ent]++;
194 m_p->m_duration_total += duration;
196 if (m_p->m_duration_max < duration)
197 m_p->m_duration_max = duration;
199 if (m_p->m_duration_min == 0.0 || m_p->m_duration_min > duration)
200 m_p->m_duration_min = duration;
202 if (m_p->m_msg_config.length())
204 Z_GDU *z_gdu = gdu->get();
206 std::ostringstream os;
207 os << m_p->m_msg_config << " "
209 << std::fixed << std::setprecision (6) << duration << " ";
216 yaz_log(YLOG_LOG, "%s %s", os.str().c_str(), t_info);
219 else if (!m_package->session().is_closed())
221 // no response package and yet the session is still open..
222 // means that request is unhandled..
223 yazpp_1::GDU *gdu_req = &m_package->request();
224 Z_GDU *z_gdu = gdu_req->get();
225 if (z_gdu && z_gdu->which == Z_GDU_Z3950)
227 // For Z39.50, response with a Close and shutdown
230 Z_APDU *apdu_response = odr.create_close(
231 z_gdu->u.z3950, Z_Close_systemProblem,
232 "unhandled Z39.50 request");
234 m_assoc_child->send_Z_PDU(apdu_response, &len);
236 else if (z_gdu && z_gdu->which == Z_GDU_HTTP_Request)
238 // For HTTP, respond with Server Error
242 = odr.create_HTTP_Response(m_package->session(),
243 z_gdu->u.HTTP_Request, 500);
244 m_assoc_child->send_GDU(zgdu_res, &len);
246 m_package->session().close();
249 if (m_assoc_child->m_no_requests == 0 && m_package->session().is_closed())
251 m_assoc_child->close();
258 mp::IThreadPoolMsg *yf::FrontendNet::ThreadPoolPackage::handle()
260 m_package->move(m_assoc_child->m_route);
264 yf::FrontendNet::ZAssocChild::ZAssocChild(
265 yazpp_1::IPDU_Observable *PDU_Observable,
266 mp::ThreadPoolSocketObserver *my_thread_pool,
267 const mp::Package *package,
268 std::string route, Rep *rep)
269 : Z_Assoc(PDU_Observable), m_p(rep)
271 m_thread_pool_observer = my_thread_pool;
273 m_delete_flag = false;
276 const char *peername = PDU_Observable->getpeername();
278 peername = "unknown";
279 m_origin.set_tcpip_address(std::string(peername), m_session.id());
280 timeout(m_p->m_session_timeout);
283 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocChild::sessionNotify(
284 yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
289 yf::FrontendNet::ZAssocChild::~ZAssocChild()
293 void yf::FrontendNet::ZAssocChild::report(Z_HTTP_Request *hreq)
297 Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
299 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
303 int number_total = 0;
305 for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
306 number_total += m_p->m_duration_freq[i];
307 number_total += m_p->m_duration_freq[i];
309 wrbuf_puts(w, "<?xml version=\"1.0\"?>\n");
310 wrbuf_puts(w, "<frontend_net>\n");
311 wrbuf_printf(w, " <responses frequency=\"%d\">\n", number_total);
312 for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
314 if (m_p->m_duration_freq[i] > 0)
316 w, " <response duration_start=\"%f\" "
317 "duration_end=\"%f\" frequency=\"%d\"/>\n",
318 i > 0 ? m_p->m_duration_lim[i - 1] : 0.0,
319 m_p->m_duration_lim[i], m_p->m_duration_freq[i]);
322 if (m_p->m_duration_freq[i] > 0)
324 w, " <response duration_start=\"%f\" frequency=\"%d\"/>\n",
325 m_p->m_duration_lim[i - 1], m_p->m_duration_freq[i]);
327 if (m_p->m_duration_max != 0.0)
329 w, " <response duration_max=\"%f\"/>\n",
330 m_p->m_duration_max);
331 if (m_p->m_duration_min != 0.0)
333 w, " <response duration_min=\"%f\"/>\n",
334 m_p->m_duration_min);
335 if (m_p->m_duration_total != 0.0)
337 w, " <response duration_average=\"%f\"/>\n",
338 m_p->m_duration_total / number_total);
340 wrbuf_puts(w, " </responses>\n");
344 m_thread_pool_observer->get_thread_info(thread_busy, thread_total);
346 wrbuf_printf(w, " <thread_info busy=\"%d\" total=\"%d\"/>\n",
347 thread_busy, thread_total);
349 wrbuf_puts(w, "</frontend_net>\n");
351 hres->content_len = w.len();
352 hres->content_buf = (char *) w.buf();
355 send_GDU(gdu_res, &len);
358 void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len)
362 mp::Package *p = new mp::Package(m_session, m_origin);
364 if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
366 Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
368 const char *f = z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
370 p->origin().set_tcpip_address(std::string(f), m_session.id());
372 if (m_p->m_stat_req.length()
373 && !strcmp(hreq->path, m_p->m_stat_req.c_str()))
380 ThreadPoolPackage *tp = new ThreadPoolPackage(p, this, m_p);
381 p->copy_route(*m_package);
382 p->request() = yazpp_1::GDU(z_pdu);
384 if (m_p->m_msg_config.length())
388 std::ostringstream os;
389 os << m_p->m_msg_config << " "
393 yaz_log(YLOG_LOG, "%s", os.str().c_str());
396 m_thread_pool_observer->put(tp);
399 void yf::FrontendNet::ZAssocChild::failNotify()
401 // TODO: send Package to signal "close"
402 if (m_session.is_closed())
404 if (m_no_requests == 0)
412 mp::Package *p = new mp::Package(m_session, m_origin);
414 ThreadPoolPackage *tp = new ThreadPoolPackage(p, this, m_p);
415 p->copy_route(*m_package);
416 m_thread_pool_observer->cleanup(tp, &m_session);
417 m_thread_pool_observer->put(tp);
420 void yf::FrontendNet::ZAssocChild::timeoutNotify()
425 void yf::FrontendNet::ZAssocChild::connectNotify()
430 yf::FrontendNet::ZAssocServer::ZAssocServer(
431 yazpp_1::IPDU_Observable *PDU_Observable,
435 Z_Assoc(PDU_Observable), m_route(route), m_p(rep)
441 void yf::FrontendNet::ZAssocServer::set_package(const mp::Package *package)
446 void yf::FrontendNet::ZAssocServer::set_thread_pool(
447 mp::ThreadPoolSocketObserver *observer)
449 m_thread_pool_observer = observer;
452 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocServer::sessionNotify(
453 yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
456 const char *peername = the_PDU_Observable->getpeername();
459 limit_connect.add_connect(peername);
460 limit_connect.cleanup(false);
461 int con_sz = limit_connect.get_total(peername);
462 if (m_p->m_connect_max && con_sz > m_p->m_connect_max)
465 ZAssocChild *my = new ZAssocChild(the_PDU_Observable,
466 m_thread_pool_observer,
467 m_package, m_route, m_p);
471 yf::FrontendNet::ZAssocServer::~ZAssocServer()
475 void yf::FrontendNet::ZAssocServer::recv_GDU(Z_GDU *apdu, int len)
479 void yf::FrontendNet::ZAssocServer::failNotify()
483 void yf::FrontendNet::ZAssocServer::timeoutNotify()
487 void yf::FrontendNet::ZAssocServer::connectNotify()
491 yf::FrontendNet::FrontendNet() : m_p(new Rep)
495 yf::FrontendNet::Rep::Rep()
498 m_listen_duration = 0;
499 m_session_timeout = 300; // 5 minutes
503 for (i = 0; i < 22; i++)
504 m_duration_freq[i] = 0;
505 m_duration_lim[0] = 0.000001;
506 m_duration_lim[1] = 0.00001;
507 m_duration_lim[2] = 0.0001;
508 m_duration_lim[3] = 0.001;
509 m_duration_lim[4] = 0.01;
510 m_duration_lim[5] = 0.1;
511 m_duration_lim[6] = 0.2;
512 m_duration_lim[7] = 0.3;
513 m_duration_lim[8] = 0.5;
514 m_duration_lim[9] = 1.0;
515 m_duration_lim[10] = 1.5;
516 m_duration_lim[11] = 2.0;
517 m_duration_lim[12] = 3.0;
518 m_duration_lim[13] = 4.0;
519 m_duration_lim[14] = 5.0;
520 m_duration_lim[15] = 6.0;
521 m_duration_lim[16] = 8.0;
522 m_duration_lim[17] = 10.0;
523 m_duration_lim[18] = 15.0;
524 m_duration_lim[19] = 20.0;
525 m_duration_lim[20] = 30.0;
526 m_duration_lim[21] = 0.0;
527 m_duration_max = 0.0;
528 m_duration_min = 0.0;
529 m_duration_total = 0.0;
533 yf::FrontendNet::Rep::~Rep()
538 for (i = 0; i < m_ports.size(); i++)
545 yf::FrontendNet::~FrontendNet()
549 void yf::FrontendNet::stop() const
554 bool yf::FrontendNet::My_Timer_Thread::timeout()
559 yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
560 yazpp_1::ISocketObservable *obs,
562 m_obs(obs), m_pipe(9123), m_timeout(false)
564 obs->addObserver(m_pipe.read_fd(), this);
565 obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
566 obs->timeoutObserver(this, duration);
569 void yf::FrontendNet::My_Timer_Thread::socketNotify(int event)
572 m_obs->deleteObserver(this);
575 void yf::FrontendNet::process(mp::Package &package) const
580 My_Timer_Thread *tt = 0;
582 if (m_p->m_listen_duration)
583 tt = new My_Timer_Thread(&m_p->mySocketManager,
584 m_p->m_listen_duration);
586 ThreadPoolSocketObserver tp(&m_p->mySocketManager, m_p->m_no_threads);
588 for (i = 0; i<m_p->m_ports.size(); i++)
590 m_p->az[i]->set_package(&package);
591 m_p->az[i]->set_thread_pool(&tp);
593 while (m_p->mySocketManager.processEvent() > 0)
601 for (i = 0; i < m_p->m_ports.size(); i++)
602 m_p->az[i]->server("");
606 int no = m_p->mySocketManager.getNumberOfObservers();
609 if (tt && tt->timeout())
615 void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only,
618 if (!ptr || !ptr->children)
620 throw yf::FilterException("No ports for Frontend");
622 std::vector<Port> ports;
623 for (ptr = ptr->children; ptr; ptr = ptr->next)
625 if (ptr->type != XML_ELEMENT_NODE)
627 if (!strcmp((const char *) ptr->name, "port"))
631 const char *names[5] = {"route", "max_recv_bytes", "port",
633 std::string values[4];
635 mp::xml::parse_attr(ptr, names, values);
636 port.route = values[0];
637 if (values[1].length() > 0)
638 port.max_recv_bytes = atoi(values[1].c_str());
640 port.max_recv_bytes = 0;
641 if (values[2].length() > 0)
642 port.port = values[2];
644 port.port = mp::xml::get_text(ptr);
645 port.cert_fname = values[3];
646 ports.push_back(port);
648 else if (!strcmp((const char *) ptr->name, "threads"))
650 std::string threads_str = mp::xml::get_text(ptr);
651 int threads = atoi(threads_str.c_str());
653 throw yf::FilterException("Bad value for threads: "
655 m_p->m_no_threads = threads;
657 else if (!strcmp((const char *) ptr->name, "timeout"))
659 std::string timeout_str = mp::xml::get_text(ptr);
660 int timeout = atoi(timeout_str.c_str());
662 throw yf::FilterException("Bad value for timeout: "
664 m_p->m_session_timeout = timeout;
666 else if (!strcmp((const char *) ptr->name, "connect-max"))
668 m_p->m_connect_max = mp::xml::get_int(ptr, 0);
670 else if (!strcmp((const char *) ptr->name, "message"))
672 m_p->m_msg_config = mp::xml::get_text(ptr);
674 else if (!strcmp((const char *) ptr->name, "stat-req"))
676 m_p->m_stat_req = mp::xml::get_text(ptr);
680 throw yf::FilterException("Bad element "
681 + std::string((const char *)
690 void yf::FrontendNet::set_ports(std::vector<std::string> &ports)
692 std::vector<Port> nports;
695 for (i = 0; i < ports.size(); i++)
699 nport.port = ports[i];
701 nports.push_back(nport);
707 void yf::FrontendNet::set_ports(std::vector<Port> &ports)
709 m_p->m_ports = ports;
711 m_p->az = new yf::FrontendNet::ZAssocServer *[m_p->m_ports.size()];
713 // Create yf::FrontendNet::ZAssocServer for each port
715 for (i = 0; i<m_p->m_ports.size(); i++)
717 // create a PDU assoc object (one per yf::FrontendNet::ZAssocServer)
718 yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&m_p->mySocketManager);
720 if (m_p->m_ports[i].cert_fname.length())
721 as->set_cert_fname(m_p->m_ports[i].cert_fname.c_str());
722 // create ZAssoc with PDU Assoc
723 m_p->az[i] = new yf::FrontendNet::ZAssocServer(
724 as, m_p->m_ports[i].route, m_p.get());
725 if (m_p->az[i]->server(m_p->m_ports[i].port.c_str()))
727 throw yf::FilterException("Unable to bind to address "
728 + std::string(m_p->m_ports[i].port));
730 COMSTACK cs = as->get_comstack();
732 if (cs && m_p->m_ports[i].max_recv_bytes)
733 cs_set_max_recv_bytes(cs, m_p->m_ports[i].max_recv_bytes);
738 void yf::FrontendNet::set_listen_duration(int d)
740 m_p->m_listen_duration = d;
743 static yf::Base* filter_creator()
745 return new yf::FrontendNet;
749 struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net = {
759 * c-file-style: "Stroustrup"
760 * indent-tabs-mode: nil
762 * vim: shiftwidth=4 tabstop=8 expandtab