Simplify a little
[metaproxy-moved-to-github.git] / src / filter_frontend_net.cpp
index 5c1f119..71fb5d1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_frontend_net.cpp,v 1.8 2005-11-07 12:31:43 adam Exp $
+/* $Id: filter_frontend_net.cpp,v 1.13 2006-01-09 18:18:07 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -7,6 +7,8 @@
 
 #include "config.hpp"
 
+#include "xmlutil.hpp"
+#include "pipe.hpp"
 #include "filter.hpp"
 #include "router.hpp"
 #include "package.hpp"
@@ -23,7 +25,7 @@ namespace yp2 {
     class My_Timer_Thread : public yazpp_1::ISocketObserver {
     private:
         yazpp_1::ISocketObservable *m_obs;
-        int m_fd[2];
+        Pipe m_pipe;
         bool m_timeout;
     public:
         My_Timer_Thread(yazpp_1::ISocketObservable *obs, int duration);
@@ -227,10 +229,9 @@ bool yp2::My_Timer_Thread::timeout()
 
 yp2::My_Timer_Thread::My_Timer_Thread(yazpp_1::ISocketObservable *obs,
                                 int duration) : 
-    m_obs(obs), m_timeout(false)
+    m_obs(obs), m_pipe(9123), m_timeout(false)
 {
-    pipe(m_fd);
-    obs->addObserver(m_fd[0], this);
+    obs->addObserver(m_pipe.read_fd(), this);
     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
     obs->timeoutObserver(this, duration);
 }
@@ -239,11 +240,12 @@ void yp2::My_Timer_Thread::socketNotify(int event)
 {
     m_timeout = true;
     m_obs->deleteObserver(this);
-    close(m_fd[0]);
-    close(m_fd[1]);
 }
 
 void yp2::filter::FrontendNet::process(Package &package) const {
+    if (m_ports.size() == 0)
+        return;
+
     yazpp_1::SocketManager mySocketManager;
 
     My_Timer_Thread *tt = 0;
@@ -277,6 +279,33 @@ void yp2::filter::FrontendNet::process(Package &package) const {
     delete tt;
 }
 
+void yp2::filter::FrontendNet::configure(const xmlNode * ptr)
+{
+    if (!ptr || !ptr->children)
+    {
+        throw yp2::filter::FilterException("No ports for Frontend");
+    }
+    std::vector<std::string> ports;
+    for (ptr = ptr->children; ptr; ptr = ptr->next)
+    {
+        if (ptr->type != XML_ELEMENT_NODE)
+            continue;
+        if (!strcmp((const char *) ptr->name, "port"))
+        {
+            std::string port = yp2::xml::get_text(ptr);
+            ports.push_back(port);
+            
+        }
+        else
+        {
+            throw yp2::filter::FilterException("Bad element " 
+                                               + std::string((const char *)
+                                                             ptr->name));
+        }
+    }
+    m_ports = ports;
+}
+
 std::vector<std::string> &yp2::filter::FrontendNet::ports()
 {
     return m_ports;
@@ -287,6 +316,19 @@ int &yp2::filter::FrontendNet::listen_duration()
     return m_listen_duration;
 }
 
+static yp2::filter::Base* filter_creator()
+{
+    return new yp2::filter::FrontendNet;
+}
+
+extern "C" {
+    struct yp2_filter_struct yp2_filter_frontend_net = {
+        0,
+        "frontend_net",
+        filter_creator
+    };
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4