[dss-commits] r8769 - dss/trunk/webservices

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Fri Sep 18 15:37:29 CEST 2009


Author: pstaehlin
Date: 2009-09-18 15:37:29 +0200 (Fri, 18 Sep 2009)
New Revision: 8769

Modified:
   dss/trunk/webservices/webservices.cpp
   dss/trunk/webservices/webservices.h
Log:
Delegated work in WebServices to worker threads.


Modified: dss/trunk/webservices/webservices.cpp
===================================================================
--- dss/trunk/webservices/webservices.cpp	2009-09-17 14:54:20 UTC (rev 8768)
+++ dss/trunk/webservices/webservices.cpp	2009-09-18 13:37:29 UTC (rev 8769)
@@ -1,5 +1,8 @@
 #include "webservices.h"
 
+#include <cassert>
+#include <cstdlib>
+
 #include <boost/foreach.hpp>
 
 namespace dss {
@@ -18,18 +21,45 @@
 
   void WebServices::doStart() {
     run();
-  }
+  } // doStart
 
   void WebServices::execute() {
     m_Service.bind(NULL, 8081, 10);
+    for(int iWorker = 0; iWorker < 4; iWorker++) {
+      m_Workers.push_back(new WebServicesWorker(this));
+      m_Workers.back().run();
+    }
     while(!m_Terminated) {
       int socket = m_Service.accept();
       if(socket != SOAP_INVALID_SOCKET) {
-        m_Service.serve();
+        struct soap* req_copy = soap_copy(&m_Service);
+        m_RequestsMutex.lock();
+        m_PendingRequests.push_back(req_copy);
+        m_RequestsMutex.unlock();
+        m_RequestArrived.signal();
+//        m_Service.serve();
       }
     }
   } // execute
 
+  struct soap* WebServices::popPendingRequest() {
+    struct soap* result = NULL;
+    while(!m_Terminated) {
+      m_RequestsMutex.lock();
+      if(!m_PendingRequests.empty()) {
+        result = m_PendingRequests.front();
+        m_PendingRequests.pop_front();
+      }
+      m_RequestsMutex.unlock();
+      if(result == NULL) {
+        m_RequestArrived.waitFor(1000);
+      } else {
+        break;
+      }
+    }
+    return result;
+  } // popPendingRequest
+
   WebServiceSession& WebServices::newSession(soap* _soapRequest, int& token) {
     token = ++m_LastSessionID;
     m_SessionByID[token] = WebServiceSession(token, _soapRequest);
@@ -72,13 +102,36 @@
 
   bool WebServiceSession::isOwner(soap* _soapRequest) {
     return _soapRequest->ip == m_OriginatorIP;
-  }
+  } // isOwner
 
   WebServiceSession& WebServiceSession::operator=(const WebServiceSession& _other) {
     Session::operator=(_other);
     m_OriginatorIP = _other.m_OriginatorIP;
 
     return *this;
-  }
+  } // operator=
 
-}
+
+  //================================================== WebServicesWorker
+
+  WebServicesWorker::WebServicesWorker(WebServices* _services)
+  : Thread("WebServicesWorker"),
+    m_pServices(_services)
+  {
+    assert(m_pServices != NULL);
+  } // ctor
+
+  void WebServicesWorker::execute() {
+    while(!m_Terminated) {
+      struct soap* req = m_pServices->popPendingRequest();
+      if(req != NULL) {
+        soap_serve(req);
+        soap_destroy(req);
+        soap_end(req);
+        soap_done(req);
+        free(req);
+      }
+    }
+  } // execute
+
+} // namespace dss

Modified: dss/trunk/webservices/webservices.h
===================================================================
--- dss/trunk/webservices/webservices.h	2009-09-17 14:54:20 UTC (rev 8768)
+++ dss/trunk/webservices/webservices.h	2009-09-18 13:37:29 UTC (rev 8769)
@@ -7,13 +7,18 @@
 #include "core/datetools.h"
 #include "core/subsystem.h"
 #include "core/session.h"
+#include "core/mutex.h"
+#include "core/syncevent.h"
 
+#include <deque>
+
 #include <boost/ptr_container/ptr_map.hpp>
+#include <boost/ptr_container/ptr_vector.hpp>
 
-#include <map>
-
 namespace dss {
 
+  class WebServicesWorker;
+
   class WebServiceSession : public Session {
   protected:
     uint32_t m_OriginatorIP;
@@ -24,7 +29,7 @@
     bool isOwner(soap* _soapRequest);
 
     WebServiceSession& operator=(const WebServiceSession& _other);
-  };
+  }; // WebServiceSession
 
   typedef boost::ptr_map<const int, WebServiceSession> WebServiceSessionByID;
 
@@ -34,13 +39,16 @@
     dssService m_Service;
     int m_LastSessionID;
     WebServiceSessionByID m_SessionByID;
+    boost::ptr_vector<WebServicesWorker> m_Workers;
+    std::deque<struct soap*> m_PendingRequests;
+    Mutex m_RequestsMutex;
+    SyncEvent m_RequestArrived;
   protected:
     virtual void doStart();
   public:
     WebServices(DSS* _pDSS);
     virtual ~WebServices();
 
-
     WebServiceSession& newSession(soap* _soapRequest, int& token);
     void deleteSession(soap* _soapRequest, const int _token);
     WebServiceSession& getSession(soap* _soapRequest, const int _token);
@@ -48,8 +56,18 @@
     bool isAuthorized(soap* _soapRequest, const int _token);
 
     virtual void execute();
-  };
 
+    struct soap* popPendingRequest();
+  }; // WebServices
+
+  class WebServicesWorker : public Thread {
+  public:
+    WebServicesWorker(WebServices* _services);
+    virtual void execute();
+  private:
+    WebServices* m_pServices;
+  }; // WebServicesWorker
+
 }
 
 #endif /*WEBSERVICES_H_*/



More information about the dss-commits mailing list