From a64066eb0e456c92c4c06959616443e531d4b39d Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Thu, 24 Jul 2008 10:01:48 +0000 Subject: [PATCH] libt2n: (gerd) some refactoring, documentation improvement --- doc/Doxyfile.in | 2 +- src/Makefile.am | 19 +++++++++---- src/client.cpp | 43 +---------------------------- src/client.hxx | 17 +----------- src/client_wrapper.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/client_wrapper.hxx | 13 +++++++-- src/command_client.hxx | 9 ++++++ src/connection.cpp | 58 +++++++++++++++++++++++++++++++++++++++ src/connection.hxx | 23 ++++++++++++--- src/server.cpp | 37 +----------------------- src/server.hxx | 9 ------ 11 files changed, 184 insertions(+), 117 deletions(-) create mode 100644 src/client_wrapper.cpp diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index e16f358..020dfa6 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -449,7 +449,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = @top_srcdir@/doc @top_srcdir@/src @top_srcdir@/codegen +INPUT = @top_srcdir@/doc @top_srcdir@/src @top_srcdir@/codegen/codegen-stubhead.hxx # If the value of the INPUT tag contains directories, you can use the # FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 2b91e29..669ce22 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,10 +3,17 @@ INCLUDES= $(all_includes) # the library search path. lib_LTLIBRARIES = libt2n.la -libt2n_la_SOURCES = server.cpp socket_server.cpp client.cpp connection.cpp \ - socket_handler.cpp socket_client.cpp command_server.cpp command_client.cpp \ - t2n_exception.cpp command.cpp container.cpp +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 + +# 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 +# example, the version is 2.1.2. (3:2:1) +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 \ + wrapper_handler_socket.hxx -pkginclude_HEADERS = server.hxx socket_server.hxx t2n_exception.hxx client.hxx \ - socket_client.hxx connection.hxx types.hxx socket_handler.hxx command.hxx container.hxx \ - command_client.hxx command_server.hxx log.hxx diff --git a/src/client.cpp b/src/client.cpp index 686ac0f..2ce213b 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -25,52 +25,11 @@ namespace libt2n { client_connection::client_connection() - : connection(), callbacks(__events_end) + : connection() { set_logging(NULL,none); } -client_connection::~client_connection() -{ - // we want the connection_closed callbacks to be called before - close(); - - do_callbacks(connection_deleted); -} - -void client_connection::close() -{ - if (!is_closed()) - { - connection::close(); - do_callbacks(connection_closed); - } -} - -/// add a callback -/** - @param event event the function will be called at - @param func functor (see boost function) that will be called - @note use boost::bind to bind to member functions and parameters like this: - @verbatim - // in this example 17 is a fixed parameter that is always added to the call - c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17)); - @endverbatim -*/ -void client_connection::add_callback(callback_event_type event, const boost::function& func) -{ - callbacks[event].push_back(func); -} - - - -void client_connection::do_callbacks(callback_event_type event) -{ - std::list >::iterator i,ie=callbacks[event].end(); - for (i=callbacks[event].begin(); i != ie; i++) - (*i)(); -} - /// get pointer to logging stream, returns NULL if no logging needed std::ostream* client_connection::get_logstream(log_level_values level) { diff --git a/src/client.hxx b/src/client.hxx index c639bb1..d4c2480 100644 --- a/src/client.hxx +++ b/src/client.hxx @@ -20,10 +20,7 @@ #define __LIBT2N_CLIENT #include -#include -#include - -#include +#include #include "connection.hxx" #include "types.hxx" @@ -39,24 +36,12 @@ class client_connection : public connection log_level_values log_level; std::ostream *logstream; - /// vector initialized for all callback-types, all elements in each list will be called - std::vector > > callbacks; - - protected: - void do_callbacks(callback_event_type event); - public: client_connection(); - virtual ~client_connection(); - - void close(); - void set_logging(std::ostream *_logstream, log_level_values _log_level); std::ostream* get_logstream(log_level_values level); - - void add_callback(callback_event_type event, const boost::function& func); }; } diff --git a/src/client_wrapper.cpp b/src/client_wrapper.cpp new file mode 100644 index 0000000..5115d20 --- /dev/null +++ b/src/client_wrapper.cpp @@ -0,0 +1,71 @@ +/*************************************************************************** + * 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 + +#include +#include +#include + +#include "../codegen/codegen-stubhead.hxx" + + +#include + +#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 0822af0..faf906b 100644 --- a/src/client_wrapper.hxx +++ b/src/client_wrapper.hxx @@ -150,14 +150,21 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages #undef _GEN_PREP #undef _GEN_ARG - T2nSingletonWrapper() + T2nSingletonWrapper(std::auto_ptr stub) + { + Stub=stub; + } + + static void init() { if (WrappedConnection.get() == NULL) throw std::logic_error(NotInitializedMessage); - Stub=std::auto_ptr(new Client(*(WrappedConnection->get_connection()), + std::auto_ptr stub(new Client(*(WrappedConnection->get_connection()), WrappedConnection->get_command_timeout_usec(), WrappedConnection->get_hello_timeout_usec())); + + SingletonObject=std::auto_ptr(new T2nSingletonWrapper(stub)); } template< typename R > @@ -188,7 +195,7 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages static void ensure_singleton_there(void) { if (SingletonObject.get() == NULL) - SingletonObject=std::auto_ptr(new T2nSingletonWrapper()); + init(); } // create an exec-method for each possible number of parameters diff --git a/src/command_client.hxx b/src/command_client.hxx index d420102..257b0a8 100644 --- a/src/command_client.hxx +++ b/src/command_client.hxx @@ -49,6 +49,15 @@ class command_client virtual ~command_client() {} void send_command(command* cmd, result_container &res); + + void set_command_timeout_usec(long long _command_timeout_usec=command_timeout_usec_default) + { command_timeout_usec=_command_timeout_usec; } + void set_hello_timeout_usec(long long _hello_timeout_usec=hello_timeout_usec_default) + { hello_timeout_usec=_hello_timeout_usec; } + long long get_command_timeout_usec(void) + { return command_timeout_usec; } + long long get_hello_timeout_usec(void) + { return hello_timeout_usec; } }; } diff --git a/src/connection.cpp b/src/connection.cpp index d339f9f..b4cf198 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -28,6 +28,23 @@ namespace libt2n { +connection::~connection() +{ + // we want the connection_closed callbacks to be called before + close(); + + do_callbacks(connection_deleted); +} + +void connection::close() +{ + if (!is_closed()) + { + closed=true; + do_callbacks(connection_closed); + } +} + connection::packet_size_indicator connection::bytes_available() { // no size information -> no packet @@ -97,4 +114,45 @@ void connection::write(const std::string& data) real_write(send_data); } +/** @brief add a callback + + @param event event the function will be called at + @param func functor (see boost function) that will be called + + @note use boost::bind to bind to member functions and parameters like this: + 17 is a fixed parameter that is always added to the call + c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17)); +*/ +void connection::add_callback(callback_event_type event, const boost::function& func) +{ + if (event == new_connection) + throw std::logic_error("new_connection callback not allowed for server_connections"); + + callbacks[event].push_back(func); +} + +/** @brief an event has occured, execute the callbacks that are registered for this event + + @param event event type that has occured +*/ +void connection::do_callbacks(callback_event_type event) +{ + std::list >::iterator i,ie=callbacks[event].end(); + for (i=callbacks[event].begin(); i != ie; i++) + (*i)(); +} + +/** @brief get the callbacks in place for one event + + @param event event the callbacks should be registered for + @return std::list of functors (boost::function) with the callbacks + + @note if you want to get the callbacks for all events, loop from 0 to __events_end +*/ +std::list > connection::get_callback_list(callback_event_type event) +{ + return callbacks[event]; +} + + } diff --git a/src/connection.hxx b/src/connection.hxx index 60ea862..94a4c02 100644 --- a/src/connection.hxx +++ b/src/connection.hxx @@ -20,6 +20,11 @@ #define __LIBT2N_CONNECTION #include +#include +#include +#include + +#include #include @@ -35,9 +40,13 @@ class connection private: bool closed; + /// vector initialized for all callback-types, all elements in each list will be called + std::vector > > callbacks; + protected: connection() - { closed=false; } + : callbacks(__events_end), closed(false) + { } std::string buffer; @@ -49,17 +58,17 @@ class connection virtual std::ostream* get_logstream(log_level_values level)=0; + void do_callbacks(callback_event_type event); + public: - virtual ~connection() - { close(); } + virtual ~connection(); /// is this connection closed or not bool is_closed() { return closed; } /// close this connection - virtual void close() - { closed=true; } + virtual void close(); /** @brief look for new data and store it in the local buffer @param usec_timeout wait until new data is found, max timeout usecs. @@ -81,6 +90,10 @@ class connection { return bytes_available(); } void write(const std::string& data); + + void add_callback(callback_event_type event, const boost::function& func); + + std::list > get_callback_list(callback_event_type event); }; } diff --git a/src/server.cpp b/src/server.cpp index a49bc7a..32dda77 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -29,7 +29,7 @@ namespace libt2n { server_connection::server_connection(int _timeout) - : connection(), callbacks(__events_end) + : connection() { set_timeout(_timeout); reset_timeout(); @@ -37,23 +37,6 @@ server_connection::server_connection(int _timeout) my_server=0; } -server_connection::~server_connection() -{ - // we want the connection_closed callbacks to be called before - close(); - - do_callbacks(connection_deleted); -} - -void server_connection::close() -{ - if (!is_closed()) - { - connection::close(); - do_callbacks(connection_closed); - } -} - /// get pointer to logging stream, returns NULL if no logging needed std::ostream* server_connection::get_logstream(log_level_values level) { @@ -84,28 +67,12 @@ void server_connection::reset_timeout() last_action_time=time(NULL); } -/** @brief add a callback - - @param event event the function will be called at - @param func functor (see boost function) that will be called - - @note use boost::bind to bind to member functions and parameters like this: - 17 is a fixed parameter that is always added to the call - c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17)); -*/ void server_connection::add_callback(callback_event_type event, const boost::function& func) { if (event == new_connection) throw std::logic_error("new_connection callback not allowed for server_connections"); - callbacks[event].push_back(func); -} - -void server_connection::do_callbacks(callback_event_type event) -{ - std::list >::iterator i,ie=callbacks[event].end(); - for (i=callbacks[event].begin(); i != ie; i++) - (*i)(); + connection::add_callback(event,func); } server::server() diff --git a/src/server.hxx b/src/server.hxx index f3ace10..aa0b232 100644 --- a/src/server.hxx +++ b/src/server.hxx @@ -56,9 +56,6 @@ class server_connection : public connection void set_id(unsigned int _connection_id) { connection_id=_connection_id; } - /// vector initialized for all callback-types, all elements in each list will be called - std::vector > > callbacks; - protected: server *my_server; @@ -66,11 +63,7 @@ class server_connection : public connection std::ostream* get_logstream(log_level_values level); - void do_callbacks(callback_event_type event); - public: - virtual ~server_connection(); - void check_timeout(); void reset_timeout(); void set_timeout(int _timeout) @@ -80,8 +73,6 @@ class server_connection : public connection unsigned int get_id() { return connection_id; } - void close(); - void add_callback(callback_event_type event, const boost::function& func); }; -- 1.7.1