X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Ffilter_frontend_net.cpp;h=d4867ad14a557bd3583eae6a35f09d0204d24836;hb=73f37c91c144b070020df2f27472c09b62367acf;hp=57fab7a8ae04b93d09e0d68755698590e57d9ca9;hpb=cb4456f564de9bb46f60aef81474f22ef1bd6dca;p=metaproxy-moved-to-github.git diff --git a/src/filter_frontend_net.cpp b/src/filter_frontend_net.cpp index 57fab7a..d4867ad 100644 --- a/src/filter_frontend_net.cpp +++ b/src/filter_frontend_net.cpp @@ -61,6 +61,10 @@ namespace metaproxy_1 { ZAssocServer **az; int m_duration_freq[22]; double m_duration_lim[22]; + double m_duration_max; + double m_duration_min; + double m_duration_total; + bool m_stop; public: Rep(); ~Rep(); @@ -116,7 +120,7 @@ namespace metaproxy_1 { ZAssocChild *m_assoc_child; mp::Package *m_package; Rep *m_p; - }; + }; class FrontendNet::ZAssocServer : public yazpp_1::Z_Assoc { public: ~ZAssocServer(); @@ -130,7 +134,7 @@ namespace metaproxy_1 { yazpp_1::IPDU_Observable *the_PDU_Observable, int fd); void recv_GDU(Z_GDU *apdu, int len); - + void failNotify(); void timeoutNotify(); void connectNotify(); @@ -178,26 +182,34 @@ void yf::FrontendNet::ThreadPoolPackage::result(const char *t_info) 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); } } @@ -213,7 +225,7 @@ void yf::FrontendNet::ThreadPoolPackage::result(const char *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); @@ -223,8 +235,8 @@ void yf::FrontendNet::ThreadPoolPackage::result(const char *t_info) // 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); } @@ -235,12 +247,12 @@ void yf::FrontendNet::ThreadPoolPackage::result(const char *t_info) { 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); return this; @@ -278,42 +290,64 @@ yf::FrontendNet::ZAssocChild::~ZAssocChild() 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, "\n"); + + wrbuf_puts(w, "\n"); wrbuf_puts(w, "\n"); wrbuf_printf(w, " \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, " \n", + w, " \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, " \n", + w, " \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, " \n", + m_p->m_duration_max); + if (m_p->m_duration_min != 0.0) + wrbuf_printf( + w, " \n", + m_p->m_duration_min); + if (m_p->m_duration_total != 0.0) + wrbuf_printf( + w, " \n", + m_p->m_duration_total / number_total); + wrbuf_puts(w, " \n"); + + int thread_busy; + int thread_total; + m_thread_pool_observer->get_thread_info(thread_busy, thread_total); + + wrbuf_printf(w, " \n", + thread_busy, thread_total); + wrbuf_puts(w, "\n"); - + hres->content_len = w.len(); hres->content_buf = (char *) w.buf(); - + int len; send_GDU(gdu_res, &len); } @@ -328,6 +362,10 @@ void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len) { 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())) { @@ -342,17 +380,17 @@ void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len) 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() @@ -390,7 +428,7 @@ yf::FrontendNet::ZAssocServer::ZAssocServer( yazpp_1::IPDU_Observable *PDU_Observable, std::string route, Rep *rep) - : + : Z_Assoc(PDU_Observable), m_route(route), m_p(rep) { m_package = 0; @@ -482,7 +520,11 @@ yf::FrontendNet::Rep::Rep() 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 = false; } yf::FrontendNet::Rep::~Rep() @@ -503,12 +545,7 @@ yf::FrontendNet::~FrontendNet() void yf::FrontendNet::stop() 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 = true; } bool yf::FrontendNet::My_Timer_Thread::timeout() @@ -518,7 +555,7 @@ 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); @@ -542,7 +579,7 @@ void yf::FrontendNet::process(Package &package) const 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; im_ports.size(); i++) @@ -552,6 +589,16 @@ void yf::FrontendNet::process(Package &package) const } while (m_p->mySocketManager.processEvent() > 0) { + if (m_p->m_stop) + { + m_p->m_stop = false; + if (m_p->az) + { + size_t i; + for (i = 0; i < m_p->m_ports.size(); i++) + m_p->az[i]->server(""); + } + } int no = m_p->mySocketManager.getNumberOfObservers(); if (no <= 1) break; @@ -584,14 +631,14 @@ void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only, } port.port = mp::xml::get_text(ptr); 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; } @@ -600,7 +647,7 @@ void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only, 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; } @@ -618,7 +665,7 @@ void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only, } else { - throw yf::FilterException("Bad element " + throw yf::FilterException("Bad element " + std::string((const char *) ptr->name)); } @@ -648,22 +695,22 @@ void yf::FrontendNet::set_ports(std::vector &ports) void yf::FrontendNet::set_ports(std::vector &ports) { m_p->m_ports = ports; - + m_p->az = new yf::FrontendNet::ZAssocServer *[m_p->m_ports.size()]; - + // Create yf::FrontendNet::ZAssocServer for each port size_t i; for (i = 0; im_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); - + // create ZAssoc with PDU Assoc m_p->az[i] = new yf::FrontendNet::ZAssocServer( as, m_p->m_ports[i].route, 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)); } }