Moved header files to include/yaz++. Switched to libtool and automake.
[yazpp-moved-to-github.git] / src / yaz-socket-manager.cpp
index a1ebbc5..d8e1ef2 100644 (file)
@@ -1,15 +1,48 @@
 /*
- * Copyright (c) 1998-1999, Index Data.
+ * Copyright (c) 1998-2000, Index Data.
  * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
  * 
  * $Log: yaz-socket-manager.cpp,v $
- * Revision 1.1  1999-01-28 09:41:07  adam
- * Initial revision
+ * Revision 1.11  2000-10-11 11:58:17  adam
+ * Moved header files to include/yaz++. Switched to libtool and automake.
+ * Configure script creates yaz++-config script.
+ *
+ * Revision 1.10  2000/09/08 10:23:42  adam
+ * Added skeleton of yaz-z-server.
+ *
+ * Revision 1.9  2000/08/07 14:19:59  adam
+ * Fixed serious bug regarding timeouts. Improved logging for proxy.
+ *
+ * Revision 1.8  1999/12/06 13:52:45  adam
+ * Modified for new location of YAZ header files. Experimental threaded
+ * operation.
+ *
+ * Revision 1.7  1999/04/28 13:02:08  adam
+ * Added include of string.h.
+ *
+ * Revision 1.6  1999/04/21 12:09:01  adam
+ * Many improvements. Modified to proxy server to work with "sessions"
+ * based on cookies.
+ *
+ * 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:23  adam
+ * First WIN32 port of YAZ++.
+ *
+ * Revision 1.2  1999/01/28 13:08:48  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++.
  *
  */
 #include <assert.h>
-#ifdef WINDOWS
+#ifdef WIN32
 #include <winsock.h>
 #else
 #include <sys/time.h>
 #include <unistd.h>
 #endif
 #include <errno.h>
+#include <string.h>
 
-#include <log.h>
-#include <yaz-socket-manager.h>
-
+#include <yaz/log.h>
+#include <yaz++/yaz-socket-manager.h>
 
 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
     IYazSocketObserver *observer)
@@ -47,6 +80,8 @@ void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
     }
     se->fd = fd;
     se->mask = 0;
+    se->last_activity = 0;
+    se->timeout = 0;
 }
 
 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
@@ -95,10 +130,14 @@ void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
 
 int Yaz_SocketManager::processEvent()
 {
+    YazSocketEntry *p;
     YazSocketEvent *event = getEvent();
+    unsigned timeout = 0;
+    logf (m_log, "processEvent");
     if (event)
     {
        event->observer->socketNotify(event->event);
+       delete event;
        return 1;
     }
 
@@ -106,21 +145,15 @@ int Yaz_SocketManager::processEvent()
     int res;
     int max = 0;
     int no = 0;
-    struct timeval to;
-    struct timeval *timeout = &to;
 
     FD_ZERO(&in);
     FD_ZERO(&out);
     FD_ZERO(&except);
 
-    timeout = &to; /* hang on select */
-    to.tv_sec = 5*60;
-    to.tv_usec = 0;
-
-    for (YazSocketEntry *p = m_observers; p; p = p->next)
+    time_t now = time(0);
+    for (p = m_observers; p; p = p->next)
     {
        int fd = p->fd;
-       logf (LOG_LOG, "fd = %d mask=%d", fd, p->mask);
        if (p->mask)
            no++;
        if (p->mask & YAZ_SOCKET_OBSERVE_READ)
@@ -131,14 +164,36 @@ int Yaz_SocketManager::processEvent()
            FD_SET(fd, &except);
        if (fd > max)
            max = fd;
+       if (p->timeout)
+       {
+           unsigned timeout_this;
+           timeout_this = p->timeout;
+           if (p->last_activity)
+               timeout_this -= now - p->last_activity;
+           if (timeout_this < 1)
+               timeout_this = 1;
+           if (!timeout || timeout_this < timeout)
+               timeout = timeout_this;
+       }
     }
     if (!no)
+    {
+       logf (m_log, "no pending events return 0");
+       if (!m_observers)
+           logf (m_log, "no observers");
        return 0;
-    while ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
+    }
+
+    struct timeval to;
+    to.tv_sec = timeout;
+    to.tv_usec = 0;
+    
+    logf (m_log, "select pending=%d timeout=%d", no, timeout);
+    while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0)
        if (errno != EINTR)
            return -1;
-
-    for (YazSocketEntry * p = m_observers; p; p = p->next)
+    now = time(0);
+    for (p = m_observers; p; p = p->next)
     {
        int fd = p->fd;
        int mask = 0;
@@ -154,14 +209,27 @@ int Yaz_SocketManager::processEvent()
        if (mask)
        {
            YazSocketEvent *event = new YazSocketEvent;
+           p->last_activity = now;
            event->observer = p->observer;
            event->event = mask;
            putEvent (event);
        }
+       else if (p->timeout && p->last_activity && 
+                now >= p->last_activity + (int) (p->timeout))
+       {
+           YazSocketEvent *event = new YazSocketEvent;
+           logf (LOG_LOG, "timeout now = %ld last_activity=%ld timeout=%d",
+                 now, p->last_activity, p->timeout);
+           p->last_activity = now;
+           event->observer = p->observer;
+           event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
+           putEvent (event);
+       }
     }
     if ((event = getEvent()))
     {
        event->observer->socketNotify(event->event);
+       delete event;
        return 1;
     }
     return 0;
@@ -169,7 +237,6 @@ int Yaz_SocketManager::processEvent()
 
 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
 {
-    logf (LOG_LOG, "putEvent p=%p event=%d", event, event->event);
     // put in back of queue
     if (m_queue_back)
     {
@@ -231,6 +298,7 @@ Yaz_SocketManager::Yaz_SocketManager()
     m_observers = 0;
     m_queue_front = 0;
     m_queue_back = 0;
+    m_log = LOG_DEBUG;
 }
 
 Yaz_SocketManager::~Yaz_SocketManager()