Release 1.6.4
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
index 4d16b08..cd7046b 100644 (file)
-/*
- * Copyright (c) 1998-1999, Index Data.
+/* This file is part of the yazpp toolkit.
+ * Copyright (C) Index Data 
  * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
- * 
- * $Log: yaz-z-assoc.cpp,v $
- * Revision 1.2  1999-04-20 10:30:05  adam
- * Implemented various stuff for client and proxy. Updated calls
- * to ODR to reflect new name parameter.
- *
- * Revision 1.1  1999/04/09 11:46:57  adam
- * Added object Yaz_Z_Assoc. Much more functional client.
- *
  */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <assert.h>
+#include <signal.h>
+
+#include <yaz/log.h>
+#include <yazpp/z-assoc.h>
+#include <yaz/otherinfo.h>
+#include <yaz/oid_db.h>
+
+namespace yazpp_1
+{
+    class Z_Assoc_priv
+    {
+        friend class Z_Assoc;
+    private:
+        Z_Assoc_priv(IPDU_Observable *the_PDU_Observable);
+        ~Z_Assoc_priv();
+        static int yaz_init_flag;
+        static int yaz_init_func();
+        IPDU_Observable *PDU_Observable;
+        ODR odr_in;
+        ODR odr_out;
+        ODR odr_print;
+        int log;
+        FILE *APDU_file;
+        char *APDU_fname;
+        char *hostname;
+        int APDU_yazlog;
+    };
+};
 
-#include <log.h>
-#include <yaz-z-assoc.h>
+using namespace yazpp_1;
 
-int Yaz_Z_Assoc::yaz_init_func()
+int Z_Assoc_priv::yaz_init_func()
 {
-    logf (LOG_LOG, "nmem_init");
-    nmem_init();
-    logf (LOG_LOG, "done");
+#ifndef WIN32
+    signal(SIGPIPE, SIG_IGN);
+#endif
     return 1;
 }
 
-int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
+int Z_Assoc_priv::yaz_init_flag =  Z_Assoc_priv::yaz_init_func();
+
+Z_Assoc_priv::Z_Assoc_priv(IPDU_Observable *the_PDU_Observable)
+{
+    PDU_Observable = the_PDU_Observable;
+    odr_in = odr_createmem(ODR_DECODE);
+    odr_out = odr_createmem(ODR_ENCODE);
+    odr_print = odr_createmem(ODR_PRINT);
+    log = YLOG_DEBUG;
+    APDU_file = 0;
+    APDU_fname = 0;
+    hostname = 0;
+    APDU_yazlog = 0;
+}
+
+Z_Assoc_priv::~Z_Assoc_priv()
+{
+    PDU_Observable->destroy();
+    delete PDU_Observable;
+    odr_destroy(odr_print);     // note: also runs fclose on APDU_file ..
+    odr_destroy(odr_out);
+    odr_destroy(odr_in);
+    delete [] APDU_fname;
+    delete [] hostname;
+}
+
+Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
+{
+    m_p = new Z_Assoc_priv(the_PDU_Observable);
+}
+
+Z_Assoc::~Z_Assoc()
+{
+    delete m_p;
+}
+
+void Z_Assoc::set_APDU_log(const char *fname)
+{
+    if (m_p->APDU_file && m_p->APDU_file != stderr)
+    {
+        fclose(m_p->APDU_file);
+        m_p->APDU_file = 0;
+    }
+    delete [] m_p->APDU_fname;
+    m_p->APDU_fname = 0;
+
+    if (fname)
+    {
+        m_p->APDU_fname = new char[strlen(fname)+1];
+        strcpy(m_p->APDU_fname, fname);
+        if (!strcmp(fname, "-"))
+            m_p->APDU_file = stderr;
+        else if (*fname == '\0')
+            m_p->APDU_file = 0;
+        else
+            m_p->APDU_file = fopen(fname, "a");
+        odr_setprint(m_p->odr_print, m_p->APDU_file);
+    }
+}
 
-Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
+int Z_Assoc::set_APDU_yazlog(int v)
 {
-    m_PDU_Observable = the_PDU_Observable;
-    m_odr_in = odr_createmem (ODR_DECODE);
-    m_odr_out = odr_createmem (ODR_ENCODE);
-    m_odr_print = odr_createmem (ODR_PRINT);
+    int old = m_p->APDU_yazlog;
+    m_p->APDU_yazlog = v;
+    return old;
 }
 
