From db49c2f908cd5a007e21d27ba614eb4018f92efc Mon Sep 17 00:00:00 2001 From: Jens Thiele Date: Tue, 14 Nov 2006 16:25:56 +0000 Subject: [PATCH] added a first "minimal" example - splitted code to be prepared for generator --- examples/Makefile.am | 13 ++++ examples/README | 3 + examples/TODO | 1 + examples/minimalistic-client-stub.cpp | 24 ++++++++ examples/minimalistic-client-stub.hxx | 23 ++++++++ examples/minimalistic-client.cpp | 20 +++++++ examples/minimalistic-server-stub.cpp | 8 +++ examples/minimalistic-server.cpp | 42 ++++++++++++++ examples/minimalistic-stub.cpp | 19 ++++++ examples/minimalistic-stub.hxx | 99 +++++++++++++++++++++++++++++++++ 10 files changed, 252 insertions(+), 0 deletions(-) create mode 100644 examples/Makefile.am create mode 100644 examples/README create mode 100644 examples/TODO create mode 100644 examples/minimalistic-client-stub.cpp create mode 100644 examples/minimalistic-client-stub.hxx create mode 100644 examples/minimalistic-client.cpp create mode 100644 examples/minimalistic-server-stub.cpp create mode 100644 examples/minimalistic-server.cpp create mode 100644 examples/minimalistic-stub.cpp create mode 100644 examples/minimalistic-stub.hxx diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..3a0058b --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,13 @@ +INCLUDES = -I$(top_srcdir)/src @BOOST_CPPFLAGS@ @CPPUNIT_CFLAGS@ + +LDADD = $(top_builddir)/src/libt2n.la @BOOST_SERIALIZATION_LIB@ @BOOST_LDFLAGS@ + +libminimalistic_client_la_SOURCES = minimalistic-client-stub.cpp +noinst_LTLIBRARIES = libminimalistic-client.la + +minimalistic_client_SOURCES = minimalistic-client.cpp +minimalistic_client_LDADD = $(LDADD) libminimalistic-client.la + +minimalistic_server_SOURCES = minimalistic-server.cpp minimalistic-server-stub.cpp minimalistic-stub.cpp + +noinst_PROGRAMS = minimalistic_client minimalistic_server diff --git a/examples/README b/examples/README new file mode 100644 index 0000000..78b63f9 --- /dev/null +++ b/examples/README @@ -0,0 +1,3 @@ +boost serialization and extended_type_info trouble: +- the problem was that the static initializers generated by the BOOST_CLASS_EXPORT macro haven't been called +this happened because the translation unit of the static library created wasn't used diff --git a/examples/TODO b/examples/TODO new file mode 100644 index 0000000..d6a808d --- /dev/null +++ b/examples/TODO @@ -0,0 +1 @@ +- function to command group mapping? diff --git a/examples/minimalistic-client-stub.cpp b/examples/minimalistic-client-stub.cpp new file mode 100644 index 0000000..b12b642 --- /dev/null +++ b/examples/minimalistic-client-stub.cpp @@ -0,0 +1,24 @@ +#include "minimalistic-client-stub.hxx" +#include "minimalistic-stub.hxx" + +// fake +libt2n::result* testfunc_cmd::operator()() +{ + return NULL; +} + +std::string cmd_group_example_client::testfunc(const std::string& str) +{ + libt2n::result_container rc; + send_command(new testfunc_cmd(str),rc); + + testfunc_res* res=dynamic_cast(rc.get_result()); + if (!res) + throw libt2n::t2n_communication_error("result object of wrong type"); + return res->get_data(); +} + +// include in this compilation unit to ensure the compilation unit is used +// see also: +// http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library +#include "minimalistic-stub.cpp" diff --git a/examples/minimalistic-client-stub.hxx b/examples/minimalistic-client-stub.hxx new file mode 100644 index 0000000..b17baca --- /dev/null +++ b/examples/minimalistic-client-stub.hxx @@ -0,0 +1,23 @@ +/* header file for the library that should be automatically generated */ + +#ifndef MINIMALISTIC_CLIENT_STUB_HXX +#define MINIMALISTIC_CLIENT_STUB_HXX + +#include + +class cmd_group_example_client : public libt2n::command_client +{ + public: + cmd_group_example_client(libt2n::client_connection &_c, + long long _command_timeout_usec=command_timeout_usec_default, + long long _hello_timeout_usec=hello_timeout_usec_default) + : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec) + { } + + std::string testfunc(const std::string& str); +}; + +void +extended_type_info_test(); + +#endif diff --git a/examples/minimalistic-client.cpp b/examples/minimalistic-client.cpp new file mode 100644 index 0000000..b2d8b8a --- /dev/null +++ b/examples/minimalistic-client.cpp @@ -0,0 +1,20 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include + +// include library header +#include "minimalistic-client-stub.hxx" + +int main(int argc, char** argv) +{ + extended_type_info_test(); + + libt2n::socket_client_connection sc("./socket"); + cmd_group_example_client cc(sc); + + return (cc.testfunc("hello")=="hello, testfunc() was here") ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/examples/minimalistic-server-stub.cpp b/examples/minimalistic-server-stub.cpp new file mode 100644 index 0000000..cb30607 --- /dev/null +++ b/examples/minimalistic-server-stub.cpp @@ -0,0 +1,8 @@ +#include "minimalistic-stub.hxx" + +std::string testfunc(const std::string& str); + +libt2n::result* testfunc_cmd::operator()() +{ + return new testfunc_res(testfunc(param)); +} diff --git a/examples/minimalistic-server.cpp b/examples/minimalistic-server.cpp new file mode 100644 index 0000000..46cb9ec --- /dev/null +++ b/examples/minimalistic-server.cpp @@ -0,0 +1,42 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include + +#include "minimalistic-stub.hxx" + +#include +#include + +using namespace std; + +LIBT2P_EXPORT string testfunc(const string& str) +{ + string ret; + if (str=="throw") + throw libt2n::t2n_runtime_error("throw me around"); + if (str=="big") + ret.insert(0,100*1024,'x'); + else + ret=str+", testfunc() was here"; + return ret; +} + +using namespace libt2n; + +int main(int argc, char** argv) { + extended_type_info_test(); + + socket_server ss("./socket"); + group_command_server cs(ss); + + // handle requests (without timeout) + while(true) + cs.handle(); + + return 0; +} diff --git a/examples/minimalistic-stub.cpp b/examples/minimalistic-stub.cpp new file mode 100644 index 0000000..1c2ffcb --- /dev/null +++ b/examples/minimalistic-stub.cpp @@ -0,0 +1,19 @@ +#include "minimalistic-stub.hxx" +#include + +/* register types with boost serialization */ + +BOOST_CLASS_EXPORT(cmd_group_example) +BOOST_CLASS_EXPORT(testfunc_cmd) +BOOST_CLASS_EXPORT(testfunc_res) + +void +extended_type_info_test() { + cmd_group_example* t=new testfunc_cmd(); + const boost::serialization::extended_type_info * true_type + = boost::serialization::type_info_implementation::type::get_derived_extended_type_info(*t); + // note:if this exception is thrown, be sure that derived pointer + // is either regsitered or exported. + assert(NULL != true_type); + assert(std::string("testfunc_cmd")==true_type->get_key()); +} diff --git a/examples/minimalistic-stub.hxx b/examples/minimalistic-stub.hxx new file mode 100644 index 0000000..7888989 --- /dev/null +++ b/examples/minimalistic-stub.hxx @@ -0,0 +1,99 @@ +#ifndef MINIMALISTIC_STUB_HXX +#define MINIMALISTIC_STUB_HXX + + +/* + stubs to be generated + header is used by the client library and the server +*/ + +// todo: why do we have to include the boost headers first? +// do we have to know which archive format we will use? + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef _GCCXML__ +#define LIBT2P_EXPORT __attribute((gccxml("libt2n"))) +#else +#define LIBT2P_EXPORT +#endif + +class cmd_group_example : public libt2n::command +{ + private: + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command); + } +}; + + +class testfunc_res : public libt2n::result +{ + private: + std::string res; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result); + ar & BOOST_SERIALIZATION_NVP(res); + } + + public: + testfunc_res() + { } + + testfunc_res(const std::string& str) + { + res=str; + } + + std::string get_data() + { + return res; + } +}; + + +class testfunc_cmd : public cmd_group_example +{ + private: + std::string param; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_example); + ar & BOOST_SERIALIZATION_NVP(param); + } + + public: + testfunc_cmd() + { } + + testfunc_cmd(const std::string& str) + { + param=str; + } + + libt2n::result* operator()(); +}; + +void +extended_type_info_test(); + +#endif -- 1.7.1