libt2n, arnied: (gerd) set logging on existing connections too, show t2n-debugging...
[libt2n] / src / socket_wrapper.cpp
index 1ebc00c..7bbe20c 100644 (file)
 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<socket_client_connection>
-                (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<socket_client_connection>
-                (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;
 }
 
 }