disable-zoom configure option
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc.cpp
index 566f9b0..203ba34 100644 (file)
@@ -2,83 +2,7 @@
  * Copyright (c) 1998-2001, Index Data.
  * See the file LICENSE for details.
  * 
- * $Log: yaz-pdu-assoc.cpp,v $
- * Revision 1.23  2001-03-26 14:43:49  adam
- * New threaded PDU association.
- *
- * Revision 1.22  2001/01/29 11:18:24  adam
- * Server sets OPTIONS search and present.
- *
- * Revision 1.21  2000/11/20 14:17:36  adam
- * Yet another WIN32 fix for connect notify.
- *
- * Revision 1.20  2000/11/20 11:27:33  adam
- * Fixes for connect operation (timeout and notify fix).
- *
- * Revision 1.19  2000/11/01 14:22:59  adam
- * Added fd parameter for method IYaz_PDU_Observer::clone.
- *
- * Revision 1.18  2000/10/24 12:29:57  adam
- * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
- * two Yaz_Proxy's (fatal).
- *
- * Revision 1.17  2000/10/11 11:58:16  adam
- * Moved header files to include/yaz++. Switched to libtool and automake.
- * Configure script creates yaz++-config script.
- *
- * Revision 1.16  2000/09/22 09:54:11  heikki
- * minor
- *
- * Revision 1.15  2000/09/21 21:43:20  adam
- * Better high-level server API.
- *
- * Revision 1.14  2000/09/12 12:09:53  adam
- * More work on high-level server.
- *
- * Revision 1.13  2000/09/08 10:23:42  adam
- * Added skeleton of yaz-z-server.
- *
- * Revision 1.12  2000/09/06 14:23:45  adam
- * WIN32 updates.
- *
- * Revision 1.11  2000/09/04 08:29:22  adam
- * Fixed memory leak(s). Added re-use of associations, rather than
- * re-init, when maximum number of targets are in use.
- *
- * Revision 1.10  2000/08/10 08:42:42  adam
- * Fixes for {set,get}_APDU_log.
- *
- * Revision 1.9  1999/12/06 13:52:45  adam
- * Modified for new location of YAZ header files. Experimental threaded
- * operation.
- *
- * Revision 1.8  1999/04/28 13:04:03  adam
- * Fixed setting of proxy otherInfo so that database(s) are removed.
- *
- * Revision 1.7  1999/04/21 12:09:01  adam
- * Many improvements. Modified to proxy server to work with "sessions"
- * based on cookies.
- *
- * Revision 1.6  1999/04/20 10:30:05  adam
- * Implemented various stuff for client and proxy. Updated calls
- * to ODR to reflect new name parameter.
- *g
- * Revision 1.5  1999/04/09 11:46:57  adam
- * Added object Yaz_Z_Assoc. Much more functional client.
- *
- * Revision 1.4  1999/03/23 14:17:57  adam
- * More work on timeout handling. Work on yaz-client.
- *
- * Revision 1.3  1999/02/02 14:01:20  adam
- * First WIN32 port of YAZ++.
- *
- * Revision 1.2  1999/01/28 13:08:44  adam
- * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
- * yaz-socket-manager.cc.
- *
- * Revision 1.1.1.1  1999/01/28 09:41:07  adam
- * First implementation of YAZ++.
- *
+ * $Id: yaz-pdu-assoc.cpp,v 1.28 2002-10-09 12:50:26 adam Exp $
  */
 
 #include <assert.h>
@@ -86,7 +10,7 @@
 #include <yaz/log.h>
 #include <yaz/tcpip.h>
 
-#include <yaz++/yaz-pdu-assoc.h>
+#include <yaz++/pdu-assoc.h>
 
 
 void Yaz_PDU_Assoc::init(IYazSocketObservable *socketObservable)
@@ -200,8 +124,8 @@ void Yaz_PDU_Assoc::socketNotify(int event)
        }
        else
        {
-           yaz_log (m_log, "cs_connect again");
-           int res = cs_connect (m_cs, 0);
+           yaz_log (m_log, "cs_rcvconnect");
+           int res = cs_rcvconnect (m_cs);
            if (res == 1)
            {
                unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
@@ -214,7 +138,8 @@ void Yaz_PDU_Assoc::socketNotify(int event)
            else
            {
                m_state = Ready;
-               m_PDU_Observer->connectNotify();
+               if (m_PDU_Observer)
+                   m_PDU_Observer->connectNotify();
                flush_PDU();
            }
        }
@@ -240,6 +165,8 @@ void Yaz_PDU_Assoc::socketNotify(int event)
                     setup observer for child fileid in pdu-assoc
                4. start thread
            */
+           yaz_log (m_log, "new session: parent fd=%d child fd=%d",
+                    cs_fileno(m_cs), cs_fileno(new_line));
            childNotify (new_line);
        }
        break;
@@ -267,13 +194,17 @@ void Yaz_PDU_Assoc::socketNotify(int event)
                {
                    yaz_log (m_log, "Yaz_PDU_Assoc::Connection closed by peer");
                    close();
-                   m_PDU_Observer->failNotify(); // problem here..
+                   if (m_PDU_Observer)
+                       m_PDU_Observer->failNotify(); // problem here..
                    return;
                }
                // lock it, so we know if recv_PDU deletes it.
                int destroyed = 0;
                m_destroyed = &destroyed;
 
+               if (!m_PDU_Observer)
+                   return;
+
                m_PDU_Observer->recv_PDU(m_input_buf, res);
                 m_destroyed = 0;
                if (destroyed)   // it really was destroyed, return now.
@@ -286,12 +217,12 @@ void Yaz_PDU_Assoc::socketNotify(int event)
        }
        break;
     case Closed:
-       logf (m_log, "CLOSING state=%d event was %d", m_state, event);
+       yaz_log (m_log, "CLOSING state=%d event was %d", m_state, event);
        close();
        m_PDU_Observer->failNotify();
        break;
     default:
-       logf (m_log, "Unknown state=%d event was %d", m_state, event);
+       yaz_log (m_log, "Unknown state=%d event was %d", m_state, event);
        close();
        m_PDU_Observer->failNotify();
     }
