libt2n: (gerd) some refactoring, documentation improvement
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 24 Jul 2008 10:01:48 +0000 (10:01 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 24 Jul 2008 10:01:48 +0000 (10:01 +0000)
doc/Doxyfile.in
src/Makefile.am
src/client.cpp
src/client.hxx
src/client_wrapper.cpp [new file with mode: 0644]
src/client_wrapper.hxx
src/command_client.hxx
src/connection.cpp
src/connection.hxx
src/server.cpp
src/server.hxx

index e16f358..020dfa6 100644 (file)
@@ -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 
index 2b91e29..669ce22 100644 (file)
@@ -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
index 686ac0f..2ce213b 100644 (file)
@@ -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<void ()>& func)
-{
-    callbacks[event].push_back(func);
-}
-
-
-
-void client_connection::do_callbacks(callback_event_type event)
-{
-    std::list<boost::function<void ()> >::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)
 {
index c639bb1..d4c2480 100644 (file)
 #define __LIBT2N_CLIENT
 
 #include <string>
-#include <vector>
-#include <list>
-
-#include <boost/function.hpp>
+#include <iostream>
 
 #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<std::list<boost::function<void ()> > > 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<void ()>& func);
 };
 
 }
diff --git a/src/client_wrapper.cpp b/src/client_wrapper.cpp
new file mode 100644 (file)
index 0000000..5115d20
--- /dev/null
@@ -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 <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>
+
+#include <wrapper_handler_socket.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 0822af0..faf906b 100644 (file)
@@ -150,14 +150,21 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages
 #undef _GEN_PREP
 #undef _GEN_ARG
 
-        T2nSingletonWrapper()
+        T2nSingletonWrapper(std::auto_ptr<Client> stub)
+        {
+            Stub=stub;
+        }
+
+        static void init()
         {
             if (WrappedConnection.get() == NULL)
                 throw std::logic_error(NotInitializedMessage);
 
-            Stub=std::auto_ptr<Client>(new Client(*(WrappedConnection->get_connection()),
+            std::auto_ptr<Client> stub(new Client(*(WrappedConnection->get_connection()),
                 WrappedConnection->get_command_timeout_usec(),
                 WrappedConnection->get_hello_timeout_usec()));
+
+            SingletonObject=std::auto_ptr<T2nSingletonWrapper>(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<T2nSingletonWrapper>(new T2nSingletonWrapper());
+                init();
         }
 
         // create an exec-method for each possible number of parameters
index d420102..257b0a8 100644 (file)
@@ -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; }
 };
 
 }
index d339f9f..b4cf198 100644 (file)
 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<void ()>& 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<boost::function<void ()> >::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<boost::function<void ()> > connection::get_callback_list(callback_event_type event)
+{
+    return callbacks[event];
+}
+
+
 }
index 60ea862..94a4c02 100644 (file)
 #define __LIBT2N_CONNECTION
 
 #include <string>
+#include <vector>
+#include <list>
+#include <iostream>
+
+#include <boost/function.hpp>
 
 #include <netinet/in.h>
 
@@ -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<std::list<boost::function<void ()> > > 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<void ()>& func);
+
+        std::list<boost::function<void ()> > get_callback_list(callback_event_type event);
 };
 
 }
index a49bc7a..32dda77 100644 (file)
@@ -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<void ()>& 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<boost::function<void ()> >::iterator i,ie=callbacks[event].end();
-    for (i=callbacks[event].begin(); i != ie; i++)
-        (*i)();
+    connection::add_callback(event,func);
 }
 
 server::server()
index f3ace10..aa0b232 100644 (file)
@@ -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<std::list<boost::function<void ()> > > 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<void ()>& func);
 };