libt2n: (gerd) basic structure of wrappers done, ignore handler missing, unit tests...
[libt2n] / src / socket_wrapper.cpp
diff --git a/src/socket_wrapper.cpp b/src/socket_wrapper.cpp
new file mode 100644 (file)
index 0000000..1ebc00c
--- /dev/null
@@ -0,0 +1,121 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by Gerd v. Egidy                                   *
+ *   gve@intra2net.com                                                     *
+ *                                                                         *
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU Lesser General Public License for more details.                   *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this program; if not, write to the                 *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#include <functional>
+#include <string>
+
+#include <socket_wrapper.hxx>
+
+namespace libt2n
+{
+
+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));
+        else if (socket_type == unix_s)
+            c=std::auto_ptr<socket_client_connection>
+                (new socket_client_connection(path,connect_timeout_usec,max_retries));
+    }
+
+    return c.get();
+}
+
+void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+{
+    int tries=0;
+
+    while(true)
+    {
+        try
+        {
+            // we always reconnect if something went wrong before:
+            // makes sure the buffers are clean of half-sent packets etc.
+            if (tries > 0)
+                c->reconnect();
+
+            f();
+            // we were successful
+            return;
+        }
+        catch(t2n_connect_error &e)
+        {
+            // reconnect already tries max_tries times to reconnect: we are done if that failed
+            throw(e);
+        }
+        catch(t2n_communication_error &e)
+        {
+            // aborts the loop with an exception after max_retries iterations
+            // retries means that the first try doesn't count: use >
+            if (tries > max_retries)
+                throw(e);
+
+            // otherwise ignore the exception and reconnect in the next iteration
+        }
+        catch(...)
+        {
+            throw;
+        }
+
+        tries++;
+    }
+}
+
+client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
+{
+    client_connection* tmp;
+
+    try
+    {
+        tmp=BasicSocketWrapper::get_connection();
+    }
+    catch(t2n_connect_error &e)
+    {
+        // TODO: return some kind of dummy connection
+    }
+    catch(...)
+    {
+        throw;
+    }
+
+    return tmp;
+}
+
+void ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+{
+    // TODO: check for dummy connection and try to establish a real one
+
+    try
+    {
+        ReconnectSocketWrapper::handle(stubBase,f);
+    }
+    catch(t2n_communication_error &e)
+    {
+        // ignore
+    }
+    catch(...)
+    {
+        throw;
+    }
+}
+
+}