-Yaz_Z_Assoc::~Yaz_Z_Assoc()
+const char *Z_Assoc::get_APDU_log()
 {
-    m_PDU_Observable->destroy();
-    delete m_PDU_Observable;
-    odr_destroy (m_odr_print);
-    odr_destroy (m_odr_out);
-    odr_destroy (m_odr_in);
+    return m_p->APDU_fname;
 }
 
-void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
+void Z_Assoc::recv_PDU(const char *buf, int len)
 {
-    logf (LOG_LOG, "recv_PDU len=%d", len);
-    Z_APDU *apdu = decode_Z_PDU (buf, len);
+    yaz_log(m_p->log, "recv_PDU len=%d", len);
+    Z_GDU *apdu = decode_GDU(buf, len);
     if (apdu)
-       recv_Z_PDU (apdu);
+    {
+        recv_GDU(apdu, len);
+    }
+    else
+    {
+        m_p->PDU_Observable->shutdown();
+        failNotify();
+    }
 }
 
-Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
+Z_APDU *Z_Assoc::create_Z_PDU(int type)
 {
-    return zget_APDU(m_odr_out, type);
+    Z_APDU *apdu = zget_APDU(m_p->odr_out, type);
+    if (apdu->which == Z_APDU_initRequest)
+    {
+        Z_InitRequest * p = apdu->u.initRequest;
+        char *newName = (char*) odr_malloc(m_p->odr_out, 50);
+        strcpy(newName, p->implementationName);
+        strcat(newName, " YAZ++");
+        p->implementationName = newName;
+    }
+    return apdu;
+}
+
+Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
+{
+    switch (apdu->which)
+    {
+    case  Z_APDU_initRequest:
+        return &apdu->u.initRequest->referenceId;
+    case  Z_APDU_initResponse:
+        return &apdu->u.initResponse->referenceId;
+    case  Z_APDU_searchRequest:
+        return &apdu->u.searchRequest->referenceId;
+    case  Z_APDU_searchResponse:
+        return &apdu->u.searchResponse->referenceId;
+    case  Z_APDU_presentRequest:
+        return &apdu->u.presentRequest->referenceId;
+    case  Z_APDU_presentResponse:
+        return &apdu->u.presentResponse->referenceId;
+    case  Z_APDU_deleteResultSetRequest:
+        return &apdu->u.deleteResultSetRequest->referenceId;
+    case  Z_APDU_deleteResultSetResponse:
+        return &apdu->u.deleteResultSetResponse->referenceId;
+    case  Z_APDU_accessControlRequest:
+        return &apdu->u.accessControlRequest->referenceId;
+    case  Z_APDU_accessControlResponse:
+        return &apdu->u.accessControlResponse->referenceId;
+    case  Z_APDU_resourceControlRequest:
+        return &apdu->u.resourceControlRequest->referenceId;
+    case  Z_APDU_resourceControlResponse:
+        return &apdu->u.resourceControlResponse->referenceId;
+    case  Z_APDU_triggerResourceControlRequest:
+        return &apdu->u.triggerResourceControlRequest->referenceId;
+    case  Z_APDU_resourceReportRequest:
+        return &apdu->u.resourceReportRequest->referenceId;
+    case  Z_APDU_resourceReportResponse:
+        return &apdu->u.resourceReportResponse->referenceId;
+    case  Z_APDU_scanRequest:
+        return &apdu->u.scanRequest->referenceId;
+    case  Z_APDU_scanResponse:
+        return &apdu->u.scanResponse->referenceId;
+    case  Z_APDU_sortRequest:
+        return &apdu->u.sortRequest->referenceId;
+    case  Z_APDU_sortResponse:
+        return &apdu->u.sortResponse->referenceId;
+    case  Z_APDU_segmentRequest:
+        return &apdu->u.segmentRequest->referenceId;
+    case  Z_APDU_extendedServicesRequest:
+        return &apdu->u.extendedServicesRequest->referenceId;
+    case  Z_APDU_extendedServicesResponse:
+        return &apdu->u.extendedServicesResponse->referenceId;
+    case  Z_APDU_close:
+        return &apdu->u.close->referenceId;
+    }
+    return 0;
 }
 
