From 91b52a9fa321f0d7a1c6082065747f5e588eb67d Mon Sep 17 00:00:00 2001 From: sbund <sbund@wiback.org> Date: Thu, 17 Aug 2006 07:35:43 +0000 Subject: [PATCH] Add socket library --- Socket/ClientSocketHandle.cti | 62 ++++++++ Socket/ClientSocketHandle.hh | 85 +++++++++++ Socket/ClientSocketHandle.test.cc | 105 ++++++++++++++ Socket/FileHandle.cc | 70 +++++++++ Socket/FileHandle.cci | 164 +++++++++++++++++++++ Socket/FileHandle.hh | 95 ++++++++++++ Socket/FileHandle.ih | 86 +++++++++++ Socket/FileHandle.test.cc | 115 +++++++++++++++ Socket/ProtocolClientSocketHandle.hh | 87 +++++++++++ Socket/SConscript | 11 ++ Socket/ServerSocketHandle.cti | 62 ++++++++ Socket/ServerSocketHandle.hh | 85 +++++++++++ Socket/ServerSocketHandle.test.cc | 106 ++++++++++++++ Socket/SocketHandle.cci | 43 ++++++ Socket/SocketHandle.cti | 72 +++++++++ Socket/SocketHandle.hh | 97 +++++++++++++ Socket/SocketHandle.ih | 73 ++++++++++ Socket/SocketHandle.test.cc | 105 ++++++++++++++ Socket/SocketPolicy.hh | 116 +++++++++++++++ Socket/SocketPolicy.ih | 210 +++++++++++++++++++++++++++ Socket/SocketPolicy.test.cc | 139 ++++++++++++++++++ Socket/SocketProtocol.cci | 52 +++++++ Socket/SocketProtocol.cti | 51 +++++++ Socket/SocketProtocol.hh | 118 +++++++++++++++ Socket/SocketProtocol.test.cc | 64 ++++++++ Socket/main.test.cc | 43 ++++++ Utils/intrusive_refcount.cci | 79 ++++++++++ Utils/intrusive_refcount.hh | 76 ++++++++++ Utils/intrusive_refcount.test.cc | 84 +++++++++++ satscons/BoostUnitTests.py | 6 +- satscons/SatSCons.py | 26 ++-- 31 files changed, 2576 insertions(+), 11 deletions(-) create mode 100644 Socket/ClientSocketHandle.cti create mode 100644 Socket/ClientSocketHandle.hh create mode 100644 Socket/ClientSocketHandle.test.cc create mode 100644 Socket/FileHandle.cc create mode 100644 Socket/FileHandle.cci create mode 100644 Socket/FileHandle.hh create mode 100644 Socket/FileHandle.ih create mode 100644 Socket/FileHandle.test.cc create mode 100644 Socket/ProtocolClientSocketHandle.hh create mode 100644 Socket/SConscript create mode 100644 Socket/ServerSocketHandle.cti create mode 100644 Socket/ServerSocketHandle.hh create mode 100644 Socket/ServerSocketHandle.test.cc create mode 100644 Socket/SocketHandle.cci create mode 100644 Socket/SocketHandle.cti create mode 100644 Socket/SocketHandle.hh create mode 100644 Socket/SocketHandle.ih create mode 100644 Socket/SocketHandle.test.cc create mode 100644 Socket/SocketPolicy.hh create mode 100644 Socket/SocketPolicy.ih create mode 100644 Socket/SocketPolicy.test.cc create mode 100644 Socket/SocketProtocol.cci create mode 100644 Socket/SocketProtocol.cti create mode 100644 Socket/SocketProtocol.hh create mode 100644 Socket/SocketProtocol.test.cc create mode 100644 Socket/main.test.cc create mode 100644 Utils/intrusive_refcount.cci create mode 100644 Utils/intrusive_refcount.hh create mode 100644 Utils/intrusive_refcount.test.cc diff --git a/Socket/ClientSocketHandle.cti b/Socket/ClientSocketHandle.cti new file mode 100644 index 000000000..567d4b647 --- /dev/null +++ b/Socket/ClientSocketHandle.cti @@ -0,0 +1,62 @@ +// $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 "ClientSocketHandle.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ satcom::lib::ClientSocketHandle<SocketPolicy>:: +ClientSocketHandle(ClientSocketHandle<OtherPolicy> other, + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type *) + : SocketHandle<SocketPolicy>(other,true) +{} + +template <class SocketPolicy> +prefix_ satcom::lib::ClientSocketHandle<SocketPolicy>:: +ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol) + : SocketHandle<SocketPolicy>(protocol) +{} + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ typename satcom::lib::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & +satcom::lib::ClientSocketHandle<SocketPolicy>::operator=(ClientSocketHandle<OtherPolicy> other) +{ + assign(other); + return *this; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ClientSocketHandle.hh b/Socket/ClientSocketHandle.hh new file mode 100644 index 000000000..5c8bfa904 --- /dev/null +++ b/Socket/ClientSocketHandle.hh @@ -0,0 +1,85 @@ +// $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_ClientSocketHandle_ +#define HH_ClientSocketHandle_ 1 + +// Custom includes +#include "SocketHandle.hh" + +//#include "ClientSocketHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + template <class SocketPolicy> + class ClientSocketHandle + : public SocketHandle<SocketPolicy> + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + // no default constructor + // default copy constructor + // default copy assignment + // default destructor + + // conversion constructors + template <class OtherPolicy> + ClientSocketHandle(ClientSocketHandle<OtherPolicy> other, + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type * = 0); + + ///@} + /////////////////////////////////////////////////////////////////////////// + + template <class OtherPolicy> + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & + operator=(ClientSocketHandle<OtherPolicy> other); + + protected: + explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol); + + private: + + }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "ClientSocketHandle.cci" +//#include "ClientSocketHandle.ct" +#include "ClientSocketHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ClientSocketHandle.test.cc b/Socket/ClientSocketHandle.test.cc new file mode 100644 index 000000000..373b01829 --- /dev/null +++ b/Socket/ClientSocketHandle.test.cc @@ -0,0 +1,105 @@ +// $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 "ClientSocketHandle.test.hh" +//#include "ClientSocketHandle.test.ih" + +// Custom includes +#include "ClientSocketHandle.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#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() {} + }; + + class MySocketHandle + : public sl::ClientSocketHandle<MyProtocol::Policy> + { + public: + MySocketHandle() + : sl::ClientSocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + {} + }; +} + +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; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/FileHandle.cc b/Socket/FileHandle.cc new file mode 100644 index 000000000..ce0214b5a --- /dev/null +++ b/Socket/FileHandle.cc @@ -0,0 +1,70 @@ +// $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 "FileHandle.hh" +//#include "FileHandle.ih" + +// Custom includes +#include <sys/poll.h> +#include <string.h> +#include <errno.h> +#include "Utils/Exception.hh" + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + bool pollCheck(int fd, int event) + { + struct ::pollfd pfd; + ::memset(&pfd,0,sizeof(pfd)); + pfd.fd = fd; + pfd.events = event; + int rv = ::poll(&pfd,1,0); + if (rv<0) + throw satcom::lib::SystemException(errno); + return rv>0; + } +} + +prefix_ bool satcom::lib::FileBody::readable() + const +{ + return pollCheck(fd(),POLLIN); +} + +prefix_ bool satcom::lib::FileBody::writeable() + const +{ + return pollCheck(fd(),POLLOUT); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/FileHandle.cci b/Socket/FileHandle.cci new file mode 100644 index 000000000..4e3269ccd --- /dev/null +++ b/Socket/FileHandle.cci @@ -0,0 +1,164 @@ +// $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 + +//#include "FileHandle.ih" + +// Custom includes +#include <unistd.h> +#include <errno.h> +#include "Utils/Exception.hh" + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// satcom::lib::FileBody + +prefix_ satcom::lib::FileBody::FileBody(int fd) + : fd_(fd) +{} + +prefix_ satcom::lib::FileBody::~FileBody() +{ + if (valid()) + try { + close(); + } + catch (...) { + terminate(); + } +} + +prefix_ int satcom::lib::FileBody::fd() + const +{ + return fd_; +} + +prefix_ int satcom::lib::FileBody::fd(int fd) +{ + int oldFd (fd_); + fd_ = fd; + return oldFd; +} + +prefix_ void satcom::lib::FileBody::close() +{ + if (::close(fd_) != 0) + throw SystemException(errno); + fd_ = -1; +} + +prefix_ void satcom::lib::FileBody::terminate() +{ + ::close(fd_); + fd_ = -1; +} + +prefix_ void satcom::lib::FileBody::flush() +{} + +prefix_ bool satcom::lib::FileBody::valid() + const +{ + return fd_ != -1; +} + +prefix_ bool satcom::lib::FileBody::ok() + const +{ + return valid(); +} + +/////////////////////////////////////////////////////////////////////////// +// satcom::lib::FileHandle + +prefix_ void satcom::lib::FileHandle::close() +{ + body_->close(); +} + +prefix_ void satcom::lib::FileHandle::terminate() +{ + body_->terminate(); +} + +prefix_ void satcom::lib::FileHandle::flush() +{ + body_->flush(); +} + +prefix_ bool satcom::lib::FileHandle::readable() + const +{ + return body_->readable(); +} + +prefix_ bool satcom::lib::FileHandle::writeable() + const +{ + return body_->writeable(); +} + +prefix_ bool satcom::lib::FileHandle::valid() + const +{ + return body_->valid(); +} + +prefix_ satcom::lib::FileHandle::operator bool () + const +{ + return body_->ok(); +} + +prefix_ bool satcom::lib::FileHandle::operator!() + const +{ + return ! body_->ok(); +} + +prefix_ int satcom::lib::FileHandle::fd() + const +{ + return body_->fd(); +} + +prefix_ satcom::lib::FileHandle::FileHandle(std::auto_ptr<FileBody> body) + : body_(body.release()) +{} + +prefix_ int satcom::lib::FileHandle::fd(int fd) +{ + return body_->fd(fd); +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/FileHandle.hh b/Socket/FileHandle.hh new file mode 100644 index 000000000..70ed6985e --- /dev/null +++ b/Socket/FileHandle.hh @@ -0,0 +1,95 @@ +// $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_FileHandle_ +#define HH_FileHandle_ 1 + +// Custom includes +#include <memory> // std::auto_ptr +#include <boost/intrusive_ptr.hpp> +#include "Utils/intrusive_refcount.hh" + +//#include "FileHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// +#include "FileHandle.ih" + +namespace satcom { +namespace lib { + + /** \brief + */ + class FileHandle + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + // protected default constructor + // default copy constructor + // default copy assignment + // default destructor + + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + + void close(); + void terminate(); + void flush(); + + bool readable() const; + bool writeable() const; + + bool valid() const; + + operator bool () const; + bool operator!() const; + + int fd() const; + + protected: + explicit FileHandle(std::auto_ptr<FileBody> body); + + int fd(int fd); + + private: + FileBody::ptr body_; + }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "FileHandle.cci" +//#include "FileHandle.ct" +//#include "FileHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/FileHandle.ih b/Socket/FileHandle.ih new file mode 100644 index 000000000..0bf15d8db --- /dev/null +++ b/Socket/FileHandle.ih @@ -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 IH_FileHandle_ +#define IH_FileHandle_ 1 + +// Custom includes + +///////////////////////////////ih.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + class FileBody + : public satcom::lib::intrusive_refcount + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + typedef boost::intrusive_ptr<FileBody> ptr; + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + explicit FileBody(int fd=-1); + virtual ~FileBody(); + + // no copy + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + + int fd() const; + int fd(int fd); + + // TODO: Move theese to i_* and make the public iface non-virtual + virtual void close(); + virtual void terminate(); + virtual void flush(); + + bool readable() const; + bool writeable() const; + + virtual bool valid() const; + virtual bool ok() const; + + protected: + + private: + int fd_; + }; + +}} + +///////////////////////////////ih.e//////////////////////////////////////// +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/FileHandle.test.cc b/Socket/FileHandle.test.cc new file mode 100644 index 000000000..c86a26b99 --- /dev/null +++ b/Socket/FileHandle.test.cc @@ -0,0 +1,115 @@ +// $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 "FileHandle.test.hh" +//#include "FileHandle.test.ih" + +// Custom includes +#include <iostream> +#include <fcntl.h> +#include <unistd.h> +#include "FileHandle.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + class FHandle : public satcom::lib::FileHandle + { + public: + FHandle(int fd=-1) + : satcom::lib::FileHandle(std::auto_ptr<satcom::lib::FileBody>( + new satcom::lib::FileBody(fd))) {} + FHandle(std::string name) + : satcom::lib::FileHandle(std::auto_ptr<satcom::lib::FileBody>( + new satcom::lib::FileBody())) + { + int rv = ::open(name.c_str(),O_RDWR|O_NONBLOCK) ; + if (rv<0) + throw satcom::lib::SystemException(errno); + fd(rv); + } + }; +} + +BOOST_AUTO_UNIT_TEST(fileHandle) +{ + try { + { + FHandle fh("/dev/null"); + BOOST_CHECK(fh.fd() != -1); + BOOST_CHECK(fh.valid()); + BOOST_CHECK(fh); + BOOST_CHECK(!!fh); + + FHandle fh2(fh); + BOOST_CHECK_EQUAL(fh.fd(), fh2.fd()); + + BOOST_CHECK(fh.writeable()); + BOOST_CHECK_NO_THROW(fh.close()); + BOOST_CHECK_THROW(fh.close(),satcom::lib::SystemException); + BOOST_CHECK_NO_THROW(fh.terminate()); + } + + { + FHandle fh("/dev/zero"); + BOOST_CHECK(fh.readable()); + } + + { + int fds[2]; + BOOST_REQUIRE(pipe(fds) == 0); + + FHandle fh(fds[0]); + BOOST_CHECK(!fh.readable()); + + // Set non-blocking IO and fill the pipe buffer + int flags = fcntl(fds[1],F_GETFL,0); + if (flags == -1) + BOOST_FAIL(strerror(errno)); + if (fcntl(fds[1],F_SETFL,flags|O_NONBLOCK) == -1) + BOOST_FAIL(strerror(errno)); + char buffer[1024]; + while (write(fds[1],buffer,1024) == 1024) ; + + FHandle fh2(fds[1]); + BOOST_CHECK(!fh.writeable()); + } + } + catch (std::exception const & ex) { + BOOST_FAIL(ex.what()); + } +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ProtocolClientSocketHandle.hh b/Socket/ProtocolClientSocketHandle.hh new file mode 100644 index 000000000..7872b645b --- /dev/null +++ b/Socket/ProtocolClientSocketHandle.hh @@ -0,0 +1,87 @@ +// $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_ProtocolClientSocketHandle_ +#define HH_ProtocolClientSocketHandle_ 1 + +// Custom includes + +//#include "ProtocolClientSocketHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + template <class SocketProtocol> + class ProtocolClientSocketHandle + : public ClientSocketHandle<typename SocketProtocol::Policy> + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + typedef SocketProtocol Protocol; + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + // default default constructor + // default copy constructor + // default copy assignment + // default destructor + + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + ///\name Accessors + ///@{ + + ///@} + /////////////////////////////////////////////////////////////////////////// + ///\name Mutators + ///@{ + + ///@} + + protected: + + private: + + }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "ProtocolClientSocketHandle.cci" +//#include "ProtocolClientSocketHandle.ct" +//#include "ProtocolClientSocketHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SConscript b/Socket/SConscript new file mode 100644 index 000000000..03990f8e1 --- /dev/null +++ b/Socket/SConscript @@ -0,0 +1,11 @@ +Import('env') +import SatSCons + +########################################################################### + +SatSCons.StandardTargets(env) + +SatSCons.Lib(env, + library = 'Socket', + sources = SatSCons.GlobSources(), + LIBS = [ 'Utils' ]) diff --git a/Socket/ServerSocketHandle.cti b/Socket/ServerSocketHandle.cti new file mode 100644 index 000000000..27b5b4b07 --- /dev/null +++ b/Socket/ServerSocketHandle.cti @@ -0,0 +1,62 @@ +// $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 "ServerSocketHandle.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ satcom::lib::ServerSocketHandle<SocketPolicy>:: +ServerSocketHandle(ServerSocketHandle<OtherPolicy> other, + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type *) + : SocketHandle<SocketPolicy>(other,true) +{} + +template <class SocketPolicy> +prefix_ satcom::lib::ServerSocketHandle<SocketPolicy>:: +ServerSocketHandle(std::auto_ptr<SocketProtocol> protocol) + : SocketHandle<SocketPolicy>(protocol) +{} + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ typename satcom::lib::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & +satcom::lib::ServerSocketHandle<SocketPolicy>::operator=(ServerSocketHandle<OtherPolicy> other) +{ + assign(other); + return *this; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ServerSocketHandle.hh b/Socket/ServerSocketHandle.hh new file mode 100644 index 000000000..0fad2cd99 --- /dev/null +++ b/Socket/ServerSocketHandle.hh @@ -0,0 +1,85 @@ +// $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_ServerSocketHandle_ +#define HH_ServerSocketHandle_ 1 + +// Custom includes +#include "SocketHandle.hh" + +//#include "ServerSocketHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + template <class SocketPolicy> + class ServerSocketHandle + : public SocketHandle<SocketPolicy> + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + // no default constructor + // default copy constructor + // default copy assignment + // default destructor + + // conversion constructors + template <class OtherPolicy> + ServerSocketHandle(ServerSocketHandle<OtherPolicy> other, + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type * = 0); + + ///@} + /////////////////////////////////////////////////////////////////////////// + + template <class OtherPolicy> + typename SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & + operator=(ServerSocketHandle<OtherPolicy> other); + + protected: + explicit ServerSocketHandle(std::auto_ptr<SocketProtocol> protocol); + + private: + + }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "ServerSocketHandle.cci" +//#include "ServerSocketHandle.ct" +#include "ServerSocketHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/ServerSocketHandle.test.cc b/Socket/ServerSocketHandle.test.cc new file mode 100644 index 000000000..f7e8e7d7d --- /dev/null +++ b/Socket/ServerSocketHandle.test.cc @@ -0,0 +1,106 @@ +// $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 "ServerSocketHandle.test.hh" +//#include "ServerSocketHandle.test.ih" + +// Custom includes +#include "ServerSocketHandle.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#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() {} + }; + + class MySocketHandle + : public sl::ServerSocketHandle<MyProtocol::Policy> + { + public: + MySocketHandle() + : sl::ServerSocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + {} + }; +} + +BOOST_AUTO_UNIT_TEST(serverSocketHandle) +{ + typedef sl::MakeSocketPolicy< + StreamFramingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy, + WritablePolicy + >::policy StreamSocketPolicy; + typedef sl::SocketHandle<StreamSocketPolicy> StreamSocketHandle; + + MySocketHandle myh; + StreamSocketHandle ssh (myh); + ssh = myh; +} + + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketHandle.cci b/Socket/SocketHandle.cci new file mode 100644 index 000000000..226cfde23 --- /dev/null +++ b/Socket/SocketHandle.cci @@ -0,0 +1,43 @@ +// $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 + +#include "SocketHandle.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ satcom::lib::SocketBody::SocketBody(std::auto_ptr<SocketProtocol> protocol) + : protocol_(protocol) +{} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketHandle.cti b/Socket/SocketHandle.cti new file mode 100644 index 000000000..88c55c530 --- /dev/null +++ b/Socket/SocketHandle.cti @@ -0,0 +1,72 @@ +// $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 "SocketHandle.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(SocketHandle<OtherPolicy> other, + typename IsCompatible<OtherPolicy>::type *) + : FileHandle(other) +{} + +template <class SocketPolicy> +template <class OtherPolicy> +prefix_ typename satcom::lib::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const & +satcom::lib::SocketHandle<SocketPolicy>::operator=(SocketHandle<OtherPolicy> other) +{ + assign(other); + return *this; +} + +template <class SocketPolicy> +prefix_ +satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(std::auto_ptr<SocketProtocol> protocol) + : FileHandle(std::auto_ptr<FileBody>(new SocketBody(protocol))) +{} + +template <class SocketPolicy> +prefix_ satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(FileHandle other, bool isChecked) + : FileHandle(other) +{} + +template <class SocketPolicy> +prefix_ void satcom::lib::SocketHandle<SocketPolicy>::assign(FileHandle other) +{ + FileHandle::operator=(other); +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketHandle.hh b/Socket/SocketHandle.hh new file mode 100644 index 000000000..8e5b1d4fe --- /dev/null +++ b/Socket/SocketHandle.hh @@ -0,0 +1,97 @@ +// $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_SocketHandle_ +#define HH_SocketHandle_ 1 + +// Custom includes +#include <memory> // std::auto_ptr +#include "FileHandle.hh" +#include "SocketPolicy.hh" + +//#include "SocketHandle.mpp" +///////////////////////////////hh.p//////////////////////////////////////// +#include "SocketHandle.ih" + +namespace satcom { +namespace lib { + + /** \brief + */ + template <class SocketPolicy> + class SocketHandle + : public FileHandle + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + typedef SocketPolicy Policy; + + template <class OtherPolicy> + struct IsCompatible + : public boost::enable_if< SocketPolicyIsBaseOf<SocketPolicy,OtherPolicy>, + SocketHandle > + {}; + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + // default copy constructor + // default copy assignment + // default destructor + + // conversion constructors + template <class OtherPolicy> + SocketHandle(SocketHandle<OtherPolicy> other, + typename IsCompatible<OtherPolicy>::type * = 0); + + ///@} + /////////////////////////////////////////////////////////////////////////// + + template <class OtherPolicy> + typename IsCompatible<OtherPolicy>::type const & operator=(SocketHandle<OtherPolicy> other); + + protected: + explicit SocketHandle(std::auto_ptr<SocketProtocol> protocol); + SocketHandle(FileHandle other, bool isChecked); + void assign(FileHandle other); + + private: + + }; + + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "SocketHandle.cci" +//#include "SocketHandle.ct" +#include "SocketHandle.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketHandle.ih b/Socket/SocketHandle.ih new file mode 100644 index 000000000..91798dec5 --- /dev/null +++ b/Socket/SocketHandle.ih @@ -0,0 +1,73 @@ +// $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 IH_SocketHandle_ +#define IH_SocketHandle_ 1 + +// Custom includes +#include <boost/scoped_ptr.hpp> +#include "SocketProtocol.hh" + +///////////////////////////////ih.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + class SocketBody + : public FileBody + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + typedef boost::intrusive_ptr<SocketBody> ptr; + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + SocketBody(std::auto_ptr<SocketProtocol> protocol); + + // no copy + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + + protected: + + private: + boost::scoped_ptr<SocketProtocol> protocol_; + }; + +}} + +///////////////////////////////ih.e//////////////////////////////////////// +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketHandle.test.cc b/Socket/SocketHandle.test.cc new file mode 100644 index 000000000..b66acfd4e --- /dev/null +++ b/Socket/SocketHandle.test.cc @@ -0,0 +1,105 @@ +// $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 "SocketHandle.test.hh" +//#include "SocketHandle.test.ih" + +// Custom includes +#include "SocketHandle.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#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() {} + }; + + class MySocketHandle + : public sl::SocketHandle<MyProtocol::Policy> + { + public: + MySocketHandle() + : sl::SocketHandle<MyProtocol::Policy>(std::auto_ptr<sl::SocketProtocol>(new MyProtocol())) + {} + }; +} + +BOOST_AUTO_UNIT_TEST(socketHandle) +{ + typedef sl::MakeSocketPolicy< + StreamFramingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy, + WritablePolicy + >::policy StreamSocketPolicy; + typedef sl::SocketHandle<StreamSocketPolicy> StreamSocketHandle; + + MySocketHandle myh; + StreamSocketHandle ssh (myh); + ssh = myh; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketPolicy.hh b/Socket/SocketPolicy.hh new file mode 100644 index 000000000..7ddcb9dc3 --- /dev/null +++ b/Socket/SocketPolicy.hh @@ -0,0 +1,116 @@ +// $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_ +#define HH_SocketPolicy_ 1 + +// Custom includes +#include <boost/type_traits.hpp> + +//#include "SocketPolicy.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + class AddressingPolicyBase + {}; + typedef AddressingPolicyBase UnspecifiedAddressingPolicy; + + class FramingPolicyBase + {}; + typedef FramingPolicyBase UnspecifiedFramingPolicy; + + class CommunicationPolicyBase + {}; + typedef CommunicationPolicyBase UnspecifiedCommunicationPolicy; + + class ReadPolicyBase + {}; + typedef ReadPolicyBase UnspecifiedReadPolicy; + + class WritePolicyBase + {}; + typedef WritePolicyBase UnspecifiedWritePolicy; + + class BufferingPolicyBase + {}; + typedef BufferingPolicyBase UnspecifiedBufferingPolicy; + + class SocketPolicyBase + { + typedef AddressingPolicyBase AddressingPolicy; + typedef FramingPolicyBase FramingPolicy; + typedef CommunicationPolicyBase CommunicationPolicy; + typedef ReadPolicyBase ReadPolicy; + typedef WritePolicyBase WritePolicy; + typedef BufferingPolicyBase BufferingPolicy; + }; + + template <class AddressingPolicy_=AddressingPolicyBase, + class FramingPolicy_=FramingPolicyBase, + class CommunicationPolicy_=CommunicationPolicyBase, + class ReadPolicy_=ReadPolicyBase, + class WritePolicy_=WritePolicyBase, + class BufferingPolicy_=BufferingPolicyBase> + class SocketPolicy + : public SocketPolicyBase + { + public: + typedef AddressingPolicy_ AddressingPolicy; + typedef FramingPolicy_ FramingPolicy; + typedef CommunicationPolicy_ CommunicationPolicy; + typedef ReadPolicy_ ReadPolicy; + typedef WritePolicy_ WritePolicy; + typedef BufferingPolicy_ BufferingPolicy; + + AddressingPolicy addressingPolicy; + FramingPolicy framingPolicy; + CommunicationPolicy communicationPolicy; + ReadPolicy readPolicy; + WritePolicy writePolicy; + BufferingPolicy bufferingPolicy; + }; + + struct nil {}; + + template <class T1, class T2=nil, class T3=nil, class T4=nil, class T5=nil, class T6=nil> + class MakeSocketPolicy; + // the rest is implementation detail + + template <class BasePolicy, class DerivedPolicy> + struct SocketPolicyIsBaseOf; + // the rest is implementation detail ... +}} + +//////////////////////////////hh.e//////////////////////////////////////// +#include "SocketPolicy.ih" +//#include "SocketPolicy.cci" +//#include "SocketPolicy.ct" +//#include "SocketPolicy.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketPolicy.ih b/Socket/SocketPolicy.ih new file mode 100644 index 000000000..92f187275 --- /dev/null +++ b/Socket/SocketPolicy.ih @@ -0,0 +1,210 @@ +// $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 IH_SocketPolicy_ +#define IH_SocketPolicy_ 1 + +// Custom includes +#include <boost/mpl/vector.hpp> +#include <boost/mpl/fold.hpp> +#include <boost/mpl/if.hpp> +#include <boost/mpl/and.hpp> + +///////////////////////////////ih.p//////////////////////////////////////// + +namespace satcom { +namespace lib { +namespace impl{ + + template <int N> + struct SocketPolicy_rv + { + int v[N]; + }; + + SocketPolicy_rv<1> MakeSocketPolicy_merge_(AddressingPolicyBase*); + SocketPolicy_rv<2> MakeSocketPolicy_merge_(FramingPolicyBase*); + SocketPolicy_rv<3> MakeSocketPolicy_merge_(CommunicationPolicyBase*); + SocketPolicy_rv<4> MakeSocketPolicy_merge_(ReadPolicyBase*); + SocketPolicy_rv<5> MakeSocketPolicy_merge_(WritePolicyBase*); + SocketPolicy_rv<6> MakeSocketPolicy_merge_(BufferingPolicyBase*); + + template <class Base, class Policy, int N> + struct MakeSocketPolicy_merge + {}; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<1>)> + { + typedef SocketPolicy< + Policy, + typename Base::FramingPolicy, + typename Base::CommunicationPolicy, + typename Base::ReadPolicy, + typename Base::WritePolicy, + typename Base::BufferingPolicy> type; + }; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<2>)> + { + typedef SocketPolicy< + typename Base::AddressingPolicy, + Policy, + typename Base::CommunicationPolicy, + typename Base::ReadPolicy, + typename Base::WritePolicy, + typename Base::BufferingPolicy> type; + }; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<3>)> + { + typedef SocketPolicy< + typename Base::AddressingPolicy, + typename Base::FramingPolicy, + Policy, + typename Base::ReadPolicy, + typename Base::WritePolicy, + typename Base::BufferingPolicy> type; + }; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<4>)> + { + typedef SocketPolicy< + typename Base::AddressingPolicy, + typename Base::FramingPolicy, + typename Base::CommunicationPolicy, + Policy, + typename Base::WritePolicy, + typename Base::BufferingPolicy> type; + }; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<5>)> + { + typedef SocketPolicy< + typename Base::AddressingPolicy, + typename Base::FramingPolicy, + typename Base::CommunicationPolicy, + typename Base::ReadPolicy, + Policy, + typename Base::BufferingPolicy> type; + }; + + template <class Base, class Policy> + struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<6>)> + { + typedef SocketPolicy< + typename Base::AddressingPolicy, + typename Base::FramingPolicy, + typename Base::CommunicationPolicy, + typename Base::ReadPolicy, + typename Base::WritePolicy, + Policy> type; + }; + + struct MakeSocketPolicy_fold + { + template <class Base, class Policy> + struct apply + : MakeSocketPolicy_merge<Base, + Policy, + sizeof(MakeSocketPolicy_merge_(static_cast<Policy*>(0)))> + {}; + + template <class Base> + struct apply<Base,nil> + { + typedef Base type; + }; + }; + + template <class Base, class Vector> + struct MakeSocketPolicy_impl + { + typedef typename boost::mpl::fold< Vector, Base, MakeSocketPolicy_fold >::type policy; + }; + + template <class Base> + SocketPolicy_rv<1> SocketPolicy_checkcompat_(typename Base::AddressingPolicy*, + typename Base::FramingPolicy*, + typename Base::CommunicationPolicy*, + typename Base::ReadPolicy*, + typename Base::WritePolicy*, + typename Base::BufferingPolicy*); + + template <class Base> + SocketPolicy_rv<2> SocketPolicy_checkcompat_(void*, void*, void*, void*, void*, void*); + + template <int Size> + struct SocketPolicy_checkcompat + : public boost::false_type + {}; + + template<> + struct SocketPolicy_checkcompat<sizeof(SocketPolicy_rv<1>)> + : public boost::true_type + {}; + + template <class Base, class Derived> + struct SocketPolicy_compatibility + : public SocketPolicy_checkcompat< sizeof(SocketPolicy_checkcompat_<Base>( + static_cast<typename Derived::AddressingPolicy*>(0), + static_cast<typename Derived::FramingPolicy*>(0), + static_cast<typename Derived::CommunicationPolicy*>(0), + static_cast<typename Derived::ReadPolicy*>(0), + static_cast<typename Derived::WritePolicy*>(0), + static_cast<typename Derived::BufferingPolicy*>(0))) > + {}; + +}}} + +namespace satcom { +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>, + 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 + {}; + + 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> >, + impl::SocketPolicy_compatibility<BasePolicy,DerivedPolicy>, + boost::false_type >::type + {}; +}} + +///////////////////////////////ih.e//////////////////////////////////////// +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketPolicy.test.cc b/Socket/SocketPolicy.test.cc new file mode 100644 index 000000000..9b1ab29e0 --- /dev/null +++ b/Socket/SocketPolicy.test.cc @@ -0,0 +1,139 @@ +// $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 "SocketPolicy.test.hh" +//#include "SocketPolicy.test.ih" + +// Custom includes +#include "SocketPolicy.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> +#include <boost/mpl/assert.hpp> +#include <boost/concept_check.hpp> +#include <boost/utility.hpp> // enable_if + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +using namespace satcom::lib; + +namespace { + struct INetAddressingPolicy : public AddressingPolicyBase {}; + struct UnixAddressingPolicy : public AddressingPolicyBase {}; + + struct StreamFramingPolicy : public FramingPolicyBase {}; + struct DgramFramingPolicy : public FramingPolicyBase {}; + + struct ConnectedCommunicationPolicy : public CommunicationPolicyBase {}; + struct UnconnectedCommunicationPolicy : public CommunicationPolicyBase {}; + + struct ReadablePolicy : public ReadPolicyBase {}; + struct UnreadablePolicy : public ReadPolicyBase {}; + + struct WritablePolicy : public WritePolicyBase {}; + struct UnwritablePolicy : public WritePolicyBase {}; + + struct SocketBufferingPolicy : public BufferingPolicyBase {}; + + template <class Policy> + struct ConvertibleValue + { + ConvertibleValue() {} + ConvertibleValue(ConvertibleValue const & other) {} + + template <class OtherPolicy> + ConvertibleValue(ConvertibleValue<OtherPolicy> const & other, + typename boost::enable_if< SocketPolicyIsBaseOf<Policy,OtherPolicy> >::type * = 0) + {} + + ConvertibleValue const & operator=(ConvertibleValue const & other) + { return *this; } + + template <class OtherPolicy> + typename boost::enable_if< SocketPolicyIsBaseOf<Policy,OtherPolicy>, + ConvertibleValue >::type const & + operator=(ConvertibleValue<OtherPolicy> const & other) + { return *this; } + }; + +} + +BOOST_AUTO_UNIT_TEST(socketPolicy) +{ + // All these checks are really compile-time checks ... + + typedef MakeSocketPolicy< + UnixAddressingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy>::policy Policy1; + + typedef SocketPolicy< + UnixAddressingPolicy, + UnspecifiedFramingPolicy, + ConnectedCommunicationPolicy, + ReadablePolicy, + UnspecifiedWritePolicy, + UnspecifiedBufferingPolicy> Policy2; + + BOOST_MPL_ASSERT(( boost::is_same<Policy1,Policy2> )); + + typedef MakeSocketPolicy< + Policy1, + UnspecifiedCommunicationPolicy>::policy Policy3; + + typedef SocketPolicy< + UnixAddressingPolicy, + UnspecifiedFramingPolicy, + UnspecifiedCommunicationPolicy, + ReadablePolicy, + UnspecifiedWritePolicy, + UnspecifiedBufferingPolicy> Policy4; + + BOOST_MPL_ASSERT(( boost::is_same<Policy3,Policy4> )); + BOOST_MPL_ASSERT_NOT(( boost::is_same<Policy1, Policy3> )); + + BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy3,Policy1> )); + BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,Policy3> )); + BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,int> )); + + // The following should fail at compile time + // BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy1,Policy3> )); + + ConvertibleValue<Policy1> p1; + ConvertibleValue<Policy3> p3(p1); + + p3 = p1; + // The following should fail at compile time + // p1 = p3; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketProtocol.cci b/Socket/SocketProtocol.cci new file mode 100644 index 000000000..4c28f2884 --- /dev/null +++ b/Socket/SocketProtocol.cci @@ -0,0 +1,52 @@ +// $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 + +//#include "SocketProtocol.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ satcom::lib::SocketProtocol::SocketProtocol() + : body_(0) +{} + +prefix_ satcom::lib::SocketProtocol::~SocketProtocol() +{} + +prefix_ satcom::lib::SocketBody * satcom::lib::SocketProtocol::body() + const +{ + return body_; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketProtocol.cti b/Socket/SocketProtocol.cti new file mode 100644 index 000000000..f2b328726 --- /dev/null +++ b/Socket/SocketProtocol.cti @@ -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. + +// Definition of inline template functions + +//#include "SocketProtocol.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template <class SocketPolicy> +prefix_ satcom::lib::ConcreteSocketProtocol<SocketPolicy>::~ConcreteSocketProtocol() +{} + +template <class SocketPolicy> +prefix_ typename satcom::lib::ConcreteSocketProtocol<SocketPolicy>::Policy const & +satcom::lib::ConcreteSocketProtocol<SocketPolicy>::policy() + const +{ + return policy_; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketProtocol.hh b/Socket/SocketProtocol.hh new file mode 100644 index 000000000..c9161f5df --- /dev/null +++ b/Socket/SocketProtocol.hh @@ -0,0 +1,118 @@ +// $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_ +#define HH_SocketProtocol_ 1 + +// Custom includes +#include <boost/utility.hpp> + +//#include "SocketProtocol.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + class SocketBody; + + /** \brief + */ + class SocketProtocol + : boost::noncopyable + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + SocketProtocol(); + virtual ~SocketProtocol() = 0; + + // no default constructor + // no copy + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + + // TODO: Abstract interface to policy subclasses + + SocketBody * body() const; + + protected: + + private: + // That SocketBody instance owns us and controls our lifetime + // Do we need this ?? + SocketBody* body_; + }; + + /** \brief + */ + template <class SocketPolicy> + class ConcreteSocketProtocol + : public SocketProtocol + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + + typedef SocketPolicy Policy; + + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + ~ConcreteSocketProtocol() = 0; + + // no default constructor + // no copy + // no conversion constructors + + ///@} + /////////////////////////////////////////////////////////////////////////// + + Policy const & policy() const; + + protected: + + private: + Policy policy_; + + }; + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "SocketProtocol.cci" +//#include "SocketProtocol.ct" +#include "SocketProtocol.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/SocketProtocol.test.cc b/Socket/SocketProtocol.test.cc new file mode 100644 index 000000000..e0b57fbf4 --- /dev/null +++ b/Socket/SocketProtocol.test.cc @@ -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. + +// Unit tests + +//#include "SocketProtocol.test.hh" +//#include "SocketProtocol.test.ih" + +// Custom includes +#include "SocketProtocol.hh" +#include "SocketPolicy.hh" + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#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; + + BOOST_CHECK( protocol.body() == 0 ); + protocol.policy(); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Socket/main.test.cc b/Socket/main.test.cc new file mode 100644 index 000000000..fd4c97a9b --- /dev/null +++ b/Socket/main.test.cc @@ -0,0 +1,43 @@ +// $Id: main.test.cc 32 2006-03-23 16:24:56Z sbund $ +// +// 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 "test.hh" +//#include "test.ih" + +// Custom includes +#define BOOST_AUTO_TEST_MAIN +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/Utils/intrusive_refcount.cci b/Utils/intrusive_refcount.cci new file mode 100644 index 000000000..a6a9d1a99 --- /dev/null +++ b/Utils/intrusive_refcount.cci @@ -0,0 +1,79 @@ +// $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 + +//#include "intrusive_refcount.ih" + +// Custom includes +#include <boost/assert.hpp> + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ satcom::lib::intrusive_refcount::refcount_t satcom::lib::intrusive_refcount::refcount() +{ + return refcount_; +} + +prefix_ bool satcom::lib::intrusive_refcount::is_shared() +{ + return refcount()>1; +} + +prefix_ satcom::lib::intrusive_refcount::intrusive_refcount() + : refcount_(0) +{} + +prefix_ satcom::lib::intrusive_refcount::~intrusive_refcount() +{} + +prefix_ void satcom::lib::intrusive_refcount::add_ref() +{ + ++refcount_; +} + +prefix_ bool satcom::lib::intrusive_refcount::release() +{ + BOOST_ASSERT(refcount_>0); + return --refcount_ == 0; +} + +prefix_ void satcom::lib::intrusive_ptr_add_ref(intrusive_refcount* p) +{ + p->add_ref(); +} + +prefix_ void satcom::lib::intrusive_ptr_release(intrusive_refcount* p) +{ + if (p->release()) + delete p; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Utils/intrusive_refcount.hh b/Utils/intrusive_refcount.hh new file mode 100644 index 000000000..a63dbe67d --- /dev/null +++ b/Utils/intrusive_refcount.hh @@ -0,0 +1,76 @@ +// $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_intrusive_refcount_ +#define HH_intrusive_refcount_ 1 + +// Custom includes +#include <boost/utility.hpp> + +//#include "intrusive_refcount.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + /** \brief + */ + class intrusive_refcount + : public boost::noncopyable + { + public: + typedef unsigned refcount_t; + + virtual ~intrusive_refcount(); + + refcount_t refcount(); + bool is_shared(); + + protected: + intrusive_refcount(); + + private: + void add_ref(); + bool release(); + + refcount_t refcount_; + + friend void satcom::lib::intrusive_ptr_add_ref(intrusive_refcount* p); + friend void satcom::lib::intrusive_ptr_release(intrusive_refcount* p); + }; + + void intrusive_ptr_add_ref(intrusive_refcount* p); + void intrusive_ptr_release(intrusive_refcount* p); + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "intrusive_refcount.cci" +//#include "intrusive_refcount.ct" +//#include "intrusive_refcount.cti" +#endif + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/Utils/intrusive_refcount.test.cc b/Utils/intrusive_refcount.test.cc new file mode 100644 index 000000000..f09e4bc6a --- /dev/null +++ b/Utils/intrusive_refcount.test.cc @@ -0,0 +1,84 @@ +// $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 "intrusive_refcount.test.hh" +//#include "intrusive_refcount.test.ih" + +// Custom includes +#include "intrusive_refcount.hh" +#include <boost/intrusive_ptr.hpp> + +#include <boost/test/auto_unit_test.hpp> +#include <boost/test/test_tools.hpp> + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + struct Tester + : public satcom::lib::intrusive_refcount + { + typedef boost::intrusive_ptr<Tester> ptr; + + Tester() { ++counter; } + ~Tester() { --counter; } + + static unsigned counter; + }; + + unsigned Tester::counter = 0; +} + +BOOST_AUTO_UNIT_TEST(intrusive_refcount) +{ + BOOST_CHECK_EQUAL(Tester::counter,0u); + + Tester::ptr p (new Tester); + BOOST_CHECK_EQUAL(Tester::counter,1u); + BOOST_CHECK_EQUAL(p->refcount(),1u); + BOOST_CHECK_EQUAL(p->is_shared(),false); + + { + Tester::ptr pp (p); + BOOST_CHECK_EQUAL(Tester::counter,1u); + BOOST_CHECK_EQUAL(p->refcount(),2u); + BOOST_CHECK_EQUAL(p->is_shared(),true); + } + + BOOST_CHECK_EQUAL(Tester::counter,1u); + BOOST_CHECK_EQUAL(p->refcount(),1u); + BOOST_CHECK_EQUAL(p->is_shared(),false); + + p = 0; + BOOST_CHECK_EQUAL(Tester::counter,0u); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// c-file-style: "satcom" +// End: diff --git a/satscons/BoostUnitTests.py b/satscons/BoostUnitTests.py index 27e5154ae..63b5976ed 100644 --- a/satscons/BoostUnitTests.py +++ b/satscons/BoostUnitTests.py @@ -13,7 +13,11 @@ def BoostUnitTests(env, target, source, test_source=None, LIBS = [], DEPENDS = [ testEnv = env.Copy(**kw) testEnv.Append(LIBS = '$BOOSTTESTLIB') testEnv.Append(LIBS = LIBS) - testRunner = testEnv.Program(target, env.Object(source) + test_source) + sources = [] + if source: + sources = sources + env.Object(source) + sources = sources + test_source + testRunner = testEnv.Program(target, sources) if DEPENDS: env.Depends(testRunner, DEPENDS) return env.Command(os.path.join(path,'.'+name+'.stamp'), testRunner, diff --git a/satscons/SatSCons.py b/satscons/SatSCons.py index 8c18e3635..8ad92960b 100644 --- a/satscons/SatSCons.py +++ b/satscons/SatSCons.py @@ -96,8 +96,10 @@ def Objects(env, sources, testSources = None, LIBS = []): if type(sources) == type(()): testSources = sources[1] sources = sources[0] - - objects = env.Object(sources) + + objects = None + if sources: + objects = env.Object(sources) if testSources: test = env.BoostUnitTests( @@ -128,16 +130,20 @@ def Doxygen(env, sources, testSources = None): def Lib(env, library, sources, testSources = None, LIBS = []): objects = Objects(env,sources,testSources,LIBS=LIBS) - lib = env.Library(env.File(LibPath(library)),objects) - env.Default(lib) - env.Append(ALLLIBS = library) + lib = None + if objects: + lib = env.Library(env.File(LibPath(library)),objects) + env.Default(lib) + env.Append(ALLLIBS = library) return lib def Binary(env, binary, sources, testSources = None, LIBS = []): objects = Objects(env,sources,testSources,LIBS=LIBS) - progEnv = env.Copy() - progEnv.Append(LIBS = LIBS) - program = progEnv.Program(target=binary,source=objects) - env.Default(program) - env.Depends(program, [ env.File(LibPath(x)) for x in LIBS ]) + program = None + if objects: + progEnv = env.Copy() + progEnv.Append(LIBS = LIBS) + program = progEnv.Program(target=binary,source=objects) + env.Default(program) + env.Depends(program, [ env.File(LibPath(x)) for x in LIBS ]) return program -- GitLab