Rename a few files
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
index 95996b2..e7783f9 100644 (file)
@@ -2,11 +2,14 @@
  * Copyright (c) 1998-2004, Index Data.
  * See the file LICENSE for details.
  * 
- * $Id: yaz-proxy.cpp,v 1.103 2004-02-24 20:55:57 adam Exp $
+ * $Id: yaz-proxy.cpp,v 1.110 2004-03-17 10:49:22 adam Exp $
  */
 
+#include <unistd.h>
 #include <assert.h>
 #include <time.h>
+#include <sys/types.h>
+#include <fcntl.h>
 
 #include <yaz/srw.h>
 #include <yaz/marcdisp.h>
@@ -128,6 +131,8 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
     m_s2z_packing = Z_SRW_recordPacking_string;
     m_time_tv.tv_sec = 0;
     m_time_tv.tv_usec = 0;
+    if (!m_parent)
+       low_socket_open();
 }
 
 Yaz_Proxy::~Yaz_Proxy()
@@ -151,6 +156,8 @@ Yaz_Proxy::~Yaz_Proxy()
        odr_destroy(m_s2z_odr_init);
     if (m_s2z_odr_search)
        odr_destroy(m_s2z_odr_search);
+    if (!m_parent)
+       low_socket_close();
     delete m_config;
 }
 
@@ -271,30 +278,53 @@ char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
 const char *Yaz_Proxy::load_balance(const char **url)
 {
     int zurl_in_use[MAX_ZURL_PLEX];
+    int zurl_in_spare[MAX_ZURL_PLEX];
     Yaz_ProxyClient *c;
     int i;
 
     for (i = 0; i<MAX_ZURL_PLEX; i++)
+    {
        zurl_in_use[i] = 0;
+       zurl_in_spare[i] = 0;
+    }
     for (c = m_parent->m_clientPool; c; c = c->m_next)
     {
        for (i = 0; url[i]; i++)
            if (!strcmp(url[i], c->get_hostname()))
+           {
                zurl_in_use[i]++;
+               if (c->m_cookie == 0 && c->m_server == 0 && c->m_waiting == 0)
+                   zurl_in_spare[i]++;
+           }
     }
-    int min = 100000;
-    const char *ret = 0;
+    int min_use = 100000;
+    int spare_for_min = 0;
+    int max_spare = 0;
+    const char *ret_min = 0;
+    const char *ret_spare = 0;
     for (i = 0; url[i]; i++)
     {
-       yaz_log(LOG_DEBUG, "%szurl=%s use=%d",
-               m_session_str, url[i], zurl_in_use[i]);
-       if (min > zurl_in_use[i])
+       yaz_log(LOG_DEBUG, "%szurl=%s use=%d spare=%d",
+               m_session_str, url[i], zurl_in_use[i], zurl_in_spare[i]);
+       if (min_use > zurl_in_use[i])
        {
-           ret = url[i];
-           min = zurl_in_use[i];
+           ret_min = url[i];
+           min_use = zurl_in_use[i];
+           spare_for_min = zurl_in_spare[i];
+       }
+       if (max_spare < zurl_in_spare[i]) 
+       {
+           ret_spare = url[i];
+           max_spare = zurl_in_spare[i];
        }
     }
-    return ret;
+    // use the one with minimum connections if spare is > 3
+    if (spare_for_min > 3)
+       return ret_min;
+    // use one with most spares (if any)
+    if (max_spare > 0)
+       return ret_spare;
+    return ret_min;
 }
 
 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
