added a first "minimal" example - splitted code to be prepared for generator
authorJens Thiele <jens.thiele@intra2net.com>
Tue, 14 Nov 2006 16:25:56 +0000 (16:25 +0000)
committerJens Thiele <jens.thiele@intra2net.com>
Tue, 14 Nov 2006 16:25:56 +0000 (16:25 +0000)
examples/Makefile.am [new file with mode: 0644]
examples/README [new file with mode: 0644]
examples/TODO [new file with mode: 0644]
examples/minimalistic-client-stub.cpp [new file with mode: 0644]
examples/minimalistic-client-stub.hxx [new file with mode: 0644]
examples/minimalistic-client.cpp [new file with mode: 0644]
examples/minimalistic-server-stub.cpp [new file with mode: 0644]
examples/minimalistic-server.cpp [new file with mode: 0644]
examples/minimalistic-stub.cpp [new file with mode: 0644]
examples/minimalistic-stub.hxx [new file with mode: 0644]

diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644 (file)
index 0000000..3a0058b
--- /dev/null
@@ -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 (file)
index 0000000..78b63f9
--- /dev/null
@@ -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 (file)
index 0000000..d6a808d
--- /dev/null
@@ -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 (file)
index 0000000..b12b642
--- /dev/null
@@ -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<testfunc_res*>(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 (file)
index 0000000..b17baca
--- /dev/null
@@ -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 <command_client.hxx>
+
+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 (file)
index 0000000..b2d8b8a
--- /dev/null
@@ -0,0 +1,20 @@
+/***************************************************************************
+ *   Copyright (C) 2004 by Intra2net AG                                    *
+ *   info@intra2net.com                                                    *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <socket_client.hxx>
+
+// 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 (file)
index 0000000..cb30607
--- /dev/null
@@ -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 (file)
index 0000000..46cb9ec
--- /dev/null
@@ -0,0 +1,42 @@
+/***************************************************************************
+ *   Copyright (C) 2004 by Intra2net AG                                    *
+ *   info@intra2net.com                                                    *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <string>
+#include <stdexcept>
+
+#include "minimalistic-stub.hxx"
+
+#include <socket_server.hxx>
+#include <command_server.hxx>
+
+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<cmd_group_example> 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 (file)
index 0000000..1c2ffcb
--- /dev/null
@@ -0,0 +1,19 @@
+#include "minimalistic-stub.hxx"
+#include <boost/serialization/export.hpp>
+
+/* 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<libt2n::command>::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 (file)
index 0000000..7888989
--- /dev/null
@@ -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 <boost/archive/binary_oarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/string.hpp>
+
+#include <string>
+#include <t2n_exception.hxx>
+#include <command.hxx>
+
+#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<class Archive>
+        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<class Archive>
+        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<class Archive>
+        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