libt2n: (gerd) basic structure of wrappers done, ignore handler missing, unit tests...
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Wed, 30 Jul 2008 17:27:32 +0000 (17:27 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Wed, 30 Jul 2008 17:27:32 +0000 (17:27 +0000)
src/Makefile.am
src/client_wrapper.cpp
src/client_wrapper.hxx
src/socket_wrapper.cpp [new file with mode: 0644]
src/socket_wrapper.hxx [new file with mode: 0644]
test/Makefile.am
test/wrapper.cpp [new file with mode: 0644]

index 44d23dd..204c59d 100644 (file)
@@ -3,9 +3,10 @@ INCLUDES= $(all_includes)
 
 # the library search path.
 lib_LTLIBRARIES = libt2n.la
-libt2n_la_SOURCES = client.cpp command.cpp command_client.cpp \
-       command_server.cpp connection.cpp container.cpp server.cpp socket_client.cpp \
-       socket_handler.cpp socket_server.cpp t2n_exception.cpp client_wrapper.cpp
+libt2n_la_SOURCES = client.cpp client_wrapper.cpp command.cpp \
+       command_client.cpp command_server.cpp connection.cpp container.cpp server.cpp \
+       socket_client.cpp socket_handler.cpp socket_server.cpp socket_wrapper.cpp \
+       t2n_exception.cpp
 
 # Note:  If you specify a:b:c as the version in the next line,
 #  the library that is made has version (a-c).c.b.  In this
@@ -14,4 +15,5 @@ libt2n_la_LDFLAGS = -version-info 1:0:0
 
 pkginclude_HEADERS = client.hxx client_wrapper.hxx command.hxx \
        command_client.hxx command_server.hxx connection.hxx container.hxx log.hxx server.hxx \
-       socket_client.hxx socket_handler.hxx socket_server.hxx t2n_exception.hxx types.hxx
+       socket_client.hxx socket_handler.hxx socket_server.hxx socket_wrapper.hxx \
+       t2n_exception.hxx types.hxx
index 751815a..af60da9 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
-#include <iostream>
-#include <string>
-
-#include <functional>
-
-#include <boost/any.hpp>
-#include <boost/bind.hpp>
-#include <boost/function.hpp>
-
-#include "../codegen/codegen-stubhead.hxx"
-
-
 #include <client_wrapper.hxx>
 
 namespace libt2n
 {
 
-class testme : public command_client
-{
-    public:
-
-    testme(client_connection &x, long long a, long long b)
-        : command_client(x,100000,10000)
-        { }
-
-    void helloworld(const std::string& text)
-    {
-        std::cout << "Hello world, " << text << std::endl;
-    }
-};
-
 const char* T2nSingletonWrapperMessages::NotInitializedMessage = "T2nSingletonWrapper used before setting initializing connection";
 
-typedef T2nSingletonWrapper<testme> wraptype;
-
-template<>
-std::auto_ptr<wraptype> wraptype::SingletonObject = std::auto_ptr<wraptype>();
-
-template<>
-std::auto_ptr<ConnectionWrapper> wraptype::WrappedConnection = std::auto_ptr<ConnectionWrapper>();
-
-void test(void)
-{
-
-
-    t2n_exec(&testme::helloworld)("gurke");
-
-}
-
 }
index faf906b..4bd1540 100644 (file)
@@ -22,7 +22,6 @@
 #include <functional>
 
 #include <boost/config.hpp>
-#include <boost/any.hpp>
 #include <boost/bind.hpp>
 #include <boost/function.hpp>
 #include <boost/preprocessor.hpp>
diff --git a/src/socket_wrapper.cpp b/src/socket_wrapper.cpp
new file mode 100644 (file)
index 0000000..1ebc00c
--- /dev/null
@@ -0,0 +1,121 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by Gerd v. Egidy                                   *
+ *   gve@intra2net.com                                                     *
+ *                                                                         *
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library 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 Lesser General Public License for more details.                   *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this program; if not, write to the                 *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#include <functional>
+#include <string>
+
+#include <socket_wrapper.hxx>
+
+namespace libt2n
+{
+
+client_connection* BasicSocketWrapper::get_connection(void)
+{
+    if (c.get() == NULL)
+    {
+        if (socket_type == tcp_s)
+            c=std::auto_ptr<socket_client_connection>
+                (new socket_client_connection(port,server,connect_timeout_usec,max_retries));
+        else if (socket_type == unix_s)
+            c=std::auto_ptr<socket_client_connection>
+                (new socket_client_connection(path,connect_timeout_usec,max_retries));
+    }
+
+    return c.get();
+}
+
+void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+{
+    int tries=0;
+
+    while(true)
+    {
+        try
+        {
+            // we always reconnect if something went wrong before:
+            // makes sure the buffers are clean of half-sent packets etc.
+            if (tries > 0)
+                c->reconnect();
+
+            f();
+            // we were successful
+            return;
+        }
+        catch(t2n_connect_error &e)
+        {
+            // reconnect already tries max_tries times to reconnect: we are done if that failed
+            throw(e);
+        }
+        catch(t2n_communication_error &e)
+        {
+            // aborts the loop with an exception after max_retries iterations
+            // retries means that the first try doesn't count: use >
+            if (tries > max_retries)
+                throw(e);
+
+            // otherwise ignore the exception and reconnect in the next iteration
+        }
+        catch(...)
+        {
+            throw;
+        }
+
+        tries++;
+    }
+}
+
+client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
+{
+    client_connection* tmp;
+
+    try
+    {
+        tmp=BasicSocketWrapper::get_connection();
+    }
+    catch(t2n_connect_error &e)
+    {
+        // TODO: return some kind of dummy connection
+    }
+    catch(...)
+    {
+        throw;
+    }
+
+    return tmp;
+}
+
+void ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+{
+    // TODO: check for dummy connection and try to establish a real one
+
+    try
+    {
+        ReconnectSocketWrapper::handle(stubBase,f);
+    }
+    catch(t2n_communication_error &e)
+    {
+        // ignore
+    }
+    catch(...)
+    {
+        throw;
+    }
+}
+
+}
diff --git a/src/socket_wrapper.hxx b/src/socket_wrapper.hxx
new file mode 100644 (file)
index 0000000..7a3ce47
--- /dev/null
@@ -0,0 +1,109 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by Gerd v. Egidy                                   *
+ *   gve@intra2net.com                                                     *
+ *                                                                         *
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library 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 Lesser General Public License for more details.                   *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this program; if not, write to the                 *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#ifndef __LIBT2N_SOCKET_WRAPPER
+#define __LIBT2N_SOCKET_WRAPPER
+
+#include <functional>
+#include <string>
+
+#include <client.hxx>
+#include <command_client.hxx>
+#include <types.hxx>
+#include <client_wrapper.hxx>
+#include <socket_client.hxx>
+
+namespace libt2n
+{
+
+class BasicSocketWrapper : public ConnectionWrapper
+{
+    protected:
+        socket_type_value socket_type;
+
+        std::string path;
+        std::string server;
+        int port;
+
+        long long connect_timeout_usec;
+        int max_retries;
+
+        std::auto_ptr<socket_client_connection> c;
+
+    public:
+        BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
+              max_retries(_max_retries), socket_type(tcp_s)
+            { }
+
+        BasicSocketWrapper(const std::string& _path,
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : path(_path), connect_timeout_usec(_connect_timeout_usec),
+              max_retries(_max_retries), socket_type(unix_s)
+            { }
+
+        client_connection* get_connection(void);
+};
+
+class ReconnectSocketWrapper : public BasicSocketWrapper
+{
+    public:
+        ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
+            { }
+
+        ReconnectSocketWrapper(const std::string& _path,
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
+            { }
+
+        void handle(command_client* stubBase, boost::function< void() > f);
+};
+
+
+class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
+{
+    public:
+        ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
+            { }
+
+        ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
+            long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
+            int _max_retries=socket_client_connection::max_retries_default)
+            : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
+            { }
+
+        client_connection* get_connection(void);
+        void handle(command_client* stubBase, boost::function< void() > f);
+};
+
+
+
+}
+
+#endif
index ed42baf..abeb415 100644 (file)
@@ -5,6 +5,6 @@ check_PROGRAMS = test
 test_LDADD = $(top_builddir)/src/libt2n.la @BOOST_SERIALIZATION_LIB@ \
        @BOOST_LDFLAGS@ @CPPUNIT_LIBS@
 test_SOURCES = callback.cpp cmdgroup.cpp comm.cpp hello.cpp reconnect.cpp \
-       serialize.cpp simplecmd.cpp test.cpp timeout.cpp
+       serialize.cpp simplecmd.cpp test.cpp timeout.cpp wrapper.cpp
 
 TESTS = test
diff --git a/test/wrapper.cpp b/test/wrapper.cpp
new file mode 100644 (file)
index 0000000..2b2e41f
--- /dev/null
@@ -0,0 +1,82 @@
+/***************************************************************************
+ *   Copyright (C) 2004 by Intra2net AG                                    *
+ *   info@intra2net.com                                                    *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <stdexcept>
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <command_client.hxx>
+#include <client_wrapper.hxx>
+#include <socket_wrapper.hxx>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+using namespace std;
+using namespace libt2n;
+using namespace CppUnit;
+
+class testme : public command_client
+{
+    public:
+
+    testme(client_connection &x, long long a, long long b)
+        : command_client(x,100000,10000)
+        { }
+
+    void helloworld(const std::string& text)
+    {
+        std::cout << "Hello world, " << text << std::endl;
+    }
+};
+
+typedef T2nSingletonWrapper<testme> wraptype;
+
+template<>
+std::auto_ptr<wraptype> wraptype::SingletonObject = std::auto_ptr<wraptype>();
+
+template<>
+std::auto_ptr<ConnectionWrapper> wraptype::WrappedConnection = std::auto_ptr<ConnectionWrapper>();
+
+class test_wrapper : public TestFixture
+{
+    CPPUNIT_TEST_SUITE(test_wrapper);
+
+    CPPUNIT_TEST(simple_wrap);
+
+    CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+    void setUp()
+    { }
+
+    void tearDown()
+    { }
+
+    void simple_wrap()
+    {
+//        t2n_exec(&testme::helloworld)("gurke");
+
+        CPPUNIT_ASSERT_EQUAL(true,true);
+    }
+
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper);