X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fsocket_wrapper.cpp;h=7bbe20c09cd6e0750619c2e12f6dbbde8493b90d;hp=1ebc00cec374ceb8bc1a74bb232b00d2f1715338;hb=238ad35f4e3b6516d4ba7611b540a0edeea71427;hpb=ffbbf9abeb195a4017c1ede383cc9ab906aa4a0c diff --git a/src/socket_wrapper.cpp b/src/socket_wrapper.cpp index 1ebc00c..7bbe20c 100644 --- a/src/socket_wrapper.cpp +++ b/src/socket_wrapper.cpp @@ -25,22 +25,35 @@ namespace libt2n { +/// set logging for coming and current connections +void BasicSocketWrapper::set_logging(std::ostream *_logstream, log_level_values _log_level) +{ + ConnectionWrapper::set_logging(_logstream,_log_level); + + if (connection_established()) + get_connection()->set_logging(_logstream,_log_level); +} + + +/// return active connection, create new tcp or unix connection if not existing 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)); + (new socket_client_connection(port,server,connect_timeout_usec,max_retries,logstream,log_level)); else if (socket_type == unix_s) c=std::auto_ptr - (new socket_client_connection(path,connect_timeout_usec,max_retries)); + (new socket_client_connection(path,connect_timeout_usec,max_retries,logstream,log_level)); } return c.get(); } -void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) +/// try to reconnect max_retries time if we encounter a t2n_communication_error +/// during execution of the command +bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) { int tries=0; @@ -55,7 +68,7 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v f(); // we were successful - return; + return true; } catch(t2n_connect_error &e) { @@ -78,44 +91,60 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v tries++; } + + return false; } +/// return active connection, return a dummy-connection if we can't establish one client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void) { - client_connection* tmp; + client_connection* tmp=BasicSocketWrapper::get_connection(); - try - { - tmp=BasicSocketWrapper::get_connection(); - } - catch(t2n_connect_error &e) - { - // TODO: return some kind of dummy connection - } - catch(...) + if (tmp->is_closed()) { - throw; + // throw away the closed connection... + c.reset(); + // ...return the dummy one instead + tmp=&dc; } return tmp; } -void ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) +/// try to execute the command, may ignore the command if server not available +bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) { - // TODO: check for dummy connection and try to establish a real one - - try + if (!connection_established()) { - ReconnectSocketWrapper::handle(stubBase,f); - } - catch(t2n_communication_error &e) - { - // ignore + // dummy connection is in place: try to establish a real one + client_connection* tmp=get_connection(); + + if (tmp != &dc) + { + // success! we've got a real connection + stubBase->replace_connection(tmp); + } } - catch(...) + + // only try to handle the call if we've got a real connection + if (connection_established()) { - throw; + try + { + ReconnectSocketWrapper::handle(stubBase,f); + return true; + } + catch(t2n_communication_error &e) + { + // ignore + } + catch(...) + { + throw; + } } + + return false; } }