@@ -411,8 +441,8 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
                !strcmp(m_proxyTarget, c->get_hostname()))
            {
                // found it in cache
-               yaz_log (LOG_LOG, "%sREUSE %s",
-                        m_session_str, c->get_hostname());
+               yaz_log (LOG_LOG, "%sREUSE %d %s",
+                        m_session_str, parent->m_seqno, c->get_hostname());
                
                c->m_seqno = parent->m_seqno;
                assert(c->m_server == 0);
@@ -477,16 +507,16 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
            c = c_min;
            if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
            {
-               yaz_log (LOG_LOG, "%sMAXCLIENTS Destroy %d",
-                        m_session_str, c->m_seqno);
+               yaz_log (LOG_LOG, "%sMAXCLIENTS %d Destroy %d",
+                        m_session_str, parent->m_max_clients, c->m_seqno);
                if (c->m_server && c->m_server != this)
                    delete c->m_server;
                c->m_server = 0;
            }
            else
            {
-               yaz_log (LOG_LOG, "%sMAXCLIENTS Reuse %d %d %s",
-                        m_session_str,
+               yaz_log (LOG_LOG, "%sMAXCLIENTS %d Reuse %d %d %s",
+                        m_session_str, parent->m_max_clients,
                         c->m_seqno, parent->m_seqno, c->get_hostname());
                xfree (c->m_cookie);
                c->m_cookie = 0;
@@ -609,13 +639,18 @@ void Yaz_Proxy::convert_xsl_delay()
        Z_External *r = npr->u.databaseRecord;
        if (r->which == Z_External_octet)
        {
+#if 0
+           fwrite((char*) r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, stdout);
+#endif
            xmlDocPtr res, doc = xmlParseMemory(
                (char*) r->u.octet_aligned->buf,
                r->u.octet_aligned->len);
+
            
            yaz_log(LOG_LOG, "%sXSLT convert %d",
                    m_session_str, m_stylesheet_offset);
            res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
+
            if (res)
            {
                xmlChar *out_buf;
@@ -629,6 +664,7 @@ void Yaz_Proxy::convert_xsl_delay()
                xmlFree(out_buf);
                xmlFreeDoc(res);
            }
+
            xmlFreeDoc(doc);
        }
     }
@@ -650,8 +686,10 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
 {
     int i;
 
+    yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
     yaz_marc_t mt = yaz_marc_create();
     yaz_marc_xml(mt, YAZ_MARC_MARCXML);
+    yaz_marc_iconv(mt, cd);
     for (i = 0; i < p->num_records; i++)
     {
        Z_NamePlusRecord *npr = p->records[i];
@@ -666,44 +704,15 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
                                        r->u.octet_aligned->len,
                                        &result, &rlen))
                {
-                   yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
-                   WRBUF wrbuf = wrbuf_alloc();
-                   
-                   char outbuf[120];
-                   size_t inbytesleft = rlen;
-                   const char *inp = result;
-                   while (cd && inbytesleft)
-                   {
-                       size_t outbytesleft = sizeof(outbuf);
-                       char *outp = outbuf;
-                       size_t r;
-                       
-                       r = yaz_iconv (cd, (char**) &inp,
-                                      &inbytesleft,
-                                      &outp, &outbytesleft);
-                       if (r == (size_t) (-1))
-                       {
-                           int e = yaz_iconv_error(cd);
-                           if (e != YAZ_ICONV_E2BIG)
-                           {
-                               yaz_log(LOG_WARN, "conversion failure");
-                               break;
-                           }
-                       }
-                       wrbuf_write(wrbuf, outbuf, outp - outbuf);
-                   }
-                   if (cd)
-                       yaz_iconv_close(cd);
-
                    npr->u.databaseRecord = z_ext_record(odr_encode(),
                                                         VAL_TEXT_XML,
-                                                        wrbuf_buf(wrbuf),
-                                                        wrbuf_len(wrbuf));
-                   wrbuf_free(wrbuf, 1);
+                                                        result, rlen);
                }
            }
        }
     }
+    if (cd)
+       yaz_iconv_close(cd);
     yaz_marc_destroy(mt);
 }
 
@@ -987,7 +996,7 @@ int Yaz_Proxy::send_to_client(Z_APDU *apdu)
     int kill_session = 0;
     Z_ReferenceId **new_id = get_referenceIdP(apdu);
 
-    if (new_id && m_referenceId)
+    if (new_id)
        *new_id = m_referenceId;
     
     if (apdu->which == Z_APDU_searchResponse)
@@ -1361,8 +1370,8 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
 
     m_bytes_recv += len;
     
-    if (m_log_mask & PROXY_LOG_APDU_CLIENT)
-       yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes",
+    if (m_log_mask & PROXY_LOG_REQ_CLIENT)
+       yaz_log (LOG_LOG, "%sReceiving %s from client %d bytes",
                 m_session_str, gdu_name(apdu), len);
 
     if (m_bw_hold_PDU)     // double incoming PDU. shutdown now.
@@ -1544,12 +1553,17 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
                                    &addinfo, &stylesheet_name, &m_schema);
        if (stylesheet_name)
        {
+           m_parent->low_socket_close();
+
            if (m_stylesheet_xsp)
                xsltFreeStylesheet(m_stylesheet_xsp);
+
            m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
                                                       stylesheet_name);
            m_stylesheet_offset = 0;
            xfree(stylesheet_name);
+
+           m_parent->low_socket_open();
        }
        if (err == -1)
        {
@@ -1586,12 +1600,17 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
                                    &addinfo, &stylesheet_name, &m_schema);
        if (stylesheet_name)
        {
+           m_parent->low_socket_close();
+
            if (m_stylesheet_xsp)
                xsltFreeStylesheet(m_stylesheet_xsp);
+
            m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
                                                       stylesheet_name);
            m_stylesheet_offset = 0;
            xfree(stylesheet_name);
+
+           m_parent->low_socket_open();
        }
        if (err == -1)
        {
@@ -2269,7 +2288,7 @@ void Yaz_Proxy::pre_init()
                        {
                            if (c->m_server == 0)
                                if (c->m_waiting)
-                                   spare_waiting;
+                                   spare_waiting++;
                                else
                                    spare++;
                            else
@@ -2283,7 +2302,7 @@ void Yaz_Proxy::pre_init()
                        "sparew=%d preinit=%d",m_session_str,
                        name, zurl_in_use[j], in_use, other,
                        spare, spare_waiting, pre_init);
-               if (spare < pre_init)
+               if (spare + spare_waiting < pre_init)
                {
                    c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
                    c->m_next = m_clientPool;
@@ -2535,6 +2554,21 @@ void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len)
     }
 }
 
+void Yaz_Proxy::low_socket_close()
+{
+    int i;
+    for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
+       if  (m_lo_fd[i] >= 0)
+           ::close(m_lo_fd[i]);
+}
+
+void Yaz_Proxy::low_socket_open()
+{
+    int i;
+    for (i = 0; i<NO_SPARE_SOLARIS_FD; i++)
+       m_lo_fd[i] = open("/dev/null", O_RDONLY);
+}
+
 int Yaz_Proxy::server(const char *addr)
 {
     int r = Yaz_Z_Assoc::server(addr);