libt2n: (gerd) fix client-connection-logic, finish wrappers, all tests are working...
[libt2n] / src / socket_wrapper.cpp
index d153716..ac88144 100644 (file)
@@ -40,7 +40,7 @@ client_connection* BasicSocketWrapper::get_connection(void)
     return c.get();
 }
 
-void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
 {
     int tries=0;
 
@@ -55,7 +55,7 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v
 
             f();
             // we were successful
-            return;
+            return true;
         }
         catch(t2n_connect_error &e)
         {
@@ -78,44 +78,58 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v
 
         tries++;
     }
+
+    return false;
 }
 
 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)
+    if (tmp->is_closed())
     {
-        // TODO: return some kind of dummy connection
-    }
-    catch(...)
-    {
-        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)
+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;
 }
 
 }