libt2n: (gerd) fix client-connection-logic, finish wrappers, all tests are working...
[libt2n] / src / client_wrapper.hxx
index 0822af0..ebc9e25 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>
@@ -39,27 +38,48 @@ namespace libt2n
 
 class ConnectionWrapper
 {
+    private:
+        long long command_timeout_usec;
+        long long hello_timeout_usec;
+
+    protected:
+        log_level_values log_level;
+        std::ostream *logstream;
+        void set_logging_on_connection(client_connection& c);
+
     public:
+        ConnectionWrapper()
+            : log_level(none), logstream(NULL), 
+              command_timeout_usec(command_client::command_timeout_usec_default),
+              hello_timeout_usec(command_client::hello_timeout_usec_default)
+            { }
+
+        virtual ~ConnectionWrapper()
+            { }
 
         virtual client_connection* get_connection()=0;
 
-        virtual long long get_command_timeout_usec(void)
+        virtual bool handle(command_client* stubBase, boost::function< void() > f)
         {
-            return command_client::command_timeout_usec_default;
+            f();
+            return true;
         }
 
-        virtual long long get_hello_timeout_usec(void)
-        {
-            return command_client::hello_timeout_usec_default;
-        }
+        long long get_command_timeout_usec(void)
+            { return command_timeout_usec; }
 
-        virtual void handle(command_client* stubBase, boost::function< void() > f)
-        {
-            f();
-        }
+        void set_command_timeout_usec(long long _command_timeout_usec)
+            { command_timeout_usec=_command_timeout_usec; }
 
-        virtual ~ConnectionWrapper()
-            { }
+        long long get_hello_timeout_usec(void)
+            { return hello_timeout_usec; }
+
+        void set_hello_timeout_usec(long long _hello_timeout_usec)
+            { hello_timeout_usec=_hello_timeout_usec; }
+
+        void set_logging(std::ostream *_logstream, log_level_values _log_level);
+
+        std::ostream* get_logstream(log_level_values level);
 };
 
 // contains the internal stuff needed for T2nWrapper
@@ -150,14 +170,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 >
@@ -167,8 +194,17 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages
             ensure_singleton_there();
 
             typename detail::TypeWrap<R>::type result;
+
+            // bind our Client-object and the local result
             detail::Call<R> call( boost::bind( f, SingletonObject->Stub.get()), result );
-            WrappedConnection->handle(SingletonObject->Stub.get(),call);
+
+            // let the wrapper-handler call the fully-bound function
+            if (!WrappedConnection->handle(SingletonObject->Stub.get(),call))
+            {
+                // create an result with default-constructor if the handler could not
+                // successfully do a call but no exception occured
+                result=typename detail::TypeWrap<R>::type();
+            }
             return result;
         }
 
@@ -182,13 +218,13 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages
             if (SingletonObject.get() != NULL)
                 SingletonObject.reset();
         }
-        static ConnectionWrapper* get_connection(void)
+        static ConnectionWrapper* get_connection_wrapper(void)
             { return WrappedConnection.get(); }
 
         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