From: Gerd v. Egidy Date: Thu, 19 Oct 2006 10:47:51 +0000 (+0000) Subject: libt2n: (gerd) fix exception passing X-Git-Tag: v0.2~139 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=e453407db5951aa7f504282ea82d1ca1f19d22fb;hp=d535333ffe637c9e547e68b792f334c229641520 libt2n: (gerd) fix exception passing --- diff --git a/src/t2n_exception.cpp b/src/t2n_exception.cpp index 37c71b0..1aae5c4 100644 --- a/src/t2n_exception.cpp +++ b/src/t2n_exception.cpp @@ -17,7 +17,13 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include +#include + #include "t2n_exception.hxx" + #include 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 +void t2n_exception::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception); + ar & BOOST_SERIALIZATION_NVP(message); +} + +template +void t2n_communication_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); +} + +template +void t2n_connect_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); +} + +template +void t2n_server_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); +} + +template +void t2n_transfer_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); +} + +template +void t2n_version_mismatch::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); +} + +template +void t2n_command_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); +} + +template +void t2n_serialization_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); +} + +template +void t2n_runtime_error::serialize(Archive & ar, const unsigned int version) +{ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); +} + +} // namespace libt2n diff --git a/src/t2n_exception.hxx b/src/t2n_exception.hxx index 7191101..4ce11aa 100644 --- a/src/t2n_exception.hxx +++ b/src/t2n_exception.hxx @@ -46,11 +46,7 @@ class t2n_exception : public std::exception friend class boost::serialization::access; template - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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) diff --git a/test/simplecmd.cpp b/test/simplecmd.cpp index 462a03f..b2b5bbd 100644 --- a/test/simplecmd.cpp +++ b/test/simplecmd.cpp @@ -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(rc.get_result())->get_data(); + } + catch(t2n_runtime_error &e) + { ret=e.what(); } + catch(...) + { throw; } + + CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret); + } + } + } };