-int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
+void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
+{
+    Z_ReferenceId **id_from = get_referenceIdP(from);
+    Z_ReferenceId **id_to = get_referenceIdP(to);
+    if (id_to)
+    {
+        if (id_from && *id_from)
+            *id_to = odr_create_Odr_oct(m_p->odr_out, (*id_from)->buf,
+                                        (*id_from)->len);
+        else
+            *id_to = 0;
+    }
+}
+
+int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
+{
+    Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
+    gdu->which = Z_GDU_Z3950;
+    gdu->u.z3950 = apdu;
+    return send_GDU(gdu, plen);
+}
+
+int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
 {
     char *buf;
     int len;
-    logf (LOG_LOG, "Yaz_Z_Assoc:send_Z_PDU");
-    if (encode_Z_PDU(apdu, &buf, &len) > 0)
-       return m_PDU_Observable->send_PDU(buf, len);
+    if (encode_GDU(apdu, &buf, &len) > 0)
+    {
+        if (plen)
+            *plen = len;
+        return m_p->PDU_Observable->send_PDU(buf, len);
+    }
     return -1;
 }
 
-Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
+Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
 {
-    Z_APDU *apdu;
+    Z_GDU *apdu;
 
-    odr_reset (m_odr_in);
-    odr_setbuf (m_odr_in, (char*) buf, len, 0);
+    odr_reset(m_p->odr_in);
+    odr_setbuf(m_p->odr_in, (char*) buf, len, 0);
 
-    if (!z_APDU(m_odr_in, &apdu, 0, 0))
+    if (!z_GDU(m_p->odr_in, &apdu, 0, 0))
     {
-        logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
-             odr_errmsg(odr_geterror(m_odr_in)),
-             odr_offset(m_odr_in));
-        logf(LOG_LOG, "PDU dump:");
-        odr_dumpBER(log_file(), buf, len);
+        const char *element = odr_getelement(m_p->odr_in);
+        yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %ld. Element %s",
+                odr_errmsg(odr_geterror(m_p->odr_in)),
+                (long) odr_offset(m_p->odr_in),
+                element && *element ? element : "unknown");
+        yaz_log(YLOG_LOG, "Buffer length: %d", (int) len);
+        if (len > 0)
+        {
+            WRBUF w = wrbuf_alloc();
+            wrbuf_write_escaped(w, buf, len > 1024 ? 1024 : len);
+            yaz_log(YLOG_LOG, "Buffer bytes: %s", wrbuf_cstr(w));
+            wrbuf_destroy(w);
+        }
+        yaz_log(YLOG_LOG, "PDU dump:");
+        odr_dumpBER(yaz_log_file(), buf, len);
         return 0;
     }
     else
     {
-        logf (LOG_LOG, "decoded ok");
+        if (m_p->APDU_yazlog)
+        {   // use YAZ log FILE
+            FILE *save = m_p->APDU_file;
+
+            odr_setprint(m_p->odr_print, yaz_log_file());
+            z_GDU(m_p->odr_print, &apdu, 0, "decode");
+            m_p->APDU_file = save;
+            odr_setprint(m_p->odr_print, save);
+        }
+        if (m_p->APDU_file)
+        {
+            z_GDU(m_p->odr_print, &apdu, 0, "decode");
+            fflush(m_p->APDU_file);
+        }
         return apdu;
     }
 }
 
