From: Gerd v. Egidy Date: Wed, 30 Jul 2008 17:27:32 +0000 (+0000) Subject: libt2n: (gerd) basic structure of wrappers done, ignore handler missing, unit tests... X-Git-Tag: v0.4~16 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=ffbbf9abeb195a4017c1ede383cc9ab906aa4a0c libt2n: (gerd) basic structure of wrappers done, ignore handler missing, unit tests missing --- diff --git a/src/Makefile.am b/src/Makefile.am index 44d23dd..204c59d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/client_wrapper.cpp b/src/client_wrapper.cpp index 751815a..af60da9 100644 --- a/src/client_wrapper.cpp +++ b/src/client_wrapper.cpp @@ -17,53 +17,11 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include -#include - -#include - -#include -#include -#include - -#include "../codegen/codegen-stubhead.hxx" - - #include 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 wraptype; - -template<> -std::auto_ptr wraptype::SingletonObject = std::auto_ptr(); - -template<> -std::auto_ptr wraptype::WrappedConnection = std::auto_ptr(); - -void test(void) -{ - - - t2n_exec(&testme::helloworld)("gurke"); - -} - } diff --git a/src/client_wrapper.hxx b/src/client_wrapper.hxx index faf906b..4bd1540 100644 --- a/src/client_wrapper.hxx +++ b/src/client_wrapper.hxx @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/src/socket_wrapper.cpp b/src/socket_wrapper.cpp new file mode 100644 index 0000000..1ebc00c --- /dev/null +++ b/src/socket_wrapper.cpp @@ -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 +#include + +#include + +namespace libt2n +{ + +client_connection* BasicSocketWrapper::get_connection(void) +{ + if (c.get() == NULL) + { + if (socket_type == tcp_s) + c=std::auto_ptr + (new socket_client_connection(port,server,connect_timeout_usec,max_retries)); + else if (socket_type == unix_s) + c=std::auto_ptr + (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 index 0000000..7a3ce47 --- /dev/null +++ b/src/socket_wrapper.hxx @@ -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 +#include + +#include +#include +#include +#include +#include + +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 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 diff --git a/test/Makefile.am b/test/Makefile.am index ed42baf..abeb415 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 index 0000000..2b2e41f --- /dev/null +++ b/test/wrapper.cpp @@ -0,0 +1,82 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#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 wraptype; + +template<> +std::auto_ptr wraptype::SingletonObject = std::auto_ptr(); + +template<> +std::auto_ptr wraptype::WrappedConnection = std::auto_ptr(); + +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);