@@ -299,11 +230,15 @@ void Yaz_PDU_Assoc::socketNotify(int event)
 
 void Yaz_PDU_Assoc::close()
 {
+    Yaz_PDU_Assoc *ch;
+    for (ch = m_children; ch; ch = ch->m_next)
+       ch->close();
+
     m_socketObservable->deleteObserver(this);
     m_state = Closed;
     if (m_cs)
     {
-       logf (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
+       yaz_log (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
        cs_close (m_cs);
     }
     m_cs = 0;
@@ -321,6 +256,7 @@ void Yaz_PDU_Assoc::close()
 void Yaz_PDU_Assoc::destroy()
 {
     close();
+
     if (m_destroyed)
        *m_destroyed = 1;
     Yaz_PDU_Assoc **c;
@@ -345,11 +281,12 @@ void Yaz_PDU_Assoc::destroy()
        here->m_parent = 0;
        delete here;
     }
+    yaz_log (m_log, "Yaz_PDU_Assoc::destroy this=%p", this);
 }
 
 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
 {
-    m_buf = (char *) malloc (len);
+    m_buf = (char *) xmalloc (len);
     memcpy (m_buf, buf, len);
     m_len = len;
     m_next = 0;
@@ -357,7 +294,7 @@ Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
 
 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
 {
-    free (m_buf);
+    xfree (m_buf);
 }
 
 int Yaz_PDU_Assoc::flush_PDU()
@@ -366,14 +303,14 @@ int Yaz_PDU_Assoc::flush_PDU()
     
     if (m_state != Ready && m_state != Writing)
     {
-        logf (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
+        yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
        return 1;
     }
     PDU_Queue *q = m_queue_out;
     if (!q)
     {
        m_state = Ready;
-       logf (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty");
+       yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty");
        m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
                                         YAZ_SOCKET_OBSERVE_WRITE|
                                         YAZ_SOCKET_OBSERVE_EXCEPT);
@@ -382,7 +319,7 @@ int Yaz_PDU_Assoc::flush_PDU()
     r = cs_put (m_cs, q->m_buf, q->m_len);
     if (r < 0)
     {
-        logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put failed");
+        yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put failed");
         close();
        m_PDU_Observer->failNotify();
         return r;
@@ -397,12 +334,12 @@ int Yaz_PDU_Assoc::flush_PDU()
             mask |= YAZ_SOCKET_OBSERVE_READ;
  
        m_socketObservable->maskObserver(this, mask);
-        logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes (incomplete)",
-             q->m_len);
+        yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes (incomp)",
+                q->m_len);
         return r;
     } 
     m_state = Ready;
-    logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len);
+    yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len);
     // whole packet sent... delete this and proceed to next ...
     m_queue_out = q->m_next;
     delete q;
@@ -415,13 +352,13 @@ int Yaz_PDU_Assoc::flush_PDU()
 
 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
 {
-    logf (m_log, "Yaz_PDU_Assoc::send_PDU");
+    yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU");
     PDU_Queue **pq = &m_queue_out;
     int is_idle = (*pq ? 0 : 1);
     
     if (!m_cs)
     {
-       logf (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
+       yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
         return -1;
     }
     while (*pq)
@@ -430,8 +367,8 @@ int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
     if (is_idle)
         return flush_PDU ();
     else
-       logf (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
-             cs_fileno(m_cs));
+       yaz_log (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
+                cs_fileno(m_cs));
     return 0;
 }
 
@@ -445,7 +382,7 @@ void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
 {
     close();
 
-    logf (LOG_LOG, "Adding listener %s", addr);
+    yaz_log (LOG_LOG, "Adding listener %s", addr);
 
     m_PDU_Observer = observer;
     void *ap;
@@ -458,31 +395,32 @@ void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
     m_socketObservable->addObserver(cs_fileno(m_cs), this);
     m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
                                     YAZ_SOCKET_OBSERVE_EXCEPT);
-    logf (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs));
+    yaz_log (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs));
     m_state = Listen;
 }
 
 void Yaz_PDU_Assoc::idleTime(int idleTime)
 {
     m_idleTime = idleTime;
-    logf (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
+    yaz_log (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
     m_socketObservable->timeoutObserver(this, m_idleTime);
 }
 
 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
                            const char *addr)
 {
-    logf (m_log, "Yaz_PDU_Assoc::connect %s", addr);
+    yaz_log (m_log, "Yaz_PDU_Assoc::connect %s", addr);
     close();
     m_PDU_Observer = observer;
     void *ap;
     m_cs = comstack(addr, &ap);
     int res = cs_connect (m_cs, ap);
-    logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs), res);
+    yaz_log (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs),
+            res);
     m_socketObservable->addObserver(cs_fileno(m_cs), this);
 
     if (res >= 0)
-    {   // Connect pending or complet
+    {   // Connect pending or complete
        m_state = Connecting;
        unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
        if (m_cs->io_pending & CS_WANT_WRITE)
@@ -506,6 +444,10 @@ void Yaz_PDU_Assoc::childNotify(COMSTACK cs)
     Yaz_PDU_Assoc *new_observable =
        new Yaz_PDU_Assoc (m_socketObservable, cs);
     
+    new_observable->m_next = m_children;
+    m_children = new_observable;
+    new_observable->m_parent = this;
+
     // Clone PDU Observer
     new_observable->m_PDU_Observer = m_PDU_Observer->sessionNotify
        (new_observable, cs_fileno(cs));