-int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
+int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
 {
-    if (!z_APDU(m_odr_out, &apdu, 0, 0))
+    const char *element = 0;
+    int r = z_GDU(m_p->odr_out, &apdu, 0, 0);
+
+    if (!r) // decoding failed. Get the failed element
+        element = odr_getelement(m_p->odr_out);
+
+    if (m_p->APDU_yazlog || !r)
     {
-       logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
-        return -1;
+        if (!r)
+            yaz_log(YLOG_LOG, "PDU encode failed. Element %s",
+                    element ? element : "unknown");
+        FILE *save = m_p->APDU_file;
+        FILE *yazf = yaz_log_file();
+        odr_setprint(m_p->odr_print, yazf); // use YAZ log FILE
+        z_GDU(m_p->odr_print, &apdu, 0, "encode");
+        m_p->APDU_file = save;
+        odr_setprint(m_p->odr_print, save);
+    }
+    if (m_p->APDU_file)
+    {
+        if (!r)
+            fprintf(m_p->APDU_file, "PDU encode failed. Element %s",
+                    element ? element : "unknown");
+        z_GDU(m_p->odr_print, &apdu, 0, "encode");
+        fflush(m_p->APDU_file);
     }
-    *buf = odr_getbuf (m_odr_out, len, 0);
-    odr_reset (m_odr_out);
+    if (!r)  // encoding failed
+        return -1;
+    *buf = odr_getbuf(m_p->odr_out, len, 0);
+    odr_reset(m_p->odr_out);
     return *len;
 }
 
-void Yaz_Z_Assoc::connectNotify()
+const char *Z_Assoc::get_hostname()
 {
-    logf (LOG_LOG, "connectNotify");
+    return m_p->hostname;
 }
 
-void Yaz_Z_Assoc::failNotify()
+int Z_Assoc::client(const char *addr)
 {
-    logf (LOG_LOG, "failNotify");
+    delete [] m_p->hostname;
+    m_p->hostname = new char[strlen(addr)+1];
+    strcpy(m_p->hostname, addr);
+    return m_p->PDU_Observable->connect(this, addr);
 }
 
-void Yaz_Z_Assoc::timeoutNotify()
+void Z_Assoc::close()
 {
-    logf (LOG_LOG, "timeoutNotify");
+    m_p->PDU_Observable->close_session();
 }
 
-void Yaz_Z_Assoc::client(const char *addr)
+int Z_Assoc::server(const char *addr)
 {
-    m_PDU_Observable->connect (this, addr);
+    delete [] m_p->hostname;
+    m_p->hostname = new char[strlen(addr)+1];
+    strcpy(m_p->hostname, addr);
+    return m_p->PDU_Observable->listen(this, addr);
 }
 
-void Yaz_Z_Assoc::close()
+ODR Z_Assoc::odr_encode()
 {
-    m_PDU_Observable->close ();
+    return m_p->odr_out;
 }
 
-void Yaz_Z_Assoc::server(const char *addr)
+ODR Z_Assoc::odr_decode()
+{
+    return m_p->odr_in;
+}
+ODR Z_Assoc::odr_print()
 {
-    m_PDU_Observable->listen (this, addr);
+    return m_p->odr_print;
 }
 
-ODR Yaz_Z_Assoc::odr_encode()
+void Z_Assoc::timeout(int timeout)
 {
-    return m_odr_out;
+    m_p->PDU_Observable->idleTime(timeout);
 }
 
-ODR Yaz_Z_Assoc::odr_decode()
+void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
 {
-    return m_odr_in;
+    switch (apdu->which)
+    {
+    case Z_APDU_initRequest:
+        *oip = &apdu->u.initRequest->otherInfo;
+        break;
+    case Z_APDU_searchRequest:
+        *oip = &apdu->u.searchRequest->otherInfo;
+        break;
+    case Z_APDU_presentRequest:
+        *oip = &apdu->u.presentRequest->otherInfo;
+        break;
+    case Z_APDU_sortRequest:
+        *oip = &apdu->u.sortRequest->otherInfo;
+        break;
+    case Z_APDU_scanRequest:
+        *oip = &apdu->u.scanRequest->otherInfo;
+        break;
+    case Z_APDU_extendedServicesRequest:
+        *oip = &apdu->u.extendedServicesRequest->otherInfo;
+        break;
+    case Z_APDU_deleteResultSetRequest:
+        *oip = &apdu->u.deleteResultSetRequest->otherInfo;
+        break;
+    case Z_APDU_initResponse:
+        *oip = &apdu->u.initResponse->otherInfo;
+        break;
+    case Z_APDU_searchResponse:
+        *oip = &apdu->u.searchResponse->otherInfo;
+        break;
+    case Z_APDU_presentResponse:
+        *oip = &apdu->u.presentResponse->otherInfo;
+        break;
+    case Z_APDU_sortResponse:
+        *oip = &apdu->u.sortResponse->otherInfo;
+        break;
+    case Z_APDU_scanResponse:
+        *oip = &apdu->u.scanResponse->otherInfo;
+        break;
+    case Z_APDU_extendedServicesResponse:
+        *oip = &apdu->u.extendedServicesResponse->otherInfo;
+        break;
+    case Z_APDU_deleteResultSetResponse:
+        *oip = &apdu->u.deleteResultSetResponse->otherInfo;
+        break;
+    default:
+        *oip = 0;
+        break;
+    }
 }
