libt2n: (gerd) basic command handling (still some todos)
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 12 Oct 2006 16:18:00 +0000 (16:18 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 12 Oct 2006 16:18:00 +0000 (16:18 +0000)
13 files changed:
src/Makefile.am
src/command.cpp [new file with mode: 0644]
src/command.hxx [new file with mode: 0644]
src/command_client.cpp [new file with mode: 0644]
src/command_client.hxx [new file with mode: 0644]
src/command_server.cpp [new file with mode: 0644]
src/command_server.hxx [new file with mode: 0644]
src/container.cpp [new file with mode: 0644]
src/container.hxx [new file with mode: 0644]
src/socket_client.cpp [new file with mode: 0644]
src/t2n_exception.cpp [new file with mode: 0644]
src/t2n_exception.hxx
test/comm.cpp

index d8ba3e6..ec5b0f7 100644 (file)
@@ -6,7 +6,9 @@ INCLUDES= $(all_includes)
 lib_LTLIBRARIES = libt2n.la
 libt2n_la_LDFLAGS = -module
 libt2n_la_SOURCES = server.cpp socket_server.cpp client.cpp connection.cpp \
-       socket_handler.cpp socket_client.cpp
+       socket_handler.cpp socket_client.cpp command_server.cpp command_client.cpp \
+       t2n_exception.cpp command.cpp container.cpp
 
 include_HEADERS = server.hxx socket_server.hxx t2n_exception.hxx client.hxx \
-       socket_client.hxx connection.hxx types.hxx socket_handler.hxx
+               socket_client.hxx connection.hxx types.hxx socket_handler.hxx command.hxx container.hxx \
+       command_client.hxx command_server.hxx
diff --git a/src/command.cpp b/src/command.cpp
new file mode 100644 (file)
index 0000000..e1b5b7f
--- /dev/null
@@ -0,0 +1,23 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 "command.hxx"
+
+BOOST_CLASS_EXPORT(libt2n::result)
+BOOST_CLASS_EXPORT(libt2n::command)
diff --git a/src/command.hxx b/src/command.hxx
new file mode 100644 (file)
index 0000000..98f5004
--- /dev/null
@@ -0,0 +1,68 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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.             *
+ ***************************************************************************/
+#ifndef __LIBT2N_COMMAND
+#define __LIBT2N_COMMAND
+
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/export.hpp>
+
+namespace libt2n
+{
+
+/** @brief base class for the results that are returned from commands.
+*/
+class result
+{
+    private:
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        { }
+
+    public:
+        result() {}
+        virtual ~result() {}
+};
+}
+//BOOST_IS_ABSTRACT(libt2n::result)
+
+namespace libt2n
+{
+/** @brief a command that can be serialized. abstract.
+*/
+class command
+{
+    private:
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        { }
+
+    public:
+        /// this calls the wanted target function on the server
+        virtual result* operator()() = 0;
+        virtual ~command() {}
+};
+} // namespace libt2n
+//BOOST_IS_ABSTRACT(libt2n::command)
+
+
+
+#endif
+
diff --git a/src/command_client.cpp b/src/command_client.cpp
new file mode 100644 (file)
index 0000000..0d387b9
--- /dev/null
@@ -0,0 +1,60 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 <string>
+#include <sstream>
+
+#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/export.hpp>
+
+#include "command_client.hxx"
+
+using namespace std;
+
+namespace libt2n
+{
+
+void command_client::send_command(command* cmd, result_container &res)
+{
+    ostringstream ofs;
+    command_container cc(cmd);
+    boost::archive::binary_oarchive oa(ofs);
+
+    // TODO: exceptions
+    oa << cc;
+
+    c.write(ofs.str());
+
+    // TODO: fix timeout
+    string resultpacket;
+    while(!c.get_packet(resultpacket))
+        c.fill_buffer();
+
+    istringstream ifs(resultpacket);
+    boost::archive::binary_iarchive ia(ifs);
+
+    // TODO: exceptions
+    ia >> res;
+}
+
+}
diff --git a/src/command_client.hxx b/src/command_client.hxx
new file mode 100644 (file)
index 0000000..640faac
--- /dev/null
@@ -0,0 +1,45 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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.             *
+ ***************************************************************************/
+#ifndef __LIBT2N_COMMAND_CLIENT
+#define __LIBT2N_COMMAND_CLIENT
+
+#include "client.hxx"
+#include "container.hxx"
+
+namespace libt2n
+{
+
+/// a client sending out commands to a server
+class command_client
+{
+    private:
+        client_connection &c;
+
+    public:
+        command_client(client_connection& _c)
+            : c(_c)
+            { }
+
+        void send_command(command* cmd, result_container &res);
+};
+
+}
+
+#endif
+
diff --git a/src/command_server.cpp b/src/command_server.cpp
new file mode 100644 (file)
index 0000000..15a19a7
--- /dev/null
@@ -0,0 +1,95 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 <string>
+#include <sstream>
+#include <stdexcept>
+
+#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/export.hpp>
+
+#include "command_server.hxx"
+#include "container.hxx"
+
+using namespace std;
+
+namespace libt2n
+{
+
+/// handle a command including deserialization and answering
+void command_server::handle_packet(const std::string& packet, server_connection* conn)
+{
+    // deserialize packet
+    istringstream ifs(packet);
+    boost::archive::binary_iarchive ia(ifs);
+    command_container ccont;
+
+    // TODO: catch
+    ia >> ccont;
+
+    // TODO: cast to command subclass (template)
+    command *cmd=ccont.get_command();
+
+    result_container res;
+
+    if (cmd)
+    {
+        try
+        {
+            res.set_result((*cmd)());
+        }
+        catch (t2n_exception &e)
+            { res.set_exception(e.clone()); }
+        catch (...)
+            { throw; }
+    }
+    else
+        throw logic_error("uninitialized command called");
+
+    ostringstream ofs;
+    boost::archive::binary_oarchive oa(ofs);
+
+    // TODO: catch
+    oa << res;
+
+    conn->write(ofs.str());
+}
+
+/** @brief handle incoming commands
+    @param usec_timeout wait until new data is found, max timeout usecs.
+            -1: wait endless, 0: no timeout
+*/
+void command_server::handle(long long usec_timeout)
+{
+    if (s.fill_buffer(usec_timeout))
+    {
+        string packet;
+        unsigned int conn_id;
+
+        while (s.get_packet(packet,conn_id))
+            handle_packet(packet,s.get_connection(conn_id)); 
+    }
+    s.cleanup();
+}
+
+}
diff --git a/src/command_server.hxx b/src/command_server.hxx
new file mode 100644 (file)
index 0000000..7e475da
--- /dev/null
@@ -0,0 +1,46 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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.             *
+ ***************************************************************************/
+#ifndef __LIBT2N_COMMAND_SERVER
+#define __LIBT2N_COMMAND_SERVER
+
+#include "server.hxx"
+
+namespace libt2n
+{
+
+/// a server handling incoming commands
+class command_server
+{
+    private:
+        server& s;
+
+        void handle_packet(const std::string& packet, server_connection* conn);
+
+    public:
+        command_server(server& _s)
+            : s(_s)
+            { }
+
+        void handle(long long usec_timeout=-1);
+};
+
+}
+
+#endif
+
diff --git a/src/container.cpp b/src/container.cpp
new file mode 100644 (file)
index 0000000..bff9bb3
--- /dev/null
@@ -0,0 +1,23 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 "container.hxx"
+
+BOOST_CLASS_EXPORT(libt2n::result_container)
+BOOST_CLASS_EXPORT(libt2n::command_container)
diff --git a/src/container.hxx b/src/container.hxx
new file mode 100644 (file)
index 0000000..90053c6
--- /dev/null
@@ -0,0 +1,121 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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.             *
+ ***************************************************************************/
+#ifndef __LIBT2N_CONTAINER
+#define __LIBT2N_CONTAINER
+
+#include "command.hxx"
+#include "t2n_exception.hxx"
+
+namespace libt2n
+{
+
+/** @brief contains the result (return value or exception) of a executed command
+*/
+class result_container
+{
+    private:
+        enum result_type_t { regular, exception } result_type;
+
+        result *res;
+        t2n_exception *ex;
+
+        friend class boost::serialization::access;
+        // When the class Archive corresponds to an output archive, the
+        // & operator is defined similar to <<.  Likewise, when the class Archive
+        // is a type of input archive the & operator is defined similar to >>.
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_NVP(result_type);
+            ar & BOOST_SERIALIZATION_NVP(res);
+            ar & BOOST_SERIALIZATION_NVP(ex);
+        }
+
+    public:
+        result_container()
+            { res=0; ex=0; }
+
+        result_container(result *_res)
+            { set_result(_res); }
+        result_container(t2n_exception *_ex)
+            { set_exception(_ex); }
+
+        void set_result(result *_res)
+            { res=_res; ex=0; result_type=regular; }
+        void set_exception(t2n_exception *_ex)
+            { res=0; ex=_ex; result_type=exception; }
+
+        /** @brief returns the result or throw the carried exception.
+                   ATTENTION: the result object is deleted in the destructor
+        */
+        result* get_result(void)
+        {
+            if (result_type==exception)
+                ex->do_throw();
+            return res;
+        }
+
+        /// deletes the carried result or exception objects
+        ~result_container()
+        {
+            if (res)
+                delete res;
+            if (ex)
+                delete ex;
+        }
+};
+
+/** @brief contains a command
+*/
+class command_container
+{
+    private:
+        command *cmd;
+
+        friend class boost::serialization::access;
+        template<class Archive>
+        void serialize(Archive & ar, const unsigned int version)
+        {
+            ar & BOOST_SERIALIZATION_NVP(cmd);
+        }
+
+    public:
+        command_container()
+            { cmd=0; }
+        command_container(command *_cmd)
+            { cmd=_cmd; }
+
+        /// return the contained command
+        command* get_command()
+            { return cmd; }
+
+        ~command_container()
+        {
+            if (cmd)
+                delete cmd;
+        }
+};
+
+} // namespace libt2n
+
+BOOST_CLASS_TRACKING(libt2n::result_container, boost::serialization::track_never)
+BOOST_CLASS_TRACKING(libt2n::command_container, boost::serialization::track_never)
+
+#endif
+
diff --git a/src/socket_client.cpp b/src/socket_client.cpp
new file mode 100644 (file)
index 0000000..7406f28
--- /dev/null
@@ -0,0 +1,123 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/time.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <time.h>
+#include <pwd.h>
+#include <grp.h>
+
+#include <sstream>
+
+#include "socket_client.hxx"
+#include "t2n_exception.hxx"
+
+using namespace std;
+
+namespace libt2n
+{
+
+socket_client_connection::socket_client_connection(const std::string& _server, int _port, int _max_retries)
+    : client_connection(), socket_handler(0,tcp_s)
+{
+    max_retries=_max_retries;
+
+    server=_server;
+    port=_port;
+
+    connect();
+}
+
+socket_client_connection::socket_client_connection(const std::string& _path, int _max_retries)
+    : client_connection(), socket_handler(0,unix_s)
+{
+    max_retries=_max_retries;
+
+    path=_path;
+
+    connect();
+}
+
+void socket_client_connection::connect()
+{
+    if (get_type() == unix_s)
+    {
+        struct sockaddr_un unix_addr;
+
+        unix_addr.sun_family = AF_UNIX;
+        strcpy (unix_addr.sun_path, path.c_str());
+
+        sock = socket(PF_UNIX, SOCK_STREAM, 0);
+        if (!sock)
+            throw t2n_connect_error(string("socket() error: ")+strerror(errno));
+
+        if (::connect(sock,(struct sockaddr *) &unix_addr, sizeof(unix_addr)))
+            throw t2n_connect_error(string("connect() error: ")+strerror(errno));
+    }
+    else if (get_type() == tcp_s)
+    {
+        struct sockaddr_in sock_addr;
+
+        sock_addr.sin_family = AF_INET;
+        sock_addr.sin_port = htons(port);
+
+        // find the target ip
+        if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0)
+        {
+            struct hostent *server_hent;
+            server_hent=gethostbyname(server.c_str());
+            if (server_hent == NULL)
+                throw t2n_connect_error(string("can't find server ")+server);
+
+            memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr));
+        }
+
+        sock = socket(PF_INET, SOCK_STREAM, 0);
+        if (!sock)
+            throw t2n_connect_error(string("socket() error: ")+strerror(errno));
+
+        if (::connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr)))
+            throw t2n_connect_error(string("connect() error: ")+strerror(errno));
+    }
+    else
+        throw t2n_connect_error(string("invalid connection type"));
+
+    set_socket_options(sock);
+}
+
+void socket_client_connection::close()
+{
+    if (!client_connection::is_closed())
+    {
+        socket_handler::close();
+        client_connection::close();
+    }
+}
+
+}
diff --git a/src/t2n_exception.cpp b/src/t2n_exception.cpp
new file mode 100644 (file)
index 0000000..9c213ba
--- /dev/null
@@ -0,0 +1,30 @@
+/***************************************************************************
+ *   Copyright (C) 2006 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 "t2n_exception.hxx"
+
+BOOST_CLASS_EXPORT(libt2n::t2n_exception)
+BOOST_CLASS_EXPORT(libt2n::t2n_communication_error)
+BOOST_CLASS_EXPORT(libt2n::t2n_connect_error)
+BOOST_CLASS_EXPORT(libt2n::t2n_server_error)
+BOOST_CLASS_EXPORT(libt2n::t2n_transfer_error)
+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)
index 9d789b5..c81ba02 100644 (file)
 #include <boost/serialization/serialization.hpp>
 #include <boost/serialization/export.hpp>
 
-// serialization for std::exception
 namespace boost {
 namespace serialization {
 
+// make std::exception serializable
 template<class Archive>
 void serialize(Archive & ar, std::exception & g, const unsigned int version)
 {
@@ -39,7 +39,6 @@ void serialize(Archive & ar, std::exception & g, const unsigned int version)
 
 namespace libt2n
 {
-
 /// a generic exception that can be handeled with libt2n
 class t2n_exception : public std::exception
 {
@@ -73,7 +72,6 @@ class t2n_exception : public std::exception
         virtual void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_exception)
 
 /// a (unspecified) problem with libt2n communication
 class t2n_communication_error : public t2n_exception
@@ -100,7 +98,6 @@ class t2n_communication_error : public t2n_exception
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_communication_error)
 
 /// can't connect to libt2n server
 class t2n_connect_error : public t2n_communication_error
@@ -127,7 +124,6 @@ class t2n_connect_error : public t2n_communication_error
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_connect_error)
 
 /// error establishing a socket on the server (only thrown on the server-side)
 class t2n_server_error : public t2n_communication_error
@@ -154,7 +150,6 @@ class t2n_server_error : public t2n_communication_error
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_server_error)
 
 /// error transmitting or receiving libt2n messages
 class t2n_transfer_error : public t2n_communication_error
@@ -181,7 +176,6 @@ class t2n_transfer_error : public t2n_communication_error
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_transfer_error)
 
 /// tried to talk to an incompatible libt2n version
 class t2n_version_mismatch : public t2n_communication_error
@@ -208,7 +202,6 @@ class t2n_version_mismatch : public t2n_communication_error
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_version_mismatch)
 
 /// illegal libt2n command received
 class t2n_command_error : public t2n_exception
@@ -235,7 +228,6 @@ class t2n_command_error : public t2n_exception
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_command_error)
 
 /// error serializing or deserializing a libt2n command packet
 class t2n_serialization_error : public t2n_exception
@@ -262,7 +254,6 @@ class t2n_serialization_error : public t2n_exception
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_serialization_error)
 
 /** @brief a runtime error within the remote function.
  derive your own custom exceptions from this one
@@ -291,8 +282,7 @@ class t2n_runtime_error : public t2n_exception
         void do_throw()
             { throw *this; }
 };
-BOOST_CLASS_EXPORT(t2n_runtime_error)
 
-}
+}  // namespace libt2n
 
 #endif
index 55c5632..6044693 100644 (file)
@@ -33,6 +33,7 @@ class test_comm : public TestFixture
     CPPUNIT_TEST(UnixCommToServer);
     CPPUNIT_TEST(UnixCommToServerAndBack);
     CPPUNIT_TEST(IPCommToServer);
+    CPPUNIT_TEST(IPCommToServerAndBack);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -196,6 +197,75 @@ class test_comm : public TestFixture
         CPPUNIT_ASSERT_EQUAL(string("hello"),data);
     }
 
+    void IPCommToServerAndBack()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss(6666);
+                ss.set_logging(&cerr,debug);
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                {
+                    ss.fill_buffer(1000000);
+
+                    string data;
+                    unsigned int cid;
+
+                    if(ss.get_packet(data,cid))
+                    {
+                        server_connection* con=ss.get_connection(cid);
+
+                        if (data=="QUIT")
+                            break;
+
+                        if (data=="ABC")
+                            con->write("DEF");
+                        else
+                            con->write("xyz");
+                    }
+                }
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("localhost",6666,socket_client_connection::max_retries_default);
+                sc.write("ABC");
+
+                sc.fill_buffer(1000000);
+                sc.get_packet(data);
+
+                CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
+
+                sc.write("HAHA");
+
+                sc.fill_buffer(1000000);
+                sc.get_packet(data);
+
+                CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
+
+                sc.write("QUIT");
+            }
+        }
+    }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);