Use scoped_ptr for private data in FrontendNet. Allow threads to be set
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Jan 2006 21:19:11 +0000 (21:19 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Jan 2006 21:19:11 +0000 (21:19 +0000)
etc/config1.xml
src/filter_frontend_net.cpp
src/filter_frontend_net.hpp

index b13674c..d529e11 100644 (file)
@@ -1,9 +1,10 @@
 <?xml version="1.0"?>
-<!-- $Id: config1.xml,v 1.6 2006-01-09 18:19:09 adam Exp $ -->
+<!-- $Id: config1.xml,v 1.7 2006-01-09 21:19:11 adam Exp $ -->
 <yp2 xmlns="http://indexdata.dk/yp2/config/1">
   <start route="start"/>
   <filters>
     <filter id="frontend" type="frontend_net">
+      <threads>10</threads>
       <port>@:9000</port>
     </filter>
     <filter id="backend" type="z3950_client">
index 71fb5d1..cd4dd14 100644 (file)
@@ -1,16 +1,14 @@
-/* $Id: filter_frontend_net.cpp,v 1.13 2006-01-09 18:18:07 adam Exp $
+/* $Id: filter_frontend_net.cpp,v 1.14 2006-01-09 21:19:11 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
  */
 
-
 #include "config.hpp"
 
 #include "xmlutil.hpp"
 #include "pipe.hpp"
 #include "filter.hpp"
-#include "router.hpp"
 #include "package.hpp"
 #include "thread_pool_observer.hpp"
 #include "filter_frontend_net.hpp"
 #include <iostream>
 
 namespace yp2 {
+    namespace filter {
+        class FrontendNet::Rep {
+            friend class FrontendNet;
+            int m_no_threads;
+            std::vector<std::string> m_ports;
+            int m_listen_duration;
+        };
+    }
     class My_Timer_Thread : public yazpp_1::ISocketObserver {
     private:
         yazpp_1::ISocketObservable *m_obs;
@@ -215,12 +221,15 @@ void yp2::ZAssocServer::connectNotify()
 {
 }
 
-yp2::filter::FrontendNet::FrontendNet()
+yp2::filter::FrontendNet::FrontendNet() : m_p(new Rep)
 {
-    m_no_threads = 5;
-    m_listen_duration = 0;
+    m_p->m_no_threads = 5;
+    m_p->m_listen_duration = 0;
 }
 
+yp2::filter::FrontendNet::~FrontendNet()
+{
+}
 
 bool yp2::My_Timer_Thread::timeout()
 {
@@ -242,37 +251,38 @@ void yp2::My_Timer_Thread::socketNotify(int event)
     m_obs->deleteObserver(this);
 }
 
-void yp2::filter::FrontendNet::process(Package &package) const {
-    if (m_ports.size() == 0)
+void yp2::filter::FrontendNet::process(Package &package) const
+{
+    if (m_p->m_ports.size() == 0)
         return;
 
     yazpp_1::SocketManager mySocketManager;
 
     My_Timer_Thread *tt = 0;
-    if (m_listen_duration)
-       tt = new My_Timer_Thread(&mySocketManager, m_listen_duration);
+    if (m_p->m_listen_duration)
+       tt = new My_Timer_Thread(&mySocketManager, m_p->m_listen_duration);
 
-    ThreadPoolSocketObserver threadPool(&mySocketManager, m_no_threads);
+    ThreadPoolSocketObserver threadPool(&mySocketManager, m_p->m_no_threads);
 
-    yp2::ZAssocServer **az = new yp2::ZAssocServer *[m_ports.size()];
+    yp2::ZAssocServer **az = new yp2::ZAssocServer *[m_p->m_ports.size()];
 
     // Create yp2::ZAssocServer for each port
     size_t i;
-    for (i = 0; i<m_ports.size(); i++)
+    for (i = 0; i<m_p->m_ports.size(); i++)
     {
        // create a PDU assoc object (one per yp2::ZAssocServer)
        yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&mySocketManager);
 
        // create ZAssoc with PDU Assoc
        az[i] = new yp2::ZAssocServer(as, &threadPool, &package);
-       az[i]->server(m_ports[i].c_str());
+       az[i]->server(m_p->m_ports[i].c_str());
     }
     while (mySocketManager.processEvent() > 0)
     {
        if (tt && tt->timeout())
            break;
     }
-    for (i = 0; i<m_ports.size(); i++)
+    for (i = 0; i<m_p->m_ports.size(); i++)
        delete az[i];
 
     delete [] az;
@@ -296,6 +306,15 @@ void yp2::filter::FrontendNet::configure(const xmlNode * ptr)
             ports.push_back(port);
             
         }
+        else if (!strcmp((const char *) ptr->name, "threads"))
+        {
+            std::string threads_str = yp2::xml::get_text(ptr);
+            int threads = atoi(threads_str.c_str());
+            if (threads < 1)
+                throw yp2::filter::FilterException("Bad value for threads: " 
+                                                   + threads_str);
+            m_p->m_no_threads = threads;
+        }
         else
         {
             throw yp2::filter::FilterException("Bad element " 
@@ -303,17 +322,17 @@ void yp2::filter::FrontendNet::configure(const xmlNode * ptr)
                                                              ptr->name));
         }
     }
-    m_ports = ports;
+    m_p->m_ports = ports;
 }
 
 std::vector<std::string> &yp2::filter::FrontendNet::ports()
 {
-    return m_ports;
+    return m_p->m_ports;
 }
 
 int &yp2::filter::FrontendNet::listen_duration()
 {
-    return m_listen_duration;
+    return m_p->m_listen_duration;
 }
 
 static yp2::filter::Base* filter_creator()
index ff168ff..546b5b1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_frontend_net.hpp,v 1.11 2006-01-09 13:43:59 adam Exp $
+/* $Id: filter_frontend_net.hpp,v 1.12 2006-01-09 21:19:11 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -7,6 +7,8 @@
 #ifndef FILTER_FRONTEND_NET_HPP
 #define FILTER_FRONTEND_NET_HPP
 
+#include <boost/scoped_ptr.hpp>
+
 #include <stdexcept>
 #include <vector>
 
 namespace yp2 {
     namespace filter {
         class FrontendNet : public Base {
-            class ZAssocServerChild;
+            class Rep;
+            boost::scoped_ptr<Rep> m_p;
         public:
-            FrontendNet::FrontendNet();
+            FrontendNet();
+            ~FrontendNet();
             void process(yp2::Package & package) const;
             void configure(const xmlNode * ptr);
-        private:
-            int m_no_threads;
-            std::vector<std::string> m_ports;
-            int m_listen_duration;
         public:
-            /// set function - left val in assignment
+            /// set ports
             std::vector<std::string> &ports();
+            // set liten duraction (number of seconcds to listen)
             int &listen_duration();
         };
     }