X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fclient_wrapper.hxx;h=14dabfb1343e4984a47cf0fed6e4fe6607e1ef88;hp=672308e7cd17484e3f46393eec95a8de654aa52f;hb=e162ddf298a2e8c3756c55995897374ef614cbcd;hpb=e1614a6d1d9c022b83c0aa8cdb948bd7dc98ff23 diff --git a/src/client_wrapper.hxx b/src/client_wrapper.hxx index 672308e..14dabfb 100644 --- a/src/client_wrapper.hxx +++ b/src/client_wrapper.hxx @@ -36,6 +36,12 @@ namespace libt2n { +/** @brief provide access to client_connection singletons and call-handling function + + This is an abstact base class for use with T2nSingletonWrapper. It provides access + to the singleton-client_connection and offers a handle-method which is used for + every call to a method on a t2n-server. +*/ class ConnectionWrapper { private: @@ -57,11 +63,30 @@ class ConnectionWrapper virtual ~ConnectionWrapper() { } + /** @brief return active connection, create new one if not existing + + Return a pointer to an active client_connection. Use detail-data + stored within the derived class to create a new connection if needed, + otherwise return an alredy active connection. The derived class has + to take care of destroying the connection when not needed anymore. + */ virtual client_connection* get_connection()=0; - virtual void handle(command_client* stubBase, boost::function< void() > f) + /** @brief this function is called on every execution of a method on a server + + @param stubBase pointer to the command_client executing the call + @param f boost::function object containing the method to call and all parameters + @retval true if the call was successful and the original return-value of the called function can be used + false if T2nSingletonWrapper has to create a return-value with the default-constructor + + T2nSingletonWrapper will call this function on every execution of a server-side + method. This version will just call the function without any special treatment. + You can overload this function to implement different error handling strategies. + */ + virtual bool handle(command_client* stubBase, boost::function< void() > f) { f(); + return true; } long long get_command_timeout_usec(void) @@ -138,6 +163,13 @@ class T2nSingletonWrapperMessages static const char* NotInitializedMessage; }; +/** @brief wrap calls to server-side-functions with different error-handling strategies + + Template class to access a process-wide singleton server-connection and to wrap all + calls using this connection with an error-handling strategy (e.g. to reconnect when + the connection broke). The source looks very complicated due to heavy use of templates, + look at the 3rd codegen example to see how to use it. +*/ template< class Client > class T2nSingletonWrapper : public T2nSingletonWrapperMessages { @@ -179,7 +211,7 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages if (WrappedConnection.get() == NULL) throw std::logic_error(NotInitializedMessage); - std::auto_ptr stub(new Client(*(WrappedConnection->get_connection()), + std::auto_ptr stub(new Client(WrappedConnection->get_connection(), WrappedConnection->get_command_timeout_usec(), WrappedConnection->get_hello_timeout_usec())); @@ -193,8 +225,17 @@ class T2nSingletonWrapper : public T2nSingletonWrapperMessages ensure_singleton_there(); typename detail::TypeWrap::type result; + + // bind our Client-object and the local result detail::Call 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::type(); + } return result; }