/* This file is part of Metaproxy.
- Copyright (C) 2005-2012 Index Data
+ Copyright (C) Index Data
Metaproxy is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
#include <yazpp/limit-connect.h>
#include <yaz/timing.h>
#include <yaz/log.h>
+#include <yaz/daemon.h>
#include "gduutil.hpp"
+#include <signal.h>
#include <iostream>
friend class FrontendNet;
std::string port;
std::string route;
+ std::string cert_fname;
+ int max_recv_bytes;
};
class FrontendNet::Rep {
friend class FrontendNet;
std::string m_stat_req;
yazpp_1::SocketManager mySocketManager;
ZAssocServer **az;
+ yazpp_1::PDU_Assoc **pdu;
int m_duration_freq[22];
double m_duration_lim[22];
+ double m_duration_max;
+ double m_duration_min;
+ double m_duration_total;
+ int m_stop_signo;
public:
Rep();
~Rep();
ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable,
mp::ThreadPoolSocketObserver *m_thread_pool_observer,
const mp::Package *package,
- std::string route,
+ Port *port,
Rep *rep);
int m_no_requests;
- std::string m_route;
+ Port *m_port;
private:
yazpp_1::IPDU_Observer* sessionNotify(
yazpp_1::IPDU_Observable *the_PDU_Observable,
int fd);
void recv_GDU(Z_GDU *apdu, int len);
-
+ void report(Z_HTTP_Request *hreq);
void failNotify();
void timeoutNotify();
void connectNotify();
ZAssocChild *m_assoc_child;
mp::Package *m_package;
Rep *m_p;
- };
+ };
class FrontendNet::ZAssocServer : public yazpp_1::Z_Assoc {
public:
~ZAssocServer();
ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
- std::string route,
+ FrontendNet::Port *port,
Rep *rep);
void set_package(const mp::Package *package);
void set_thread_pool(ThreadPoolSocketObserver *observer);
yazpp_1::IPDU_Observable *the_PDU_Observable,
int fd);
void recv_GDU(Z_GDU *apdu, int len);
-
+
void failNotify();
void timeoutNotify();
void connectNotify();
mp::ThreadPoolSocketObserver *m_thread_pool_observer;
const mp::Package *m_package;
yazpp_1::LimitConnect limit_connect;
- std::string m_route;
+ Port *m_port;
Rep *m_p;
};
}
yaz_timing_stop(timer);
double duration = yaz_timing_get_real(timer);
-
+
size_t ent = 0;
while (m_p->m_duration_lim[ent] != 0.0 && duration > m_p->m_duration_lim[ent])
ent++;
m_p->m_duration_freq[ent]++;
-
+
+ m_p->m_duration_total += duration;
+
+ if (m_p->m_duration_max < duration)
+ m_p->m_duration_max = duration;
+
+ if (m_p->m_duration_min == 0.0 || m_p->m_duration_min > duration)
+ m_p->m_duration_min = duration;
+
if (m_p->m_msg_config.length())
{
Z_GDU *z_gdu = gdu->get();
-
+
std::ostringstream os;
os << m_p->m_msg_config << " "
<< *m_package << " "
<< std::fixed << std::setprecision (6) << duration << " ";
-
- if (z_gdu)
+
+ if (z_gdu)
os << *z_gdu;
else
os << "-";
-
+
yaz_log(YLOG_LOG, "%s %s", os.str().c_str(), t_info);
}
}
mp::odr odr;
int len;
Z_APDU *apdu_response = odr.create_close(
- z_gdu->u.z3950, Z_Close_systemProblem,
+ z_gdu->u.z3950, Z_Close_systemProblem,
"unhandled Z39.50 request");
m_assoc_child->send_Z_PDU(apdu_response, &len);
// For HTTP, respond with Server Error
int len;
mp::odr odr;
- Z_GDU *zgdu_res
- = odr.create_HTTP_Response(m_package->session(),
+ Z_GDU *zgdu_res
+ = odr.create_HTTP_Response(m_package->session(),
z_gdu->u.HTTP_Request, 500);
m_assoc_child->send_GDU(zgdu_res, &len);
}
{
m_assoc_child->close();
}
-
+
delete this;
}
-mp::IThreadPoolMsg *yf::FrontendNet::ThreadPoolPackage::handle()
+mp::IThreadPoolMsg *yf::FrontendNet::ThreadPoolPackage::handle()
{
- m_package->move(m_assoc_child->m_route);
+ m_package->move(m_assoc_child->m_port->route);
return this;
}
yazpp_1::IPDU_Observable *PDU_Observable,
mp::ThreadPoolSocketObserver *my_thread_pool,
const mp::Package *package,
- std::string route, Rep *rep)
+ Port *port, Rep *rep)
: Z_Assoc(PDU_Observable), m_p(rep)
{
m_thread_pool_observer = my_thread_pool;
m_no_requests = 0;
m_delete_flag = false;
m_package = package;
- m_route = route;
+ m_port = port;
const char *peername = PDU_Observable->getpeername();
if (!peername)
peername = "unknown";
- m_origin.set_tcpip_address(std::string(peername), m_session.id());
+ else
+ {
+ const char *cp = strchr(peername, ':');
+ if (cp)
+ peername = cp + 1;
+ }
+ std::string addr;
+ addr.append(peername);
+ addr.append(" ");
+ addr.append(port->port);
+ m_origin.set_tcpip_address(addr, m_session.id());
timeout(m_p->m_session_timeout);
}
{
}
+void yf::FrontendNet::ZAssocChild::report(Z_HTTP_Request *hreq)
+{
+ mp::odr o;
+
+ Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
+
+ Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
+
+ mp::wrbuf w;
+ size_t i;
+ int number_total = 0;
+
+ for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
+ number_total += m_p->m_duration_freq[i];
+ number_total += m_p->m_duration_freq[i];
+
+ wrbuf_puts(w, "<?xml version=\"1.0\"?>\n");
+ wrbuf_puts(w, "<frontend_net>\n");
+ wrbuf_printf(w, " <responses frequency=\"%d\">\n", number_total);
+ for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
+ {
+ if (m_p->m_duration_freq[i] > 0)
+ wrbuf_printf(
+ w, " <response duration_start=\"%f\" "
+ "duration_end=\"%f\" frequency=\"%d\"/>\n",
+ i > 0 ? m_p->m_duration_lim[i - 1] : 0.0,
+ m_p->m_duration_lim[i], m_p->m_duration_freq[i]);
+ }
+
+ if (m_p->m_duration_freq[i] > 0)
+ wrbuf_printf(
+ w, " <response duration_start=\"%f\" frequency=\"%d\"/>\n",
+ m_p->m_duration_lim[i - 1], m_p->m_duration_freq[i]);
+
+ if (m_p->m_duration_max != 0.0)
+ wrbuf_printf(
+ w, " <response duration_max=\"%f\"/>\n",
+ m_p->m_duration_max);
+ if (m_p->m_duration_min != 0.0)
+ wrbuf_printf(
+ w, " <response duration_min=\"%f\"/>\n",
+ m_p->m_duration_min);
+ if (m_p->m_duration_total != 0.0)
+ wrbuf_printf(
+ w, " <response duration_average=\"%f\"/>\n",
+ m_p->m_duration_total / number_total);
+
+ wrbuf_puts(w, " </responses>\n");
+
+ int thread_busy;
+ int thread_total;
+ m_thread_pool_observer->get_thread_info(thread_busy, thread_total);
+
+ wrbuf_printf(w, " <thread_info busy=\"%d\" total=\"%d\"/>\n",
+ thread_busy, thread_total);
+
+ wrbuf_puts(w, "</frontend_net>\n");
+
+ hres->content_len = w.len();
+ hres->content_buf = (char *) w.buf();
+
+ int len;
+ send_GDU(gdu_res, &len);
+}
+
void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len)
{
m_no_requests++;
{
Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
+ const char *f = z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
+ if (f)
+ p->origin().set_tcpip_address(std::string(f), m_session.id());
+
if (m_p->m_stat_req.length()
&& !strcmp(hreq->path, m_p->m_stat_req.c_str()))
{
- mp::odr o;
-
- Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
-
- Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
-
- mp::wrbuf w;
- size_t i;
- int number_total = 0;
-
- for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
- number_total += m_p->m_duration_freq[i];
- number_total += m_p->m_duration_freq[i];
-
- wrbuf_puts(w, "<?xml version=\"1.0\">\n");
- wrbuf_puts(w, "<frontend_net>\n");
- wrbuf_printf(w, " <responses frequency=\"%d\">\n", number_total);
- for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
- {
- if (m_p->m_duration_freq[i] > 0)
- wrbuf_printf(
- w, " <response mintime=\"%f\" "
- "maxtime=\"%f\" frequency=\"%d\"/>\n",
- i > 0 ? m_p->m_duration_lim[i - 1] : 0.0,
- m_p->m_duration_lim[i], m_p->m_duration_freq[i]);
- }
-
- if (m_p->m_duration_freq[i] > 0)
- wrbuf_printf(
- w, " <response mintime=\"%f\" frequency=\"%d\"/>\n",
- m_p->m_duration_lim[i - 1], m_p->m_duration_freq[i]);
- wrbuf_puts(w, " </responses>\n");
- wrbuf_puts(w, "</frontend_net>\n");
-
- hres->content_len = w.len();
- hres->content_buf = (char *) w.buf();
-
- int len;
- send_GDU(gdu_res, &len);
+ report(hreq);
return;
}
}
if (m_p->m_msg_config.length())
{
- if (z_pdu)
+ if (z_pdu)
{
std::ostringstream os;
os << m_p->m_msg_config << " "
<< *p << " "
- << "0.000000" << " "
+ << "0.000000" << " "
<< *z_pdu;
yaz_log(YLOG_LOG, "%s", os.str().c_str());
}
}
- m_thread_pool_observer->put(tp);
+ m_thread_pool_observer->put(tp);
}
void yf::FrontendNet::ZAssocChild::failNotify()
yf::FrontendNet::ZAssocServer::ZAssocServer(
yazpp_1::IPDU_Observable *PDU_Observable,
- std::string route,
+ Port *port,
Rep *rep)
- :
- Z_Assoc(PDU_Observable), m_route(route), m_p(rep)
+ :
+ Z_Assoc(PDU_Observable), m_port(port), m_p(rep)
{
m_package = 0;
}
}
void yf::FrontendNet::ZAssocServer::set_thread_pool(
- ThreadPoolSocketObserver *observer)
+ mp::ThreadPoolSocketObserver *observer)
{
m_thread_pool_observer = observer;
}
}
ZAssocChild *my = new ZAssocChild(the_PDU_Observable,
m_thread_pool_observer,
- m_package, m_route, m_p);
+ m_package, m_port, m_p);
return my;
}
m_duration_lim[18] = 15.0;
m_duration_lim[19] = 20.0;
m_duration_lim[20] = 30.0;
- m_duration_lim[21] = 0;
+ m_duration_lim[21] = 0.0;
+ m_duration_max = 0.0;
+ m_duration_min = 0.0;
+ m_duration_total = 0.0;
+ m_stop_signo = 0;
}
-
yf::FrontendNet::Rep::~Rep()
{
if (az)
for (i = 0; i < m_ports.size(); i++)
delete az[i];
delete [] az;
+ delete [] pdu;
}
az = 0;
}
-
+
yf::FrontendNet::~FrontendNet()
{
}
-void yf::FrontendNet::stop() const
+void yf::FrontendNet::stop(int signo) const
{
- if (m_p->az)
- {
- size_t i;
- for (i = 0; i < m_p->m_ports.size(); i++)
- m_p->az[i]->server("");
- }
+ m_p->m_stop_signo = signo;
}
bool yf::FrontendNet::My_Timer_Thread::timeout()
yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
yazpp_1::ISocketObservable *obs,
- int duration) :
+ int duration) :
m_obs(obs), m_pipe(9123), m_timeout(false)
{
obs->addObserver(m_pipe.read_fd(), this);
m_obs->deleteObserver(this);
}
-void yf::FrontendNet::process(Package &package) const
+void yf::FrontendNet::process(mp::Package &package) const
{
if (m_p->az == 0)
return;
if (m_p->m_listen_duration)
tt = new My_Timer_Thread(&m_p->mySocketManager,
m_p->m_listen_duration);
-
+
ThreadPoolSocketObserver tp(&m_p->mySocketManager, m_p->m_no_threads);
for (i = 0; i<m_p->m_ports.size(); i++)
}
while (m_p->mySocketManager.processEvent() > 0)
{
+ if (m_p->m_stop_signo == SIGTERM)
+ {
+ yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
+ if (m_p->az)
+ {
+ size_t i;
+ for (i = 0; i < m_p->m_ports.size(); i++)
+ {
+ m_p->pdu[i]->shutdown();
+ m_p->az[i]->server("");
+ }
+ yaz_daemon_stop();
+ }
+ break; /* stop right away */
+ }
+#ifndef WIN32
+ if (m_p->m_stop_signo == SIGUSR1)
+ { /* just stop listeners and cont till all sessions are done*/
+ yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
+ m_p->m_stop_signo = 0;
+ if (m_p->az)
+ {
+ size_t i;
+ for (i = 0; i < m_p->m_ports.size(); i++)
+ m_p->az[i]->server("");
+ yaz_daemon_stop();
+ }
+ }
+#endif
int no = m_p->mySocketManager.getNumberOfObservers();
if (no <= 1)
break;
if (!strcmp((const char *) ptr->name, "port"))
{
Port port;
- const struct _xmlAttr *attr;
- for (attr = ptr->properties; attr; attr = attr->next)
- {
- if (!strcmp((const char *) attr->name, "route"))
- port.route = mp::xml::get_text(attr);
- }
- port.port = mp::xml::get_text(ptr);
+
+ const char *names[5] = {"route", "max_recv_bytes", "port",
+ "cert_fname", 0};
+ std::string values[4];
+
+ mp::xml::parse_attr(ptr, names, values);
+ port.route = values[0];
+ if (values[1].length() > 0)
+ port.max_recv_bytes = atoi(values[1].c_str());
+ else
+ port.max_recv_bytes = 0;
+ if (values[2].length() > 0)
+ port.port = values[2];
+ else
+ port.port = mp::xml::get_text(ptr);
+ port.cert_fname = values[3];
ports.push_back(port);
-
}
else if (!strcmp((const char *) ptr->name, "threads"))
{
std::string threads_str = mp::xml::get_text(ptr);
int threads = atoi(threads_str.c_str());
if (threads < 1)
- throw yf::FilterException("Bad value for threads: "
+ throw yf::FilterException("Bad value for threads: "
+ threads_str);
m_p->m_no_threads = threads;
}
std::string timeout_str = mp::xml::get_text(ptr);
int timeout = atoi(timeout_str.c_str());
if (timeout < 1)
- throw yf::FilterException("Bad value for timeout: "
+ throw yf::FilterException("Bad value for timeout: "
+ timeout_str);
m_p->m_session_timeout = timeout;
}
}
else
{
- throw yf::FilterException("Bad element "
+ throw yf::FilterException("Bad element "
+ std::string((const char *)
ptr->name));
}
void yf::FrontendNet::set_ports(std::vector<Port> &ports)
{
m_p->m_ports = ports;
-
+
m_p->az = new yf::FrontendNet::ZAssocServer *[m_p->m_ports.size()];
-
+ m_p->pdu = new yazpp_1::PDU_Assoc *[m_p->m_ports.size()];
+
// Create yf::FrontendNet::ZAssocServer for each port
size_t i;
- for (i = 0; i<m_p->m_ports.size(); i++)
+ for (i = 0; i < m_p->m_ports.size(); i++)
+ m_p->az[i] = 0;
+ for (i = 0; i < m_p->m_ports.size(); i++)
{
// create a PDU assoc object (one per yf::FrontendNet::ZAssocServer)
yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&m_p->mySocketManager);
-
+
+ if (m_p->m_ports[i].cert_fname.length())
+ as->set_cert_fname(m_p->m_ports[i].cert_fname.c_str());
// create ZAssoc with PDU Assoc
+ m_p->pdu[i] = as;
m_p->az[i] = new yf::FrontendNet::ZAssocServer(
- as, m_p->m_ports[i].route, m_p.get());
+ as, &m_p->m_ports[i], m_p.get());
if (m_p->az[i]->server(m_p->m_ports[i].port.c_str()))
{
- throw yf::FilterException("Unable to bind to address "
+ throw yf::FilterException("Unable to bind to address "
+ std::string(m_p->m_ports[i].port));
}
+ COMSTACK cs = as->get_comstack();
+
+ if (cs && m_p->m_ports[i].max_recv_bytes)
+ cs_set_max_recv_bytes(cs, m_p->m_ports[i].max_recv_bytes);
+
}
}