From 7087e18783f91d2b889e880462d1d1da24831c28 Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Thu, 12 Oct 2006 16:18:00 +0000 Subject: [PATCH] libt2n: (gerd) basic command handling (still some todos) --- src/Makefile.am | 6 ++- src/command.cpp | 23 +++++++++ src/command.hxx | 68 ++++++++++++++++++++++++++ src/command_client.cpp | 60 +++++++++++++++++++++++ src/command_client.hxx | 45 +++++++++++++++++ src/command_server.cpp | 95 +++++++++++++++++++++++++++++++++++++ src/command_server.hxx | 46 ++++++++++++++++++ src/container.cpp | 23 +++++++++ src/container.hxx | 121 +++++++++++++++++++++++++++++++++++++++++++++++ src/socket_client.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ src/t2n_exception.cpp | 30 ++++++++++++ src/t2n_exception.hxx | 14 +----- test/comm.cpp | 70 +++++++++++++++++++++++++++ 13 files changed, 710 insertions(+), 14 deletions(-) create mode 100644 src/command.cpp create mode 100644 src/command.hxx create mode 100644 src/command_client.cpp create mode 100644 src/command_client.hxx create mode 100644 src/command_server.cpp create mode 100644 src/command_server.hxx create mode 100644 src/container.cpp create mode 100644 src/container.hxx create mode 100644 src/socket_client.cpp create mode 100644 src/t2n_exception.cpp diff --git a/src/Makefile.am b/src/Makefile.am index d8ba3e6..ec5b0f7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000..e1b5b7f --- /dev/null +++ b/src/command.cpp @@ -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 index 0000000..98f5004 --- /dev/null +++ b/src/command.hxx @@ -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 +#include + +namespace libt2n +{ + +/** @brief base class for the results that are returned from commands. +*/ +class result +{ + private: + friend class boost::serialization::access; + template + 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 + 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 index 0000000..0d387b9 --- /dev/null +++ b/src/command_client.cpp @@ -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 +#include + +#include +#include +#include +#include +#include +#include + +#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 index 0000000..640faac --- /dev/null +++ b/src/command_client.hxx @@ -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 index 0000000..15a19a7 --- /dev/null +++ b/src/command_server.cpp @@ -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 +#include +#include + +#include +#include +#include +#include +#include +#include + +#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 index 0000000..7e475da --- /dev/null +++ b/src/command_server.hxx @@ -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 index 0000000..bff9bb3 --- /dev/null +++ b/src/container.cpp @@ -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 index 0000000..90053c6 --- /dev/null +++ b/src/container.hxx @@ -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 + 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 + 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 index 0000000..7406f28 --- /dev/null +++ b/src/socket_client.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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 index 0000000..9c213ba --- /dev/null +++ b/src/t2n_exception.cpp @@ -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) diff --git a/src/t2n_exception.hxx b/src/t2n_exception.hxx index 9d789b5..c81ba02 100644 --- a/src/t2n_exception.hxx +++ b/src/t2n_exception.hxx @@ -25,10 +25,10 @@ #include #include -// serialization for std::exception namespace boost { namespace serialization { +// make std::exception serializable template 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 diff --git a/test/comm.cpp b/test/comm.cpp index 55c5632..6044693 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -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); -- 1.7.1