-ODR Yaz_Z_Assoc::odr_print()
+
+void Z_Assoc::set_otherInformationString(
+    Z_APDU *apdu,
+    const Odr_oid *oid, int categoryValue, const char *str)
 {
-    return m_odr_print;
+    Z_OtherInformation **otherInformation;
+    get_otherInfoAPDU(apdu, &otherInformation);
+    if (!otherInformation)
+        return;
+    set_otherInformationString(otherInformation, oid, categoryValue, str);
 }
 
-Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
+
+void Z_Assoc::set_otherInformationString (
+    Z_OtherInformation **otherInformation,
+    const Odr_oid *oid, int categoryValue, const char *str)
+{
+    Z_OtherInformationUnit *oi =
+        update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
+    if (!oi)
+        return;
+    oi->information.characterInfo = odr_strdup(odr_encode(), str);
+}
+
+Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
     Z_OtherInformation **otherInformationP, int createFlag,
-    int *oid, int categoryValue)
+    const Odr_oid *oid, int categoryValue, int deleteFlag)
 {
-    int i;
-    Z_OtherInformation *otherInformation = *otherInformationP;
-    if (!otherInformation)
-    {
-       if (!createFlag)
-           return 0;
-       otherInformation = *otherInformationP = (Z_OtherInformation *)
-           odr_malloc (odr_encode(), sizeof(*otherInformation));
-       otherInformation->num_elements = 0;
-       otherInformation->list = (Z_OtherInformationUnit **)
-           odr_malloc (odr_encode(), 8*sizeof(*otherInformation));
-       for (i = 0; i<8; i++)
-           otherInformation->list[i] = 0;
-    }
-    for (i = 0; i<otherInformation->num_elements; i++)
-    {
-       assert (otherInformation->list[i]);
-       if (!oid)
-       {
-           if (!otherInformation->list[i]->category)
-               return otherInformation->list[i];
-       }
-       else
-       {
-           if (otherInformation->list[i]->category &&
-               categoryValue ==
-               *otherInformation->list[i]->category->categoryValue &&
-               !oid_oidcmp (oid, otherInformation->list[i]->category->
-                            categoryTypeId))
-               return otherInformation->list[i];
-       }
-    }
-    if (!createFlag)
-       return 0;
-    otherInformation->list[i] = (Z_OtherInformationUnit*)
-       odr_malloc (odr_encode(), sizeof(Z_OtherInformationUnit));
-    if (oid)
-    {
-       otherInformation->list[i]->category = (Z_InfoCategory*)
-           odr_malloc (odr_encode(), sizeof(Z_InfoCategory));
-       otherInformation->list[i]->category->categoryTypeId = (int*)
-           odr_oiddup (odr_encode(), oid);
-       otherInformation->list[i]->category->categoryValue = (int*)
-           odr_malloc (odr_encode(), sizeof(int));
-       *otherInformation->list[i]->category->categoryValue =
-           categoryValue;
-    }
-    else
-       otherInformation->list[i]->category = 0;
-    otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
-    otherInformation->list[i]->information.characterInfo = 0;
-    
-    otherInformation->num_elements = i+1;
-    return otherInformation->list[i];
+    return yaz_oi_update(otherInformationP,
+                         (createFlag ? odr_encode() : 0),
+                         oid, categoryValue, deleteFlag);
 }
 
+Z_ReferenceId* Z_Assoc::getRefID(char* str)
+{
+    Z_ReferenceId* id = NULL;
+
+    if (str)
+        id = odr_create_Odr_oct(m_p->odr_out, 
+#if YAZ_VERSIONL < 0x50000
+                                (unsigned char *)
+#endif
+                                str, strlen(str));
+    return id;
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+