From bad61b40c6d67c4eba9255dbdd89586e7621a66e Mon Sep 17 00:00:00 2001 From: sbund <sbund@wiback.org> Date: Fri, 25 Aug 2006 09:51:50 +0000 Subject: [PATCH] started TCPSocket implementation started ClientSocketHandle generic interface --- SConfig | 3 +- SConstruct | 1 + Socket/BufferingPolicy.hh | 51 ++++++++++ Socket/ClientSocketHandle.ct | 81 ++++++++++++++++ Socket/ClientSocketHandle.cti | 68 ++++++++++++-- Socket/ClientSocketHandle.hh | 44 +++++++-- Socket/ClientSocketHandle.test.cc | 99 +++++++++++--------- Socket/CommunicationPolicy.hh | 53 +++++++++++ Socket/INetAddress.cc | 93 +++++++++++++++++++ Socket/INetAddress.cci | 91 ++++++++++++++++++ Socket/INetAddress.hh | 86 +++++++++++++++++ Socket/INetAddress.test.cc | 89 ++++++++++++++++++ Socket/INetProtocol.hh | 64 +++++++++++++ Socket/ProtocolClientSocketHandle.cti | 17 +++- Socket/ProtocolClientSocketHandle.hh | 10 +- Socket/ProtocolClientSocketHandle.mpp | 93 +++++++++++++++++++ Socket/ProtocolClientSocketHandle.test.cc | 41 ++------ Socket/ProtocolServerSocketHandle.test.cc | 41 +------- Socket/ReadWritePolicy.cci | 57 ++++++++++++ Socket/ReadWritePolicy.cti | 77 +++++++++++++++ Socket/ReadWritePolicy.hh | 77 +++++++++++++++ Socket/ServerSocketHandle.test.cc | 51 ++-------- Socket/SocketHandle.cci | 4 +- Socket/SocketHandle.cti | 7 ++ Socket/SocketHandle.hh | 1 + Socket/SocketHandle.test.cc | 50 ++-------- Socket/SocketPolicy.hh | 24 +++++ Socket/SocketPolicy.ih | 36 +++++++- Socket/SocketPolicy.test.cc | 1 + Socket/SocketPolicy.test.hh | 108 ++++++++++++++++++++++ Socket/SocketProtocol.cci | 8 +- Socket/SocketProtocol.hh | 12 ++- Socket/SocketProtocol.test.cc | 18 +--- Socket/SocketProtocol.test.hh | 64 +++++++++++++ Socket/StreamFramingPolicy.hh | 49 ++++++++++ Socket/TCPProtocol.hh | 49 ++++++++++ Socket/TCPSocketHandle.cc | 107 +++++++++++++++++++++ Socket/TCPSocketHandle.hh | 102 ++++++++++++++++++++ Socket/TCPSocketHandle.test.cc | 55 +++++++++++ 39 files changed, 1733 insertions(+), 249 deletions(-) create mode 100644 Socket/BufferingPolicy.hh create mode 100644 Socket/ClientSocketHandle.ct create mode 100644 Socket/CommunicationPolicy.hh create mode 100644 Socket/INetAddress.cc create mode 100644 Socket/INetAddress.cci create mode 100644 Socket/INetAddress.hh create mode 100644 Socket/INetAddress.test.cc create mode 100644 Socket/INetProtocol.hh create mode 100644 Socket/ProtocolClientSocketHandle.mpp create mode 100644 Socket/ReadWritePolicy.cci create mode 100644 Socket/ReadWritePolicy.cti create mode 100644 Socket/ReadWritePolicy.hh create mode 100644 Socket/SocketPolicy.test.hh create mode 100644 Socket/SocketProtocol.test.hh create mode 100644 Socket/StreamFramingPolicy.hh create mode 100644 Socket/TCPProtocol.hh create mode 100644 Socket/TCPSocketHandle.cc create mode 100644 Socket/TCPSocketHandle.hh create mode 100644 Socket/TCPSocketHandle.test.cc diff --git a/SConfig b/SConfig index ec7a52d98..1a99c3145 100644 --- a/SConfig +++ b/SConfig @@ -1,8 +1,7 @@ - CXX = "g++-3.4" BOOST_INCLUDES = "/usr/local/include/boost-1_33_1" -BOOST_VARIANT = "-gcc-gdp" +BOOST_TOOLSET = "gcc" BOOST_LIBDIR = "/usr/local/lib" STLPORT_INCLUDES = "/usr/local/include/stlport" diff --git a/SConstruct b/SConstruct index d0c089438..cb04af999 100644 --- a/SConstruct +++ b/SConstruct @@ -18,3 +18,4 @@ Export('env') SConscript(glob.glob("*/SConscript")) SatSCons.StandardTargets(env) +SatSCons.GlobalTargets(env) diff --git a/Socket/BufferingPolicy.hh b/Socket/BufferingPolicy.hh new file mode 100644 index 000000000..a79b179ba --- /dev/null +++ b/Socket/BufferingPolicy.hh @@ -0,0 +1,51 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_BufferingPolicy_ +#define HH_BufferingPolicy_ 1 + +// Custom includes +#include "SocketPolicy.hh" + +//#include "BufferingPolicy.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + struct SocketBufferingPolicy : public BufferingPolicyBase + {}; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "BufferingPolicy.cci" +//#include "BufferingPolicy.ct" +//#include "BufferingPolicy.cti" +//#include "BufferingPolicy.mpp" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ClientSocketHandle.ct b/Socket/ClientSocketHandle.ct new file mode 100644 index 000000000..734148b16 --- /dev/null +++ b/Socket/ClientSocketHandle.ct @@ -0,0 +1,81 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of non-inline template functions + +//#include "ClientSocketHandle.ih" + +// Custom includes + +#define prefix_ +///////////////////////////////ct.p//////////////////////////////////////// + +template <class Policy> +prefix_ std::string satcom::lib::ClientSocketHandle<Policy>::read() +{ + std::string rv; + this->read(rv); + return rv; +} + +template <class Policy> +prefix_ void satcom::lib::ClientSocketHandle<Policy>::read(std::string & buffer) +{ + unsigned nread = this->protocol().available(); + // FIXME: This is not necessary correct and more or less a hack ... + buffer.assign(nread,0); + unsigned rv = this->read(const_cast<char *>(buffer.data()),nread); + if (rv < nread) + buffer.erase(buffer.begin()+rv,buffer.end()); +} + +template <class Policy> +prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address> +satcom::lib::ClientSocketHandle<Policy>::readfrom() +{ + std::string rv; + typename Policy::AddressingPolicy::Address addr; + this->readfrom(rv,addr); + return std::make_pair(rv,addr); +} + +template <class Policy> +prefix_ void satcom::lib::ClientSocketHandle<Policy>:: +readfrom(std::string & buffer, typename Policy::AddressingPolicy::Address & from) +{ + unsigned nread = this->protocol().available(); + // FIXME: s.o. + buffer.assign(nread,0); + unsigned rv = this->readfrom(const_cast<char *>(buffer.data()), nread, from); + if (rv < nread) + buffer.erase(buffer.begin()+rv,buffer.end()); +} + + +///////////////////////////////ct.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ClientSocketHandle.cti b/Socket/ClientSocketHandle.cti index 567d4b647..0e3d79623 100644 --- a/Socket/ClientSocketHandle.cti +++ b/Socket/ClientSocketHandle.cti @@ -29,29 +29,77 @@ #define prefix_ inline ///////////////////////////////cti.p/////////////////////////////////////// -template <class SocketPolicy> +template <class Policy> template <class OtherPolicy> -prefix_ satcom::lib::ClientSocketHandle<SocketPolicy>:: +prefix_ satcom::lib::ClientSocketHandle<Policy>:: ClientSocketHandle(ClientSocketHandle<OtherPolicy> other, - typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type *) - : SocketHandle<SocketPolicy>(other,true) + typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type *) + : SocketHandle<Policy>(other,true) {} -template <class SocketPolicy> -prefix_ satcom::lib::ClientSocketHandle<SocketPolicy>:: +template <class Policy> +prefix_ satcom::lib::ClientSocketHandle<Policy>:: ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol) - : SocketHandle<SocketPolicy>(protocol) + : SocketHandle<Policy>(protocol) {} -template <class SocketPolicy> +template <class Policy> template <class OtherPolicy> -prefix_ typename satcom::lib::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & -satcom::lib::ClientSocketHandle<SocketPolicy>::operator=(ClientSocketHandle<OtherPolicy> other) +prefix_ typename satcom::lib::SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const & +satcom::lib::ClientSocketHandle<Policy>::operator=(ClientSocketHandle<OtherPolicy> other) { assign(other); return *this; } +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// reading and writing + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>::read(char * buffer, + unsigned size) +{ + return Policy::ReadPolicy::read(*this, buffer, size); +} + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>:: +readfrom(char * buffer, unsigned size, typename Policy::AddressingPolicy::Address & from) +{ + return Policy::ReadPolicy::readfrom(*this, buffer, size, from); +} + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>::write(std::string const & data) +{ + return this->write(data.data(),data.size()); +} + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>::write(char const * buffer, + unsigned size) +{ + return Policy::WritePolicy::write(*this, buffer, size); +} + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>:: +writeto(typename boost::call_traits<typename Policy::AddressingPolicy::Address>::param_type addr, + std::string const & data) +{ + return this->writeto(addr, data.data(), data.size()); +} + +template <class Policy> +prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>:: +writeto(typename boost::call_traits<typename Policy::AddressingPolicy::Address>::param_type addr, + char const * buffer, unsigned size) +{ + return Policy::WritePolicy::writeto(*this, addr, buffer, size); +} + ///////////////////////////////cti.e/////////////////////////////////////// #undef prefix_ diff --git a/Socket/ClientSocketHandle.hh b/Socket/ClientSocketHandle.hh index 5c8bfa904..774df2905 100644 --- a/Socket/ClientSocketHandle.hh +++ b/Socket/ClientSocketHandle.hh @@ -24,6 +24,7 @@ #define HH_ClientSocketHandle_ 1 // Custom includes +#include <boost/call_traits.hpp> #include "SocketHandle.hh" //#include "ClientSocketHandle.mpp" @@ -34,9 +35,9 @@ namespace lib { /** \brief */ - template <class SocketPolicy> + template <class Policy> class ClientSocketHandle - : public SocketHandle<SocketPolicy> + : public SocketHandle<Policy> { public: /////////////////////////////////////////////////////////////////////////// @@ -54,15 +55,44 @@ namespace lib { // conversion constructors template <class OtherPolicy> ClientSocketHandle(ClientSocketHandle<OtherPolicy> other, - typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type * = 0); + typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type * = 0); + + template <class OtherPolicy> + typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const & + operator=(ClientSocketHandle<OtherPolicy> other); ///@} /////////////////////////////////////////////////////////////////////////// - template <class OtherPolicy> - typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & - operator=(ClientSocketHandle<OtherPolicy> other); + /////////////////////////////////////////////////////////////////////////// + ///\name reading and writing + ///@{ + // read from socket (connected or unconnected) + std::string read (); + void read (std::string & buffer); + unsigned read (char * buffer, unsigned size); + + // read from unconnected socket returning peer address + std::pair<std::string, typename Policy::AddressingPolicy::Address> + readfrom (); + void readfrom (std::string & buffer, + typename Policy::AddressingPolicy::Address & from); + unsigned readfrom (char * buffer, unsigned size, + typename Policy::AddressingPolicy::Address & from); + + // write to connected socket + unsigned write (std::string const & data); + unsigned write (char const * buffer, unsigned size); + + // write to unconnected socket + unsigned writeto (typename boost::call_traits<typename Policy::AddressingPolicy::Address>::param_type addr, + std::string const & data); + unsigned writeto (typename boost::call_traits<typename Policy::AddressingPolicy::Address>::param_type addr, + char const * buffer, unsigned size); + + ///@} + protected: explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol); @@ -74,7 +104,7 @@ namespace lib { ///////////////////////////////hh.e//////////////////////////////////////// //#include "ClientSocketHandle.cci" -//#include "ClientSocketHandle.ct" +#include "ClientSocketHandle.ct" #include "ClientSocketHandle.cti" #endif diff --git a/Socket/ClientSocketHandle.test.cc b/Socket/ClientSocketHandle.test.cc index 373b01829..7d01f4609 100644 --- a/Socket/ClientSocketHandle.test.cc +++ b/Socket/ClientSocketHandle.test.cc @@ -26,6 +26,8 @@ //#include "ClientSocketHandle.test.ih" // Custom includes +#include "SocketPolicy.test.hh" +#include "SocketProtocol.test.hh" #include "ClientSocketHandle.hh" #include <boost/test/auto_unit_test.hpp> @@ -35,64 +37,71 @@ ///////////////////////////////cc.p//////////////////////////////////////// namespace { - namespace sl = satcom::lib; - - struct INetAddressingPolicy : public sl::AddressingPolicyBase {}; - struct UnixAddressingPolicy : public sl::AddressingPolicyBase {}; - - struct StreamFramingPolicy : public sl::FramingPolicyBase {}; - struct DgramFramingPolicy : public sl::FramingPolicyBase {}; - struct ConnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - struct UnconnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - - struct ReadablePolicy : public sl::ReadPolicyBase {}; - struct UnreadablePolicy : public sl::ReadPolicyBase {}; - - struct WritablePolicy : public sl::WritePolicyBase {}; - struct UnwritablePolicy : public sl::WritePolicyBase {}; - - struct SocketBufferingPolicy : public sl::BufferingPolicyBase {}; - - typedef sl::MakeSocketPolicy< - INetAddressingPolicy, - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy, - SocketBufferingPolicy - >::policy MyProtocol_Policy; - - class MyProtocol - : public sl::ConcreteSocketProtocol<MyProtocol_Policy> - { - public: - ~MyProtocol() {} - }; + namespace sl = satcom::lib; class MySocketHandle - : public sl::ClientSocketHandle<MyProtocol::Policy> + : public sl::ClientSocketHandle<sl::test::SomeProtocol::Policy> { public: MySocketHandle() - : sl::ClientSocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + : sl::ClientSocketHandle<sl::test::SomeProtocol::Policy>( + std::auto_ptr<sl::SocketProtocol>(new sl::test::SomeProtocol())) {} }; } BOOST_AUTO_UNIT_TEST(clientSocketHandle) { - typedef sl::MakeSocketPolicy< - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy - >::policy StreamSocketPolicy; - typedef sl::SocketHandle<StreamSocketPolicy> StreamSocketHandle; - MySocketHandle myh; - StreamSocketHandle ssh (myh); - ssh = myh; + + // conversion to other socket handles + { + typedef sl::MakeSocketPolicy< + sl::test::SomeFramingPolicy, + sl::test::SomeReadPolicy, + sl::test::SomeWritePolicy + >::policy OtherSocketPolicy; + typedef sl::SocketHandle<OtherSocketPolicy> OtherSocketHandle; + + OtherSocketHandle ssh (myh); + ssh = myh; + } + + // reading and writing + BOOST_CHECK_EQUAL( myh.read(), "TEST-READ" ); + { + std::string buf("FOO-BAR"); + myh.read(buf); + BOOST_CHECK_EQUAL( buf, "TEST-READ" ); + } + { + char buf[11]; + ::strcpy(buf,"0123456789"); + BOOST_CHECK_EQUAL( myh.read(buf,10), 9u ); + BOOST_CHECK_EQUAL( buf, "TEST-READ9" ); + } + + BOOST_CHECK_EQUAL( myh.readfrom().first, "TEST-READ" ); + { + std::string buf("FOO-BAR"); + satcom::lib::nil addr; + myh.readfrom(buf,addr); + BOOST_CHECK_EQUAL( buf, "TEST-READ" ); + } + { + char buf[11]; + satcom::lib::nil addr; + ::strcpy(buf,"0123456789"); + BOOST_CHECK_EQUAL( myh.readfrom(buf,10,addr), 9u ); + BOOST_CHECK_EQUAL( buf, "TEST-READ9" ); + } + + BOOST_CHECK_EQUAL( myh.write("TEST-WRITE"), 10u ); + BOOST_CHECK_EQUAL( myh.write("TEST"), 0u ); + BOOST_CHECK_EQUAL( myh.write("TEST-WRITE9",10), 10u ); + BOOST_CHECK_EQUAL( myh.writeto(satcom::lib::nil(),"TEST-WRITE"), 10u ); + BOOST_CHECK_EQUAL( myh.writeto(satcom::lib::nil(),"TEST-WRITE9",10), 10u ); } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/Socket/CommunicationPolicy.hh b/Socket/CommunicationPolicy.hh new file mode 100644 index 000000000..e32a97d0a --- /dev/null +++ b/Socket/CommunicationPolicy.hh @@ -0,0 +1,53 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_CommunicationPolicy_ +#define HH_CommunicationPolicy_ 1 + +// Custom includes + +//#include "CommunicationPolicy.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + struct ConnectedCommunicationPolicy : public CommunicationPolicyBase + {}; + + struct UnconnectedCommunicationPolicy : public CommunicationPolicyBase + {}; + +}} + + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "CommunicationPolicy.cci" +//#include "CommunicationPolicy.ct" +//#include "CommunicationPolicy.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/INetAddress.cc b/Socket/INetAddress.cc new file mode 100644 index 000000000..c95e28b04 --- /dev/null +++ b/Socket/INetAddress.cc @@ -0,0 +1,93 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of non-inline non-template functions + +#include "INetAddress.hh" +//#include "INetAddress.ih" + +// Custom includes +#include <strstream> +#include <string.h> +#include <sys/socket.h> +#include <boost/lexical_cast.hpp> + +//#include "INetAddress.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +prefix_ satcom::lib::INet4Address::INet4Address() +{ + ::memset(&addr,0,sizeof(addr)); + addr.sin_family = AF_INET; +} + +prefix_ satcom::lib::INet4Address const & +satcom::lib::INet4Address::operator=(std::string const & address) +{ + ::memset(&addr,0,sizeof(addr)); + addr.sin_family = AF_INET; + // TODO: gethostbyname einbauen + unsigned i = address.find(':'); + if (i == std::string::npos) + throw InvalidINetAddressException(); + if (::inet_aton(std::string(address,0,i).c_str(), &addr.sin_addr) == 0) + throw InvalidINetAddressException(); + try { + // Replace lexical_cast with strtoul ? + addr.sin_port = htons(boost::lexical_cast< ::u_int16_t >(std::string(address,i+1))); + } + catch (boost::bad_lexical_cast const & ex) { + throw InvalidINetAddressException(); + } + return *this; +} + +prefix_ satcom::lib::INet4Address const & +satcom::lib::INet4Address::operator=(std::pair<std::string, unsigned> const & address) +{ + ::memset(&addr,0,sizeof(addr)); + addr.sin_family = AF_INET; + // TODO: gethostbyname einbauen + if (::inet_aton(address.first.c_str(), &addr.sin_addr) == 0) + throw InvalidINetAddressException(); + addr.sin_port = htons(address.second); + return *this; +} + +prefix_ std::string satcom::lib::INet4Address::str() + const +{ + std::stringstream s; + s << host() << ':' << port(); + return s.str(); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "INetAddress.mpp" + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/INetAddress.cci b/Socket/INetAddress.cci new file mode 100644 index 000000000..dde22885f --- /dev/null +++ b/Socket/INetAddress.cci @@ -0,0 +1,91 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of inline non-template functions + +// Custom includes +#include <arpa/inet.h> +#include <netinet/in.h> + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ satcom::lib::INet4Address::INet4Address(std::string address) +{ + *this = address; +} + +prefix_ satcom::lib::INet4Address::INet4Address(std::string host, unsigned port) +{ + *this = std::make_pair(host,port); +} + +prefix_ bool satcom::lib::INet4Address::operator==(INet4Address const & other) +{ + return addr.sin_port == other.addr.sin_port && + addr.sin_addr.s_addr == other.addr.sin_addr.s_addr; +} + +prefix_ std::string satcom::lib::INet4Address::host() + const +{ + // FIXME: thread safety? + return std::string(::inet_ntoa(addr.sin_addr)); +} + +prefix_ unsigned satcom::lib::INet4Address::port() + const +{ + return ntohs(addr.sin_port); +} + +prefix_ struct sockaddr * satcom::lib::INet4Address::sockaddr_p() +{ + return reinterpret_cast<struct sockaddr *>(&addr); +} + +prefix_ struct sockaddr const * satcom::lib::INet4Address::sockaddr_p() + const +{ + return reinterpret_cast<struct sockaddr const *>(&addr); +} + +prefix_ unsigned satcom::lib::INet4Address::sockaddr_len() + const +{ + return sizeof(addr); +} + +prefix_ std::ostream & satcom::lib::operator<<(std::ostream & os, INet4Address const & addr) +{ + os << addr.str(); + return os; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/INetAddress.hh b/Socket/INetAddress.hh new file mode 100644 index 000000000..2e6a6c266 --- /dev/null +++ b/Socket/INetAddress.hh @@ -0,0 +1,86 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_INetAddress_ +#define HH_INetAddress_ 1 + +// Custom includes +#include <string> +#include <exception> +#include <netinet/in.h> + +//#include "INetAddress.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + class INet4Address + { + public: + INet4Address(); + INet4Address(std::string address); + INet4Address(std::string host, unsigned port); + + INet4Address const & operator=(std::string const & address); + INet4Address const & operator=(std::pair<std::string, unsigned> const & address); + + bool operator==(INet4Address const & other); + + std::string str() const; + std::string host() const; + unsigned port() const; + + // TODO: Interface + + struct sockaddr * sockaddr_p(); + struct sockaddr const * sockaddr_p() const; + unsigned sockaddr_len() const; + + private: + struct ::sockaddr_in addr; + }; + + std::ostream & operator<<(std::ostream & os, INet4Address const & addr); + + class INet6Address + { + // TODO: Implement + }; + + struct InvalidINetAddressException : public std::exception + { char const * what() const throw() { return "invalid inet address"; } }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "INetAddress.cci" +//#include "INetAddress.ct" +//#include "INetAddress.cti" +//#include "INetAddress.mpp" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/INetAddress.test.cc b/Socket/INetAddress.test.cc new file mode 100644 index 000000000..e9e8b8421 --- /dev/null +++ b/Socket/INetAddress.test.cc @@ -0,0 +1,89 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Unit tests + +//#include "INetAddress.test.hh" +//#include "INetAddress.test.ih" + +// Custom includes +#include "INetAddress.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(inet4Address) +{ + using satcom::lib::INet4Address; + using satcom::lib::InvalidINetAddressException; + + { + INet4Address addr; + + addr = "127.0.0.1:12345"; + addr = std::make_pair("127.0.0.1",12345); + } + + { + INet4Address addr1("127.0.0.1:12345"); + INet4Address addr2("127.0.0.1",12345); + } + + BOOST_CHECK( INet4Address("127.0.0.1:12345") == INet4Address("127.0.0.1",12345) ); + + BOOST_CHECK_THROW( INet4Address("127.0.0.1"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4Address("foo@bar:12345"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4Address("127.0.0.1:1234a"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4Address("foo@bar",12345), InvalidINetAddressException ); + + BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").host(), "127.0.0.1" ); + BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").port(), 12345u ); + BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").str(), "127.0.0.1:12345" ); + + { + INet4Address addr("127.0.0.1:12345"); + BOOST_CHECK_EQUAL( reinterpret_cast< ::sockaddr_in * >(addr.sockaddr_p())->sin_port, + htons(12345) ); + BOOST_CHECK_EQUAL( reinterpret_cast< ::sockaddr_in * >(addr.sockaddr_p())->sin_addr.s_addr, + htonl(INADDR_LOOPBACK) ); + + std::stringstream s; + s << addr; + BOOST_CHECK_EQUAL( s.str(), "127.0.0.1:12345" ); + } +} + +BOOST_AUTO_UNIT_TEST(inet6Address) +{ +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/INetProtocol.hh b/Socket/INetProtocol.hh new file mode 100644 index 000000000..b19556d8c --- /dev/null +++ b/Socket/INetProtocol.hh @@ -0,0 +1,64 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_INetProtocol_ +#define HH_INetProtocol_ 1 + +// Custom includes +#include "SocketPolicy.hh" +#include "INetAddress.hh" + +//#include "INetProtocol.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + struct INet4AddressingPolicy : public AddressingPolicyBase + { + typedef INet4Address Address; + }; + + struct INet6AddressingPolicy : public AddressingPolicyBase + { + typedef INet6Address Address; + }; + + class IPv4Protocol + {}; + + class IPv6Protocol + {}; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "INetProtocol.cci" +//#include "INetProtocol.ct" +//#include "INetProtocol.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ProtocolClientSocketHandle.cti b/Socket/ProtocolClientSocketHandle.cti index bb7b6b0b0..11b4f14b3 100644 --- a/Socket/ProtocolClientSocketHandle.cti +++ b/Socket/ProtocolClientSocketHandle.cti @@ -33,7 +33,22 @@ template <class SocketProtocol> prefix_ satcom::lib::ProtocolClientSocketHandle<SocketProtocol>::ProtocolClientSocketHandle() : ClientSocketHandle<typename SocketProtocol::Policy>( std::auto_ptr<satcom::lib::SocketProtocol>(new SocketProtocol())) -{} +{ + this->protocol().init_client(); +} + +template <class SocketProtocol> +template <class A1> +prefix_ satcom::lib::ProtocolClientSocketHandle<SocketProtocol>:: +ProtocolClientSocketHandle(A1 const & a1) + : ClientSocketHandle<typename SocketProtocol::Policy>( + std::auto_ptr<satcom::lib::SocketProtocol>(new SocketProtocol())) +{ + this->protocol().init_client(a1); +} + +#define BOOST_PP_ITERATION_PARAMS_1 (4, (2, 9, "Socket/ProtocolClientSocketHandle.mpp", 2)) +#include BOOST_PP_ITERATE() template <class SocketProtocol> prefix_ SocketProtocol const & diff --git a/Socket/ProtocolClientSocketHandle.hh b/Socket/ProtocolClientSocketHandle.hh index 42ef2d187..04579b49f 100644 --- a/Socket/ProtocolClientSocketHandle.hh +++ b/Socket/ProtocolClientSocketHandle.hh @@ -26,7 +26,7 @@ // Custom includes #include "ClientSocketHandle.hh" -//#include "ProtocolClientSocketHandle.mpp" +#include "ProtocolClientSocketHandle.mpp" ///////////////////////////////hh.p//////////////////////////////////////// namespace satcom { @@ -48,10 +48,13 @@ namespace lib { ///\name Structors and default members ///@{ - // TODO: Non-default constructors (via argument forwarding - // to the SocketProtocol::init(...) methods ProtocolClientSocketHandle(); + template <class A1> + ProtocolClientSocketHandle(A1 const & a1); +# define BOOST_PP_ITERATION_PARAMS_1 (4, (2, 9, "Socket/ProtocolClientSocketHandle.mpp", 1)) +# include BOOST_PP_ITERATE() + ///@} /////////////////////////////////////////////////////////////////////////// @@ -69,6 +72,7 @@ namespace lib { //#include "ProtocolClientSocketHandle.cci" //#include "ProtocolClientSocketHandle.ct" #include "ProtocolClientSocketHandle.cti" +#include "ProtocolClientSocketHandle.mpp" #endif diff --git a/Socket/ProtocolClientSocketHandle.mpp b/Socket/ProtocolClientSocketHandle.mpp new file mode 100644 index 000000000..14a0149c9 --- /dev/null +++ b/Socket/ProtocolClientSocketHandle.mpp @@ -0,0 +1,93 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +#if !BOOST_PP_IS_ITERATING +#ifndef MPP_ProtocolClientSocketHandle + +// Custom includes +#include <boost/preprocessor/iteration/iterate.hpp> +#include <boost/preprocessor/enum.hpp> +#include <boost/preprocessor/cat.hpp> + +//////////////////////////////mpp.p//////////////////////////////////////// +// Local Macros + +#define mppArg(z,n,data) BOOST_PP_CAT(A,n) const & BOOST_PP_CAT(a,n) + +#define mppTemplateParameters() BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A ) +#define mppMethodParameters() BOOST_PP_ENUM(BOOST_PP_ITERATION(), mppArg, ) +#define mppCallParameters() BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), a ) + +////// +#endif +#else +/////////////////////////////////////////////////////////////////////////// + +////// +#if BOOST_PP_ITERATION_FLAGS()==1 +/////////////////////////////////////////////////////////////////////////// +// satcom::lib::ProtocolClientSocketHandle<SocketProtocol>:: +// ProtocolClientSocketHandle (constructor) declaration + +template < mppTemplateParameters() > +ProtocolClientSocketHandle( mppMethodParameters() ); + +////// +#elif BOOST_PP_ITERATION_FLAGS()==2 +/////////////////////////////////////////////////////////////////////////// +// satcom::lib::ProtocolClientSocketHandle<SocketProtocol>:: +// ProtocolClientSocketHandle (constructor) implementation + +template <class SocketProtocol> +template < mppTemplateParameters() > +prefix_ satcom::lib::ProtocolClientSocketHandle<SocketProtocol>:: +ProtocolClientSocketHandle( mppMethodParameters() ) + : ClientSocketHandle<typename SocketProtocol::Policy>( + std::auto_ptr<satcom::lib::SocketProtocol>(new SocketProtocol())) +{ + this->protocol().init_client( mppCallParameters() ); +} + +////// +#endif +#endif +#if !BOOST_PP_IS_ITERATING +#ifdef MPP_ProtocolClientSocketHandle_ +/////////////////////////////////////////////////////////////////////////// +// Undefine local Macros + +#undef mppArg +#undef mppTemplateParameters +#undef mppMethodParameters +#undef mppCallParameters + +//////////////////////////////mpp.e//////////////////////////////////////// +#else +#define MPP_ProtocolClientSocketHandle_ 1 +#endif +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ProtocolClientSocketHandle.test.cc b/Socket/ProtocolClientSocketHandle.test.cc index 9b078e8da..33273ba8d 100644 --- a/Socket/ProtocolClientSocketHandle.test.cc +++ b/Socket/ProtocolClientSocketHandle.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "ProtocolClientSocketHandle.hh" +#include "SocketProtocol.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> @@ -35,49 +36,21 @@ ///////////////////////////////cc.p//////////////////////////////////////// namespace { - namespace sl = satcom::lib; - - struct INetAddressingPolicy : public sl::AddressingPolicyBase {}; - struct UnixAddressingPolicy : public sl::AddressingPolicyBase {}; - - struct StreamFramingPolicy : public sl::FramingPolicyBase {}; - struct DgramFramingPolicy : public sl::FramingPolicyBase {}; - - struct ConnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - struct UnconnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - - struct ReadablePolicy : public sl::ReadPolicyBase {}; - struct UnreadablePolicy : public sl::ReadPolicyBase {}; - - struct WritablePolicy : public sl::WritePolicyBase {}; - struct UnwritablePolicy : public sl::WritePolicyBase {}; - - struct SocketBufferingPolicy : public sl::BufferingPolicyBase {}; - - typedef sl::MakeSocketPolicy< - INetAddressingPolicy, - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy, - SocketBufferingPolicy - >::policy MyProtocol_Policy; - - class MyProtocol - : public sl::ConcreteSocketProtocol<MyProtocol_Policy> + struct MyProtocol : public satcom::lib::test::SomeProtocol { - public: - ~MyProtocol() {} + using satcom::lib::test::SomeProtocol::init_client; + void init_client(char const *,unsigned) const {} }; - } BOOST_AUTO_UNIT_TEST(protocolClientSocketHandle) { - typedef sl::ProtocolClientSocketHandle<MyProtocol> MySocketHandle; + typedef satcom::lib::ProtocolClientSocketHandle<MyProtocol> MySocketHandle; MySocketHandle h; h.protocol(); + + MySocketHandle hh("foo.bar.c",1234); } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/Socket/ProtocolServerSocketHandle.test.cc b/Socket/ProtocolServerSocketHandle.test.cc index c44791200..ae3ea2c40 100644 --- a/Socket/ProtocolServerSocketHandle.test.cc +++ b/Socket/ProtocolServerSocketHandle.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "ProtocolServerSocketHandle.hh" +#include "SocketProtocol.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> @@ -34,47 +35,9 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -namespace { - namespace sl = satcom::lib; - - struct INetAddressingPolicy : public sl::AddressingPolicyBase {}; - struct UnixAddressingPolicy : public sl::AddressingPolicyBase {}; - - struct StreamFramingPolicy : public sl::FramingPolicyBase {}; - struct DgramFramingPolicy : public sl::FramingPolicyBase {}; - - struct ConnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - struct UnconnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - - struct ReadablePolicy : public sl::ReadPolicyBase {}; - struct UnreadablePolicy : public sl::ReadPolicyBase {}; - - struct WritablePolicy : public sl::WritePolicyBase {}; - struct UnwritablePolicy : public sl::WritePolicyBase {}; - - struct SocketBufferingPolicy : public sl::BufferingPolicyBase {}; - - typedef sl::MakeSocketPolicy< - INetAddressingPolicy, - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy, - SocketBufferingPolicy - >::policy MyProtocol_Policy; - - class MyProtocol - : public sl::ConcreteSocketProtocol<MyProtocol_Policy> - { - public: - ~MyProtocol() {} - }; - -} - BOOST_AUTO_UNIT_TEST(protocolServerSocketHandle) { - typedef sl::ProtocolServerSocketHandle<MyProtocol> MySocketHandle; + typedef satcom::lib::ProtocolServerSocketHandle<satcom::lib::test::SomeProtocol> MySocketHandle; MySocketHandle h; h.protocol(); diff --git a/Socket/ReadWritePolicy.cci b/Socket/ReadWritePolicy.cci new file mode 100644 index 000000000..bbcf510e5 --- /dev/null +++ b/Socket/ReadWritePolicy.cci @@ -0,0 +1,57 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of inline non-template functions + +// Custom includes +#include <unistd.h> +#include <errno.h> + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ unsigned satcom::lib::ReadablePolicy::read(FileHandle handle, char * buffer, + unsigned size) +{ + int rv = ::read(handle.fd(), buffer, size); + if (rv < 0) + throw SystemException(errno); + return rv; +} + +prefix_ unsigned satcom::lib::WriteablePolicy::write(int fd, char * buffer, unsigned size, + ConnectedCommunicationPolicy*) +{ + int rv = ::write(fd, buffer, size); + if (rv < 0) + throw SystemException(errno); + return rv; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ReadWritePolicy.cti b/Socket/ReadWritePolicy.cti new file mode 100644 index 000000000..893354c1c --- /dev/null +++ b/Socket/ReadWritePolicy.cti @@ -0,0 +1,77 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of inline template functions + +//#include "ReadWritePolicy.ih" + +// Custom includes +#include <unistd.h> +#include <errno.h> + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class Policy> +prefix_ unsigned satcom::lib::ReadablePolicy:: +readfrom(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + typename Policy::AddressingPolicy::Address & address, + IfCommunicationPolicyIs<Policy,UnconnectedCommunicationPolicy>::type *) +{ + int rv = ::recvfrom(handle.fd(),buffer, size, 0, + address.sockaddr_p(), address.sockaddr_len()); + if (rv < 0) + throw SystemException(errno); + return rv; +} + +template <class Policy> +prefix_ unsigned satcom::lib::WriteablePolicy:: +write(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + IfCommunicationPolicyIs<Policy,ConnectedCommunicationPolicy>::type *) +{ + int rv = ::write(handle.fd(), buffer, size); + if (rv < 0) + throw SystemException(errno); + return rv; +} + +template <class Policy> +prefix_ unsigned satcom::lib::WriteablePolicy:: +writeto(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + IfCommunicationPolicyIs<Policy,UnconnectedCommunicationPolicy>::type *) +{ + int rv = ::sendto(handle.fd(), buffer, size, 0, + address.sockaddr_p(), address.sockaddr_len()); + if (rv < 0) + throw SystemException(errno); + return rv; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ReadWritePolicy.hh b/Socket/ReadWritePolicy.hh new file mode 100644 index 000000000..7f13bb6ef --- /dev/null +++ b/Socket/ReadWritePolicy.hh @@ -0,0 +1,77 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_ReadWritePolicy_ +#define HH_ReadWritePolicy_ 1 + +// Custom includes +#include "SocketPolicy.hh" +#include "ClientSocketHandle.hh" +#include "CommunicationPolicy.hh" + +//#include "ReadWritePolicy.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +// TODO: ReadWritePolicy.test.cc ... + +namespace satcom { +namespace lib { + + struct ReadablePolicy : public ReadPolicyBase + { + static unsigned read(FileHandle handle, char * buffer, unsigned size); + template <class Policy> + static unsigned readfrom(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + typename Policy::AddressingPolicy::Address & address, + typename IfCommunicationPolicyIs<Policy,UnconnectedCommunicationPolicy>::type * = 0); + }; + + struct NotReadablePolicy : public ReadPolicyBase + {}; + + struct WriteablePolicy : public WritePolicyBase + { + template <class Policy> + static unsigned write(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + typename IfCommunicationPolicyIs<Policy,ConnectedCommunicationPolicy>::type * = 0); + template <class Policy> + static unsigned writeto(ClientSocketHandle<Policy> handle, char * buffer, unsigned size, + typename IfCommunicationPolicyIs<Policy,UnconnectedCommunicationPolicy>::type * = 0); + }; + + struct NotWriteablePolicy : public WritePolicyBase + {}; + +}} + + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "ReadWritePolicy.cci" +//#include "ReadWritePolicy.ct" +//#include "ReadWritePolicy.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ServerSocketHandle.test.cc b/Socket/ServerSocketHandle.test.cc index f7e8e7d7d..336ed3b3f 100644 --- a/Socket/ServerSocketHandle.test.cc +++ b/Socket/ServerSocketHandle.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "ServerSocketHandle.hh" +#include "SocketProtocol.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> @@ -36,46 +37,13 @@ namespace { namespace sl = satcom::lib; - - struct INetAddressingPolicy : public sl::AddressingPolicyBase {}; - struct UnixAddressingPolicy : public sl::AddressingPolicyBase {}; - - struct StreamFramingPolicy : public sl::FramingPolicyBase {}; - struct DgramFramingPolicy : public sl::FramingPolicyBase {}; - - struct ConnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - struct UnconnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - - struct ReadablePolicy : public sl::ReadPolicyBase {}; - struct UnreadablePolicy : public sl::ReadPolicyBase {}; - - struct WritablePolicy : public sl::WritePolicyBase {}; - struct UnwritablePolicy : public sl::WritePolicyBase {}; - - struct SocketBufferingPolicy : public sl::BufferingPolicyBase {}; - - typedef sl::MakeSocketPolicy< - INetAddressingPolicy, - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy, - SocketBufferingPolicy - >::policy MyProtocol_Policy; - - class MyProtocol - : public sl::ConcreteSocketProtocol<MyProtocol_Policy> - { - public: - ~MyProtocol() {} - }; class MySocketHandle - : public sl::ServerSocketHandle<MyProtocol::Policy> + : public sl::ServerSocketHandle<sl::test::SomeProtocol::Policy> { public: MySocketHandle() - : sl::ServerSocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + : sl::ServerSocketHandle<sl::test::SomeProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new sl::test::SomeProtocol())) {} }; } @@ -83,15 +51,14 @@ namespace { BOOST_AUTO_UNIT_TEST(serverSocketHandle) { typedef sl::MakeSocketPolicy< - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy - >::policy StreamSocketPolicy; - typedef sl::SocketHandle<StreamSocketPolicy> StreamSocketHandle; + sl::test::SomeFramingPolicy, + sl::test::SomeReadPolicy, + sl::test::SomeWritePolicy + >::policy OtherSocketPolicy; + typedef sl::SocketHandle<OtherSocketPolicy> OtherSocketHandle; MySocketHandle myh; - StreamSocketHandle ssh (myh); + OtherSocketHandle ssh (myh); ssh = myh; } diff --git a/Socket/SocketHandle.cci b/Socket/SocketHandle.cci index d967d1b2d..686241c63 100644 --- a/Socket/SocketHandle.cci +++ b/Socket/SocketHandle.cci @@ -31,7 +31,9 @@ prefix_ satcom::lib::SocketBody::SocketBody(std::auto_ptr<SocketProtocol> protocol) : protocol_(protocol) -{} +{ + protocol_->body_ = this; +} prefix_ satcom::lib::SocketProtocol const & satcom::lib::SocketBody::protocol() const diff --git a/Socket/SocketHandle.cti b/Socket/SocketHandle.cti index d5c04fe04..1f9d8536f 100644 --- a/Socket/SocketHandle.cti +++ b/Socket/SocketHandle.cti @@ -71,6 +71,13 @@ prefix_ satcom::lib::SocketBody const & satcom::lib::SocketHandle<SocketPolicy>: return static_cast<SocketBody const &>(FileHandle::body()); } +template <class SocketPolicy> +prefix_ satcom::lib::SocketProtocol const & satcom::lib::SocketHandle<SocketPolicy>::protocol() + const +{ + return body().protocol(); +} + template <class SocketPolicy> prefix_ void satcom::lib::SocketHandle<SocketPolicy>::assign(FileHandle other) { diff --git a/Socket/SocketHandle.hh b/Socket/SocketHandle.hh index 7a4b7ec80..8dcb570f4 100644 --- a/Socket/SocketHandle.hh +++ b/Socket/SocketHandle.hh @@ -78,6 +78,7 @@ namespace lib { SocketBody & body(); SocketBody const & body() const; + SocketProtocol const & protocol() const; void assign(FileHandle other); diff --git a/Socket/SocketHandle.test.cc b/Socket/SocketHandle.test.cc index b66acfd4e..0f25c3246 100644 --- a/Socket/SocketHandle.test.cc +++ b/Socket/SocketHandle.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "SocketHandle.hh" +#include "SocketProtocol.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> @@ -36,46 +37,13 @@ namespace { namespace sl = satcom::lib; - - struct INetAddressingPolicy : public sl::AddressingPolicyBase {}; - struct UnixAddressingPolicy : public sl::AddressingPolicyBase {}; - - struct StreamFramingPolicy : public sl::FramingPolicyBase {}; - struct DgramFramingPolicy : public sl::FramingPolicyBase {}; - - struct ConnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - struct UnconnectedCommunicationPolicy : public sl::CommunicationPolicyBase {}; - - struct ReadablePolicy : public sl::ReadPolicyBase {}; - struct UnreadablePolicy : public sl::ReadPolicyBase {}; - - struct WritablePolicy : public sl::WritePolicyBase {}; - struct UnwritablePolicy : public sl::WritePolicyBase {}; - - struct SocketBufferingPolicy : public sl::BufferingPolicyBase {}; - - typedef sl::MakeSocketPolicy< - INetAddressingPolicy, - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy, - SocketBufferingPolicy - >::policy MyProtocol_Policy; - - class MyProtocol - : public sl::ConcreteSocketProtocol<MyProtocol_Policy> - { - public: - ~MyProtocol() {} - }; class MySocketHandle - : public sl::SocketHandle<MyProtocol::Policy> + : public sl::SocketHandle<sl::test::SomeProtocol::Policy> { public: MySocketHandle() - : sl::SocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + : sl::SocketHandle<sl::test::SomeProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new sl::test::SomeProtocol())) {} }; } @@ -83,15 +51,13 @@ namespace { BOOST_AUTO_UNIT_TEST(socketHandle) { typedef sl::MakeSocketPolicy< - StreamFramingPolicy, - ConnectedCommunicationPolicy, - ReadablePolicy, - WritablePolicy - >::policy StreamSocketPolicy; - typedef sl::SocketHandle<StreamSocketPolicy> StreamSocketHandle; + sl::test::SomeCommunicationPolicy, + sl::test::SomeReadPolicy + >::policy OtherSocketPolicy; + typedef sl::SocketHandle<OtherSocketPolicy> OtherSocketHandle; MySocketHandle myh; - StreamSocketHandle ssh (myh); + OtherSocketHandle ssh (myh); ssh = myh; } diff --git a/Socket/SocketPolicy.hh b/Socket/SocketPolicy.hh index 7ddcb9dc3..41aa09415 100644 --- a/Socket/SocketPolicy.hh +++ b/Socket/SocketPolicy.hh @@ -100,6 +100,30 @@ namespace lib { template <class BasePolicy, class DerivedPolicy> struct SocketPolicyIsBaseOf; // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfAddressingPolicyIs; + // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfFramingPolicyIs; + // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfCommunicationPolicyIs; + // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfReadPolicyIs; + // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfWritePolicyIs; + // the rest is implementation detail ... + + template <class Policy, class Trait> + struct IfBufferingPolicyIs; + // the rest is implementation detail ... }} //////////////////////////////hh.e//////////////////////////////////////// diff --git a/Socket/SocketPolicy.ih b/Socket/SocketPolicy.ih index 92f187275..6c666c08d 100644 --- a/Socket/SocketPolicy.ih +++ b/Socket/SocketPolicy.ih @@ -185,7 +185,7 @@ namespace lib { template <class T1, class T2, class T3, class T4, class T5, class T6> class MakeSocketPolicy - : public boost::mpl::if_< boost::is_base_of< SocketPolicyBase, T1>, + : public boost::mpl::if_< boost::is_convertible< T1*, SocketPolicyBase* >, impl::MakeSocketPolicy_impl< T1, boost::mpl::vector<T2,T3,T4,T5,T6> >, impl::MakeSocketPolicy_impl< SocketPolicy<>, boost::mpl::vector<T1,T2,T3,T4,T5,T6> > >::type @@ -193,11 +193,41 @@ namespace lib { template <class BasePolicy, class DerivedPolicy> struct SocketPolicyIsBaseOf - : public boost::mpl::if_< boost::mpl::and_< boost::is_base_of< SocketPolicyBase, BasePolicy>, - boost::is_base_of< SocketPolicyBase, DerivedPolicy> >, + : public boost::mpl::if_< boost::mpl::and_< boost::is_convertible< BasePolicy*, SocketPolicyBase* >, + boost::is_convertible< DerivedPolicy*, SocketPolicyBase* > >, impl::SocketPolicy_compatibility<BasePolicy,DerivedPolicy>, boost::false_type >::type {}; + + template <class Policy, class Trait> + struct IfAddressingPolicyIs + : public boost::is_convertible< typename Policy::AddressingPolicy*, Trait* > + {}; + + template <class Policy, class Trait> + struct IfFramingPolicyIs + : public boost::is_convertible< typename Policy::FramingPolicy*, Trait* > + {}; + + template <class Policy, class Trait> + struct IfCommunicationPolicyIs + : public boost::is_convertible< typename Policy::CommunicationPolicy*, Trait* > + {}; + + template <class Policy, class Trait> + struct IfReadPolicyIs + : public boost::is_convertible< typename Policy::ReadPolicy*, Trait* > + {}; + + template <class Policy, class Trait> + struct IfWritePolicyIs + : public boost::is_convertible< typename Policy::WritePolicy*, Trait* > + {}; + + template <class Policy, class Trait> + struct IfBufferingPolicyIs + : public boost::is_convertible< typename Policy::BufferingPolicy*, Trait* > + {}; }} ///////////////////////////////ih.e//////////////////////////////////////// diff --git a/Socket/SocketPolicy.test.cc b/Socket/SocketPolicy.test.cc index 9b1ab29e0..af167a467 100644 --- a/Socket/SocketPolicy.test.cc +++ b/Socket/SocketPolicy.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "SocketPolicy.hh" +#include "SocketPolicy.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> diff --git a/Socket/SocketPolicy.test.hh b/Socket/SocketPolicy.test.hh new file mode 100644 index 000000000..ec7b05907 --- /dev/null +++ b/Socket/SocketPolicy.test.hh @@ -0,0 +1,108 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_SocketPolicy_test_ +#define HH_SocketPolicy_test_ 1 + +// Custom includes +#include "SocketPolicy.hh" +#include "FileHandle.hh" + +//#include "SocketPolicy.test.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { +namespace test { + + struct SomeAddressingPolicy : public satcom::lib::AddressingPolicyBase + { + typedef satcom::lib::nil Address; + }; + + struct SomeFramingPolicy : public satcom::lib::FramingPolicyBase + {}; + + struct SomeCommunicationPolicy : public satcom::lib::CommunicationPolicyBase + {}; + + struct SomeReadPolicy : public satcom::lib::ReadPolicyBase + { + static unsigned const TEST_SIZE = 9; + + static unsigned read(FileHandle handle, char * buffer, unsigned size) + { + int const n (size<TEST_SIZE?size:TEST_SIZE); + ::memcpy(buffer,"TEST-READ",n); + return n; + } + + static unsigned readfrom(FileHandle handle, char * buffer, unsigned size, + satcom::lib::nil & address) + { + return read(handle,buffer,size); + } + }; + + struct SomeWritePolicy : public satcom::lib::WritePolicyBase + { + static unsigned write(FileHandle handle, char const * buffer, unsigned size) + { + if (size == 10 && ::strncmp(buffer,"TEST-WRITE",10) == 0) + return size; + else + return 0; + } + + static unsigned writeto(FileHandle handle, satcom::lib::nil const & address, + char const * buffer, unsigned size) + { + return write(handle,buffer,size); + } + }; + + struct SomeBufferingPolicy : public satcom::lib::BufferingPolicyBase + {}; + + typedef satcom::lib::MakeSocketPolicy< + SomeAddressingPolicy, + SomeFramingPolicy, + SomeCommunicationPolicy, + SomeReadPolicy, + SomeWritePolicy, + SomeBufferingPolicy + >::policy SomeSocketPolicy; + +}}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "SocketPolicy.test.cci" +//#include "SocketPolicy.test.ct" +//#include "SocketPolicy.test.cti" +//#include "SocketPolicy.test.mpp" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketProtocol.cci b/Socket/SocketProtocol.cci index 4c28f2884..82611e179 100644 --- a/Socket/SocketProtocol.cci +++ b/Socket/SocketProtocol.cci @@ -25,21 +25,23 @@ //#include "SocketProtocol.ih" // Custom includes +#include <boost/assert.hpp> #define prefix_ inline ///////////////////////////////cci.p/////////////////////////////////////// prefix_ satcom::lib::SocketProtocol::SocketProtocol() - : body_(0) + : body_() {} prefix_ satcom::lib::SocketProtocol::~SocketProtocol() {} -prefix_ satcom::lib::SocketBody * satcom::lib::SocketProtocol::body() +prefix_ satcom::lib::SocketBody & satcom::lib::SocketProtocol::body() const { - return body_; + BOOST_ASSERT( body_ ); + return *body_; } ///////////////////////////////cci.e/////////////////////////////////////// diff --git a/Socket/SocketProtocol.hh b/Socket/SocketProtocol.hh index c9161f5df..2d25ef76d 100644 --- a/Socket/SocketProtocol.hh +++ b/Socket/SocketProtocol.hh @@ -57,16 +57,20 @@ namespace lib { ///@} /////////////////////////////////////////////////////////////////////////// - // TODO: Abstract interface to policy subclasses - - SocketBody * body() const; + SocketBody & body() const; + + /////////////////////////////////////////////////////////////////////////// + + virtual unsigned available() const = 0; protected: private: // That SocketBody instance owns us and controls our lifetime // Do we need this ?? - SocketBody* body_; + SocketBody * body_; + + friend class SocketBody; }; /** \brief diff --git a/Socket/SocketProtocol.test.cc b/Socket/SocketProtocol.test.cc index e0b57fbf4..fdd880ca6 100644 --- a/Socket/SocketProtocol.test.cc +++ b/Socket/SocketProtocol.test.cc @@ -28,6 +28,7 @@ // Custom includes #include "SocketProtocol.hh" #include "SocketPolicy.hh" +#include "SocketProtocol.test.hh" #include <boost/test/auto_unit_test.hpp> #include <boost/test/test_tools.hpp> @@ -35,22 +36,13 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -namespace { - typedef satcom::lib::SocketPolicy<> MyProtocol_Policy; - - class MyProtocol - : public satcom::lib::ConcreteSocketProtocol< MyProtocol_Policy > - { - public: - MyProtocol() : satcom::lib::ConcreteSocketProtocol< MyProtocol_Policy >() {} - }; -} - BOOST_AUTO_UNIT_TEST(socketProtocol) { - MyProtocol protocol; + satcom::lib::test::SomeProtocol protocol; + + // This would fail an assertion ... + // BOOST_CHECK( protocol.body() == 0 ); - BOOST_CHECK( protocol.body() == 0 ); protocol.policy(); } diff --git a/Socket/SocketProtocol.test.hh b/Socket/SocketProtocol.test.hh new file mode 100644 index 000000000..aa9036d47 --- /dev/null +++ b/Socket/SocketProtocol.test.hh @@ -0,0 +1,64 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_SocketProtocol_test_ +#define HH_SocketProtocol_test_ 1 + +// Custom includes +#include "SocketProtocol.hh" +#include "SocketPolicy.test.hh" + +//#include "SocketProtocol.test.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { +namespace test { + + class SomeProtocol + : public ConcreteSocketProtocol<SomeSocketPolicy> + { + public: + ~SomeProtocol() {} + + void init_client() const {} + void init_server() const {} + + unsigned available() const { + return Policy::ReadPolicy::TEST_SIZE; + } + }; + +}}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "SocketProtocol.test.cci" +//#include "SocketProtocol.test.ct" +//#include "SocketProtocol.test.cti" +//#include "SocketProtocol.test.mpp" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/StreamFramingPolicy.hh b/Socket/StreamFramingPolicy.hh new file mode 100644 index 000000000..983edc2d5 --- /dev/null +++ b/Socket/StreamFramingPolicy.hh @@ -0,0 +1,49 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_StreamFramingPolicy_ +#define HH_StreamFramingPolicy_ 1 + +// Custom includes + +//#include "StreamFramingPolicy.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + struct StreamFramingPolicy : public FramingPolicyBase + {}; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "StreamFramingPolicy.cci" +//#include "StreamFramingPolicy.ct" +//#include "StreamFramingPolicy.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/TCPProtocol.hh b/Socket/TCPProtocol.hh new file mode 100644 index 000000000..849da76df --- /dev/null +++ b/Socket/TCPProtocol.hh @@ -0,0 +1,49 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_TCPProtocol_ +#define HH_TCPProtocol_ 1 + +// Custom includes + +//#include "TCPProtocol.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + class TCPProtocol + {}; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "TCPProtocol.cci" +//#include "TCPProtocol.ct" +//#include "TCPProtocol.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/TCPSocketHandle.cc b/Socket/TCPSocketHandle.cc new file mode 100644 index 000000000..5ec50d7d1 --- /dev/null +++ b/Socket/TCPSocketHandle.cc @@ -0,0 +1,107 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Definition of non-inline non-template functions + +#include "TCPSocketHandle.hh" +//#include "TCPSocketHandle.ih" + +// Custom includes +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/ioctl.h> + +#include "Utils/Exception.hh" + +//#include "TCPSocketHandle.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +prefix_ void satcom::lib::TCPv4SocketProtocol::init_client() + const +{ + int sock = ::socket(PF_INET,SOCK_STREAM,0); + if (sock < 0) + throw SystemException(errno); + body().fd(sock); +} + +prefix_ void +satcom::lib::TCPv4SocketProtocol::init_client(INet4AddressingPolicy::Address const & address) + const +{ + init_client(); + connect(address); +} + +prefix_ void satcom::lib::TCPv4SocketProtocol::init_client(std::string address) + const +{ + init_client(); + connect(address); +} + +prefix_ void satcom::lib::TCPv4SocketProtocol::init_client(std::string host, unsigned port) + const +{ + init_client(); + connect(host,port); +} + +prefix_ void +satcom::lib::TCPv4SocketProtocol::connect(INet4AddressingPolicy::Address const & address) + const +{ + if (::connect(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0) + throw SystemException(errno); +} + +prefix_ void satcom::lib::TCPv4SocketProtocol::connect(std::string address) + const +{ + connect(INet4AddressingPolicy::Address(address)); +} + +prefix_ void satcom::lib::TCPv4SocketProtocol::connect(std::string host, unsigned port) + const +{ + connect(INet4AddressingPolicy::Address(host,port)); +} + +prefix_ unsigned satcom::lib::TCPv4SocketProtocol::available() + const +{ + int n; + if (::ioctl(body().fd(),FIONREAD,&n) < 0) + throw SystemException(errno); + return n; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "TCPSocketHandle.mpp" + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/TCPSocketHandle.hh b/Socket/TCPSocketHandle.hh new file mode 100644 index 000000000..e7c6ee152 --- /dev/null +++ b/Socket/TCPSocketHandle.hh @@ -0,0 +1,102 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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 HH_TCPSocketHandle_ +#define HH_TCPSocketHandle_ 1 + +// Custom includes +#include "INetProtocol.hh" +#include "TCPProtocol.hh" +#include "StreamFramingPolicy.hh" +#include "CommunicationPolicy.hh" +#include "ReadWritePolicy.hh" +#include "BufferingPolicy.hh" +#include "ProtocolClientSocketHandle.hh" +#include "ProtocolServerSocketHandle.hh" + +//#include "TCPSocketHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + typedef MakeSocketPolicy< + INet4AddressingPolicy, + StreamFramingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy, + WriteablePolicy, + SocketBufferingPolicy + >::policy TCPv4Socket_Policy; + + class TCPv4SocketProtocol + : public ConcreteSocketProtocol<TCPv4Socket_Policy>, + public IPv4Protocol, + public TCPProtocol + { + public: + void init_client() const; + void init_client(INet4AddressingPolicy::Address const & address) const; + void init_client(std::string address) const; + void init_client(std::string host, unsigned port) const; + + void connect(INet4AddressingPolicy::Address const & address) const; + void connect(std::string address) const; + void connect(std::string host, unsigned port) const; + + unsigned available() const; + }; + + typedef ProtocolClientSocketHandle<TCPv4SocketProtocol> TCPv4ClientSocketHandle; + typedef ProtocolServerSocketHandle<TCPv4SocketProtocol> TCPv4ServerSocketHandle; + + typedef MakeSocketPolicy< + INet6AddressingPolicy, + StreamFramingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy, + WriteablePolicy, + SocketBufferingPolicy + >::policy TCPv6Socket_Policy; + + class TCPv6SocketProtocol + : public ConcreteSocketProtocol<TCPv6Socket_Policy>, + public IPv6Protocol, + public TCPProtocol + {}; + + typedef ProtocolClientSocketHandle<TCPv6SocketProtocol> TCPv6ClientSocketHandle; + typedef ProtocolServerSocketHandle<TCPv6SocketProtocol> TCPv6ServerSocketHandle; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "TCPSocketHandle.cci" +//#include "TCPSocketHandle.ct" +//#include "TCPSocketHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/TCPSocketHandle.test.cc b/Socket/TCPSocketHandle.test.cc new file mode 100644 index 000000000..8ee63b866 --- /dev/null +++ b/Socket/TCPSocketHandle.test.cc @@ -0,0 +1,55 @@ +// $Id$ +// +// Copyright (C) 2006 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund <stefan.bund@fokus.fraunhofer.de> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU 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. + +// Unit tests + +//#include "TCPSocketHandle.test.hh" +//#include "TCPSocketHandle.test.ih" + +// Custom includes +#include "TCPSocketHandle.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(tcpSocketHandle) +{ + satcom::lib::TCPv4ClientSocketHandle sock; + + BOOST_CHECK_THROW( sock.protocol().connect(satcom::lib::INet4Address("127.0.0.1:12345")), satcom::lib::SystemException ); + BOOST_CHECK_THROW( sock.protocol().connect("127.0.0.1:12345"), satcom::lib::SystemException ); + BOOST_CHECK_THROW( sock.protocol().connect("127.0.0.1",12345), satcom::lib::SystemException ); + + // TODO: Richtige connection testen +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: -- GitLab