Happy new year
[yazproxy-moved-to-github.git] / src / t-server.cpp
index 5489f59..b54058c 100644 (file)
@@ -1,9 +1,20 @@
-/*
- * Copyright (c) 1998-2005, Index Data.
- * See the file LICENSE for details.
- * 
- * $Id: t-server.cpp,v 1.1 2005-06-21 21:54:05 adam Exp $
- */
+/* This file is part of YAZ proxy
+   Copyright (C) 1998-2009 Index Data
+
+YAZ proxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
 
 #include <stdlib.h>
 #include <pthread.h>
 #include <yaz/diagbib1.h>
 #include <yaz/options.h>
 #include "msg-thread.h"
-#include <yaz++/z-assoc.h>
-#include <yaz++/pdu-assoc.h>
-#include <yaz++/gdu.h>
-#include <yaz++/socket-manager.h>
-
-using namespace yazpp_1;
+#include <yazpp/z-assoc.h>
+#include <yazpp/pdu-assoc.h>
+#include <yazpp/gdu.h>
+#include <yazpp/gduqueue.h>
+#include <yazpp/socket-manager.h>
 
-class Mutex {
-public:
-    Mutex();
-    ~Mutex();
-    void lock();
-    void unlock();
-private:
-    pthread_mutex_t m_mutex;
-    
-};
-
-Mutex::Mutex()
-{
-    pthread_mutex_init(&m_mutex, 0);
-}
-
-Mutex::~Mutex()
-{
-    pthread_mutex_destroy(&m_mutex);
-}
-void Mutex::lock()
-{
-    pthread_mutex_lock(&m_mutex);
-}
-void Mutex::unlock()
-{
-    pthread_mutex_unlock(&m_mutex);
-}
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
+using namespace yazpp_1;
 
 class MyServer;
 
@@ -85,23 +69,30 @@ IMsg_Thread *Auth_Msg::handle()
     Z_GDU *z_gdu = m_gdu->get();
     if (z_gdu->which == Z_GDU_Z3950)
     {
-       Z_APDU *apdu = 0;
-       switch(z_gdu->u.z3950->which)
-       {
-       case Z_APDU_initRequest:
-           apdu = zget_APDU(odr, Z_APDU_initResponse);
-           break;
-       case Z_APDU_searchRequest:
-           sleep(5);
-           apdu = zget_APDU(odr, Z_APDU_searchResponse);
-           break;
-       default:
-           apdu = zget_APDU(odr, Z_APDU_close);
-           m_close_flag = 1;
-           break;
-       }
-       if (apdu)
-           m_output = new GDU(apdu);
+        Z_APDU *apdu = 0;
+        switch(z_gdu->u.z3950->which)
+        {
+        case Z_APDU_initRequest:
+            apdu = zget_APDU(odr, Z_APDU_initResponse);
+            ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_triggerResourceCtrl);
+            ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_search);
+            ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_present);
+            break;
+        case Z_APDU_searchRequest:
+#if HAVE_UNISTD_H
+            sleep(5);
+#endif
+            apdu = zget_APDU(odr, Z_APDU_searchResponse);
+            break;
+        case Z_APDU_triggerResourceControlRequest:
+            break;
+        default:
+            apdu = zget_APDU(odr, Z_APDU_close);
+            m_close_flag = 1;
+            break;
+        }
+        if (apdu)
+            m_output = new GDU(apdu);
     }
     yaz_log(YLOG_LOG, "Auth_Msg:handle end");
     odr_destroy(odr);
@@ -112,10 +103,10 @@ class MyServer : public Z_Assoc {
 public:
     ~MyServer();
     MyServer(IPDU_Observable *the_PDU_Observable,
-            Msg_Thread *m_my_thread
-       );
+             Msg_Thread *m_my_thread
+        );
     IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
-                                int fd);
+                                 int fd);
 
     void recv_GDU(Z_GDU *apdu, int len);
 
@@ -135,23 +126,24 @@ void Auth_Msg::result()
     m_front->m_no_requests--;
     if (!m_front->m_delete_flag)
     {
-       if (m_output)
-       {
-           int len;
-           m_front->send_GDU(m_output->get(), &len);
-       }
-       if (m_close_flag)
-       {
-           m_front->close();
-           m_front->m_delete_flag = 1;
-       }
+        if (m_output)
+        {
+            int len;
+            m_front->send_GDU(m_output->get(), &len);
+        }
+        if (m_close_flag)
+        {
+            m_front->close();
+            m_front->m_delete_flag = 1;
+        }
     }
     if (m_front->m_delete_flag && m_front->m_no_requests == 0)
-       delete m_front;
+        delete m_front;
+    delete this;
 }
 
 MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
-                  Msg_Thread *my_thread
+                   Msg_Thread *my_thread
 )
     :  Z_Assoc(the_PDU_Observable)
 {
@@ -162,7 +154,7 @@ MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
 }
 
 IPDU_Observer *MyServer::sessionNotify(IPDU_Observable
-                                      *the_PDU_Observable, int fd)
+                                       *the_PDU_Observable, int fd)
 {
     MyServer *my = new MyServer(the_PDU_Observable, m_my_thread);
     yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
@@ -186,7 +178,7 @@ void MyServer::failNotify()
 {
     m_delete_flag = 1;
     if (m_no_requests == 0)
-       delete this;
+        delete this;
     
 }
 
@@ -194,7 +186,7 @@ void MyServer::timeoutNotify()
 {
     m_delete_flag = 1;
     if (m_no_requests == 0)
-       delete this;
+        delete this;
 }
 
 void MyServer::connectNotify()
@@ -216,27 +208,31 @@ int main(int argc, char **argv)
     int ret;
     const char *addr = "tcp:@:9999";
     char *apdu_log = 0;
+    int no_threads = 1;
 
-    while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
+    while ((ret = options("n:a:v:T", argv, argc, &arg)) != -2)
     {
-       switch (ret)
-       {
-       case 0:
-           addr = xstrdup(arg);
-           break;
-       case 'a':
-           apdu_log = xstrdup(arg);
-           break;
-       case 'v':
-           yaz_log_init_level (yaz_log_mask_str(arg));
-           break;
-       case 'T':
-           thread_flag = 1;
-           break;
-       default:
-           usage(prog);
-           return 1;
-       }
+        switch (ret)
+        {
+        case 0:
+            addr = xstrdup(arg);
+            break;
+        case 'n':
+            no_threads = atoi(arg);
+            break;
+        case 'a':
+            apdu_log = xstrdup(arg);
+            break;
+        case 'v':
+            yaz_log_init_level (yaz_log_mask_str(arg));
+            break;
+        case 'T':
+            thread_flag = 1;
+            break;
+        default:
+            usage(prog);
+            return 1;
+        }
     }
 
     SocketManager mySocketManager;
@@ -245,13 +241,13 @@ int main(int argc, char **argv)
     
     MyServer *z = 0;
 
-    Msg_Thread *my_thread = new Msg_Thread(&mySocketManager);
+    Msg_Thread *my_thread = new Msg_Thread(&mySocketManager, no_threads);
 
 #if YAZ_POSIX_THREADS
     if (thread_flag)
-       my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
+        my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
     else
-       my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
+        my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
 #else
     my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
 #endif
@@ -260,13 +256,21 @@ int main(int argc, char **argv)
     z->server(addr);
     if (apdu_log)
     {
-       yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
-       z->set_APDU_log(apdu_log);
+        yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
+        z->set_APDU_log(apdu_log);
     }
 
     while (mySocketManager.processEvent() > 0)
-       ;
+        ;
     delete z;
     delete my_thread;
     return 0;    
 }
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+