libt2n: (gerd) fix exception passing
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 19 Oct 2006 10:47:51 +0000 (10:47 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 19 Oct 2006 10:47:51 +0000 (10:47 +0000)
src/t2n_exception.cpp
src/t2n_exception.hxx
test/simplecmd.cpp

index 37c71b0..1aae5c4 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <boost/archive/binary_oarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+
 #include "t2n_exception.hxx"
+
 #include <boost/serialization/export.hpp>
 
 BOOST_CLASS_EXPORT(libt2n::t2n_exception)
@@ -29,3 +35,63 @@ BOOST_CLASS_EXPORT(libt2n::t2n_version_mismatch)
 BOOST_CLASS_EXPORT(libt2n::t2n_command_error)
 BOOST_CLASS_EXPORT(libt2n::t2n_serialization_error)
 BOOST_CLASS_EXPORT(libt2n::t2n_runtime_error)
+
+namespace libt2n
+{
+
+template<class Archive>
+void t2n_exception::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception);
+    ar & BOOST_SERIALIZATION_NVP(message);
+}
+
+template<class Archive>
+void t2n_communication_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
+}
+
+template<class Archive>
+void t2n_connect_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
+}
+
+template<class Archive>
+void t2n_server_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
+}
+
+template<class Archive>
+void t2n_transfer_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
+}
+
+template<class Archive>
+void t2n_version_mismatch::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
+}
+
+template<class Archive>
+void t2n_command_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
+}
+
+template<class Archive>
+void t2n_serialization_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
+}
+
+template<class Archive>
+void t2n_runtime_error::serialize(Archive & ar, const unsigned int version)
+{
+    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
+}
+
+} // namespace libt2n
index 7191101..4ce11aa 100644 (file)
@@ -46,11 +46,7 @@ class t2n_exception : public std::exception
 
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception);
-            ar & BOOST_SERIALIZATION_NVP(message);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_exception(const std::string& _message)
@@ -78,10 +74,7 @@ class t2n_communication_error : public t2n_exception
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_communication_error(const std::string& _message)
@@ -104,10 +97,7 @@ class t2n_connect_error : public t2n_communication_error
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_connect_error(const std::string& _message)
@@ -130,10 +120,7 @@ class t2n_server_error : public t2n_communication_error
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_server_error(const std::string& _message)
@@ -156,10 +143,7 @@ class t2n_transfer_error : public t2n_communication_error
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_transfer_error(const std::string& _message)
@@ -182,10 +166,7 @@ class t2n_version_mismatch : public t2n_communication_error
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_version_mismatch(const std::string& _message)
@@ -208,10 +189,7 @@ class t2n_command_error : public t2n_exception
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_command_error(const std::string& _message)
@@ -234,10 +212,7 @@ class t2n_serialization_error : public t2n_exception
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_serialization_error(const std::string& _message)
@@ -262,10 +237,7 @@ class t2n_runtime_error : public t2n_exception
     private:
         friend class boost::serialization::access;
         template<class Archive>
-        void serialize(Archive & ar, const unsigned int version)
-        {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
-        }
+        void serialize(Archive & ar, const unsigned int version);
 
     public:
         t2n_runtime_error(const std::string& _message)
index 462a03f..b2b5bbd 100644 (file)
@@ -36,6 +36,8 @@ using namespace CppUnit;
 
 string testfunc(const string& str)
 {
+    if (str=="throw")
+        throw libt2n::t2n_runtime_error("throw me around");
     string ret(str);
     ret+=", testfunc() was here";
     return ret;
@@ -110,6 +112,7 @@ class test_simplecmd : public TestFixture
     CPPUNIT_TEST_SUITE(test_simplecmd);
 
     CPPUNIT_TEST(SimpleCmd);
+    CPPUNIT_TEST(SimpleException);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -166,6 +169,58 @@ class test_simplecmd : public TestFixture
         }
     }
 
+    void SimpleException()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+                command_server cs(ss);
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                    cs.handle(1000000);
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("./socket");
+                sc.set_logging(&cerr,debug);
+                command_client cc(sc);
+
+                result_container rc;
+                cc.send_command(new testfunc_cmd("throw"),rc);
+
+                string ret;
+
+                try
+                {
+                    ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
+                }
+                catch(t2n_runtime_error &e)
+                    { ret=e.what(); }
+                catch(...)
+                    { throw; }
+
+                CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
+            }
+        }
+    }
 
 };