FilterFrontendNet allows listening on multiple ports.
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 14 Oct 2005 16:44:55 +0000 (16:44 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 14 Oct 2005 16:44:55 +0000 (16:44 +0000)
Program ex_filter_frontend_net allows ports to be specified as
options -- as either '--port==port=val' or just 'val'.

src/ex_filter_frontend_net.cpp
src/filter_frontend_net.cpp
src/filter_frontend_net.hpp
src/test_filter_frontend_net.cpp

index 4ae21fa..23c5aa1 100644 (file)
@@ -68,10 +68,15 @@ int main(int argc, char **argv)
             ("help", "produce help message")
             ("duration", po::value<int>(),
              "number of seconds for server to exist")
+            ("port", po::value< std::vector<std::string> >(), "listener port")
             ;
 
+        po::positional_options_description p;
+        p.add("port", -1);
+
         po::variables_map vm;        
-        po::store(po::parse_command_line(argc, argv, desc), vm);
+        po::store(po::command_line_parser(argc, argv).
+                  options(desc).positional(p).run(), vm);
         po::notify(vm);    
 
         if (vm.count("help")) {
@@ -79,17 +84,23 @@ int main(int argc, char **argv)
             return 1;
         }
 
+        if (vm.count("port"))
         {
+            std::vector<std::string> ports = 
+                vm["port"].as< std::vector<std::string> >();
+
+            for (size_t i = 0; i<ports.size(); i++)
+                std::cout << "port " << i << " " << ports[i] << "\n";
+
            yp2::RouterChain router;
 
             // put in frontend first
             yp2::FilterFrontendNet filter_front;
-            filter_front.listen_address() = "tcp:@:9999";
+            filter_front.ports() = ports;
 
             // 0=no time, >0 timeout in seconds
             if (vm.count("duration")) {
-                filter_front.listen_duration() = 
-                    vm["duration"].as<int>();
+                filter_front.listen_duration() = vm["duration"].as<int>();
             }
            router.rule(filter_front);
 
index ba81508..d610cee 100644 (file)
@@ -176,8 +176,9 @@ ZAssocServer::ZAssocServer(yazpp_1::IPDU_Observable *the_PDU_Observable,
 yazpp_1::IPDU_Observer *ZAssocServer::sessionNotify(yazpp_1::IPDU_Observable
                                                 *the_PDU_Observable, int fd)
 {
-    ZAssocServerChild *my = new ZAssocServerChild(the_PDU_Observable, m_thread_pool_observer,
-                                   m_package);
+    ZAssocServerChild *my =
+       new ZAssocServerChild(the_PDU_Observable, m_thread_pool_observer,
+                             m_package);
     return my;
 }
 
@@ -204,7 +205,6 @@ void ZAssocServer::connectNotify()
 FilterFrontendNet::FilterFrontendNet()
 {
     m_no_threads = 5;
-    m_listen_address = "@:9001";
     m_listen_duration = 0;
 }
 
@@ -249,25 +249,36 @@ void FilterFrontendNet::process(Package &package) const {
     if (m_listen_duration)
        tt = new My_Timer_Thread(&mySocketManager, m_listen_duration);
 
-    yazpp_1::PDU_Assoc *my_PDU_Assoc =
-       new yazpp_1::PDU_Assoc(&mySocketManager);
-    
     ThreadPoolSocketObserver threadPool(&mySocketManager, m_no_threads);
 
-    ZAssocServer z(my_PDU_Assoc, &threadPool, &package);
-    z.server(m_listen_address.c_str());
+    ZAssocServer **az = new ZAssocServer *[m_ports.size()];
+
+    // Create ZAssocServer for each port
+    size_t i;
+    for (i = 0; i<m_ports.size(); i++)
+    {
+       // create a PDU assoc object (one per ZAssocServer)
+       yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&mySocketManager);
 
+       // create ZAssoc with PDU Assoc
+       az[i] = new ZAssocServer(as, &threadPool, &package);
+       az[i]->server(m_ports[i].c_str());
+    }
     while (mySocketManager.processEvent() > 0)
     {
        if (tt && tt->timeout())
            break;
     }
+    for (i = 0; i<m_ports.size(); i++)
+       delete az[i];
+
+    delete [] az;
     delete tt;
 }
 
-std::string &FilterFrontendNet::listen_address()
+std::vector<std::string> &FilterFrontendNet::ports()
 {
-    return m_listen_address;
+    return m_ports;
 }
 
 int &FilterFrontendNet::listen_duration()
index d00945f..b1c6bd4 100644 (file)
@@ -3,6 +3,7 @@
 #define FILTER_FRONEND_NET_HPP
 
 #include <stdexcept>
+#include <vector>
 
 #include "filter.hpp"
 
@@ -13,11 +14,11 @@ namespace yp2 {
        void process(yp2::Package & package) const;
     private:
         int m_no_threads;
-        std::string m_listen_address;
+        std::vector<std::string> m_ports;
         int m_listen_duration;
     public:
         /// set function - left val in assignment
-        std::string & listen_address();
+        std::vector<std::string> &ports();
         int &listen_duration();
     };
 }
index 1c9b8c2..f6b9c5d 100644 (file)
@@ -102,7 +102,10 @@ BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
 
             // put in frontend first
             yp2::FilterFrontendNet filter_front;
-            filter_front.listen_address() = "unix:socket";
+
+            std::vector <std::string> ports;
+            ports.insert(ports.begin(), "unix:socket");
+            filter_front.ports() = ports;
             filter_front.listen_duration() = 1;  // listen a short time only
            router.rule(filter_front);