From b968e7d9872759cb6e9ff9b3df274518fd11da71 Mon Sep 17 00:00:00 2001
From: sbund <sbund@wiback.org>
Date: Thu, 7 Sep 2006 11:46:13 +0000
Subject: [PATCH] Finished SocketHandle state() implementation

---
 Socket/ClientSocketHandle.cti             | 15 +++++++
 Socket/ClientSocketHandle.hh              |  6 ++-
 Socket/ProtocolClientSocketHandle.cti     | 18 ++++++++
 Socket/ProtocolClientSocketHandle.hh      |  3 ++
 Socket/ProtocolServerSocketHandle.cti     | 18 ++++++++
 Socket/ProtocolServerSocketHandle.hh      |  3 ++
 Socket/ProtocolServerSocketHandle.test.cc |  9 ++++
 Socket/ServerSocketHandle.cti             | 15 +++++++
 Socket/ServerSocketHandle.hh              |  4 ++
 Socket/SocketHandle.cc                    |  8 +++-
 Socket/SocketHandle.cci                   | 10 +++++
 Socket/SocketHandle.ct                    | 50 +++++++++++++++++++++++
 Socket/SocketHandle.cti                   | 10 +++++
 Socket/SocketHandle.hh                    |  9 ++--
 Socket/SocketHandle.ih                    | 13 +++++-
 Socket/SocketHandle.test.cc               |  9 ++++
 16 files changed, 193 insertions(+), 7 deletions(-)
 create mode 100644 Socket/SocketHandle.ct

diff --git a/Socket/ClientSocketHandle.cti b/Socket/ClientSocketHandle.cti
index 1e3112133..eec7cf7e7 100644
--- a/Socket/ClientSocketHandle.cti
+++ b/Socket/ClientSocketHandle.cti
@@ -197,6 +197,21 @@ satcom::lib::ClientSocketHandle<Policy>::cast_dynamic(FileHandle handle)
     return cast_static(handle);
 }
 
+template <class Policy>
+prefix_ void satcom::lib::ClientSocketHandle<Policy>::state(SocketStateMap & map, unsigned lod)
+{
+    map["handle"] = prettyName(typeid(*this));
+    this->body().state(map,lod);
+}
+
+template <class Policy>
+prefix_ std::string satcom::lib::ClientSocketHandle<Policy>::dumpState(unsigned lod)
+{
+    SocketStateMap map;
+    state(map,lod);
+    return detail::dumpState(map);
+}
+
 ///////////////////////////////cti.e///////////////////////////////////////
 #undef prefix_
 
diff --git a/Socket/ClientSocketHandle.hh b/Socket/ClientSocketHandle.hh
index 3b5b8b571..0e670ab45 100644
--- a/Socket/ClientSocketHandle.hh
+++ b/Socket/ClientSocketHandle.hh
@@ -125,7 +125,11 @@ namespace lib {
         
         static ClientSocketHandle cast_static(FileHandle handle);
         static ClientSocketHandle cast_dynamic(FileHandle handle);
-                 
+
+        // we need to override both since SocketHandle is *not* polymorphic
+        void state(SocketStateMap & map, unsigned lod=0);
+        std::string dumpState(unsigned lod=0);
+
     protected:
         ClientSocketHandle(FileHandle other, bool isChecked);
         explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol,
diff --git a/Socket/ProtocolClientSocketHandle.cti b/Socket/ProtocolClientSocketHandle.cti
index d33f3e13d..0f01b8899 100644
--- a/Socket/ProtocolClientSocketHandle.cti
+++ b/Socket/ProtocolClientSocketHandle.cti
@@ -74,6 +74,24 @@ satcom::lib::ProtocolClientSocketHandle<SocketProtocol>::cast_dynamic(FileHandle
     return cast_static(handle);
 }
 
+template <class SocketProtocol>
+prefix_ void
+satcom::lib::ProtocolClientSocketHandle<SocketProtocol>::state(SocketStateMap & map,
+                                                               unsigned lod)
+{
+    map["handle"] = prettyName(typeid(*this));
+    this->body().state(map,lod);
+}
+
+template <class SocketProtocol>
+prefix_ std::string
+satcom::lib::ProtocolClientSocketHandle<SocketProtocol>::dumpState(unsigned lod)
+{
+    SocketStateMap map;
+    state(map,lod);
+    return detail::dumpState(map);
+}
+
 ///////////////////////////////cti.e///////////////////////////////////////
 #undef prefix_
 
diff --git a/Socket/ProtocolClientSocketHandle.hh b/Socket/ProtocolClientSocketHandle.hh
index cf7ccc136..eeca1e762 100644
--- a/Socket/ProtocolClientSocketHandle.hh
+++ b/Socket/ProtocolClientSocketHandle.hh
@@ -63,6 +63,9 @@ namespace lib {
         static ProtocolClientSocketHandle cast_static(FileHandle handle);
         static ProtocolClientSocketHandle cast_dynamic(FileHandle handle);
 
+        void state(SocketStateMap & map, unsigned lod=0);
+        std::string dumpState(unsigned lod=0);
+
     protected:
         ProtocolClientSocketHandle(FileHandle other, bool isChecked);
 
diff --git a/Socket/ProtocolServerSocketHandle.cti b/Socket/ProtocolServerSocketHandle.cti
index 157bc5665..76bb24237 100644
--- a/Socket/ProtocolServerSocketHandle.cti
+++ b/Socket/ProtocolServerSocketHandle.cti
@@ -75,6 +75,24 @@ satcom::lib::ProtocolServerSocketHandle<SocketProtocol>::cast_dynamic(FileHandle
     return cast_static(handle);
 }
 
+template <class SocketProtocol>
+prefix_ void
+satcom::lib::ProtocolServerSocketHandle<SocketProtocol>::state(SocketStateMap & map,
+                                                               unsigned lod)
+{
+    map["handle"] = prettyName(typeid(*this));
+    this->body().state(map,lod);
+}
+
+template <class SocketProtocol>
+prefix_ std::string
+satcom::lib::ProtocolServerSocketHandle<SocketProtocol>::dumpState(unsigned lod)
+{
+    SocketStateMap map;
+    state(map,lod);
+    return detail::dumpState(map);
+}
+
 template <class SocketProtocol>
 prefix_ satcom::lib::ProtocolServerSocketHandle<SocketProtocol>::
 ProtocolServerSocketHandle(FileHandle other, bool isChecked)
diff --git a/Socket/ProtocolServerSocketHandle.hh b/Socket/ProtocolServerSocketHandle.hh
index a14ecf22a..c42ee11ab 100644
--- a/Socket/ProtocolServerSocketHandle.hh
+++ b/Socket/ProtocolServerSocketHandle.hh
@@ -65,6 +65,9 @@ namespace lib {
         static ProtocolServerSocketHandle cast_static(FileHandle handle);
         static ProtocolServerSocketHandle cast_dynamic(FileHandle handle);
 
+        void state(SocketStateMap & map, unsigned lod=0);
+        std::string dumpState(unsigned lod=0);
+
     protected:
         ProtocolServerSocketHandle(FileHandle other, bool isChecked);
 
diff --git a/Socket/ProtocolServerSocketHandle.test.cc b/Socket/ProtocolServerSocketHandle.test.cc
index eb56abc11..22b6cb4a5 100644
--- a/Socket/ProtocolServerSocketHandle.test.cc
+++ b/Socket/ProtocolServerSocketHandle.test.cc
@@ -64,6 +64,15 @@ BOOST_AUTO_UNIT_TEST(protocolServerSocketHandle)
    
         MySocketHandle::ClientSocketHandle client = h.accept();
         BOOST_CHECK_EQUAL( client.fd(), -1 );
+
+        BOOST_CHECK_EQUAL( h.dumpState(),
+                           "handle: satcom::lib::ProtocolServerSocketHandle<(anonymous namespace)::MyProtocol>\n"
+                           "file.handle: -1\n"
+                           "file.refcount: 2\n"
+                           "socket.policy: satcom::lib::SocketPolicy<satcom::lib::test::SomeAddressingPolicy, satcom::lib::test::SomeFramingPolicy, satcom::lib::test::SomeCommunicationPolicy, satcom::lib::test::SomeReadPolicy, satcom::lib::test::SomeWritePolicy, satcom::lib::test::SomeBufferingPolicy>\n"
+                           "socket.protocol: (anonymous namespace)::MyProtocol\n"
+                           "socket.server: true\n" );
+
     }
 
     {
diff --git a/Socket/ServerSocketHandle.cti b/Socket/ServerSocketHandle.cti
index 17caf770e..0887c90b5 100644
--- a/Socket/ServerSocketHandle.cti
+++ b/Socket/ServerSocketHandle.cti
@@ -135,6 +135,21 @@ satcom::lib::ServerSocketHandle<Policy>::cast_dynamic(FileHandle handle)
     return cast_static(handle);
 }
 
+template <class Policy>
+prefix_ void satcom::lib::ServerSocketHandle<Policy>::state(SocketStateMap & map, unsigned lod)
+{
+    map["handle"] = prettyName(typeid(*this));
+    this->body().state(map,lod);
+}
+
+template <class Policy>
+prefix_ std::string satcom::lib::ServerSocketHandle<Policy>::dumpState(unsigned lod)
+{
+    SocketStateMap map;
+    state(map,lod);
+    return detail::dumpState(map);
+}
+
 ///////////////////////////////cti.e///////////////////////////////////////
 #undef prefix_
 
diff --git a/Socket/ServerSocketHandle.hh b/Socket/ServerSocketHandle.hh
index 013d63fa1..42a6bae39 100644
--- a/Socket/ServerSocketHandle.hh
+++ b/Socket/ServerSocketHandle.hh
@@ -98,6 +98,10 @@ namespace lib {
         static ServerSocketHandle cast_static(FileHandle handle);
         static ServerSocketHandle cast_dynamic(FileHandle handle);
 
+        // we need to override both since SocketHandle is *not* polymorphic
+        void state(SocketStateMap & map, unsigned lod=0);
+        std::string dumpState(unsigned lod=0);
+
     protected:
         ServerSocketHandle(FileHandle other, bool isChecked);
         explicit ServerSocketHandle(std::auto_ptr<SocketProtocol> protocol);
diff --git a/Socket/SocketHandle.cc b/Socket/SocketHandle.cc
index 401b8028b..186953aa2 100644
--- a/Socket/SocketHandle.cc
+++ b/Socket/SocketHandle.cc
@@ -28,6 +28,7 @@
 // Custom includes
 #include <sstream>
 #include <sys/socket.h>
+#include "Utils/TypeInfo.hh"
 
 //#include "SocketHandle.mpp"
 #define prefix_
@@ -63,8 +64,11 @@ prefix_ bool satcom::lib::SocketBody::v_eof()
 
 prefix_ void satcom::lib::SocketBody::state(SocketStateMap & map, unsigned lod)
 {
-    map["handle.server"] = isServer() ? "true" : "false";
-    map["handle.protocol"] = typeid(protocol()).name();
+    map["file.handle"] = fd();
+    map["file.refcount"] = refcount();
+    map["socket.server"] = isServer();
+    map["socket.protocol"] = prettyName(typeid(protocol()));
+    map["socket.policy"] = prettyName(typeid(protocol().policy()));
     protocol().state(map,lod);
 }
 
diff --git a/Socket/SocketHandle.cci b/Socket/SocketHandle.cci
index dc16cab37..f036df15e 100644
--- a/Socket/SocketHandle.cci
+++ b/Socket/SocketHandle.cci
@@ -58,6 +58,16 @@ prefix_ bool satcom::lib::SocketBody::isServer()
     return isServer_;
 }
 
+///////////////////////////////////////////////////////////////////////////
+// satcom::lib::detail::ConvertibleString
+
+prefix_ satcom::lib::detail::ConvertibleString::ConvertibleString()
+{}
+
+prefix_ satcom::lib::detail::ConvertibleString::ConvertibleString(bool v)
+    : std::string(v ? "true" : "false")
+{}
+
 ///////////////////////////////cci.e///////////////////////////////////////
 #undef prefix_
 
diff --git a/Socket/SocketHandle.ct b/Socket/SocketHandle.ct
new file mode 100644
index 000000000..9a90bd8f8
--- /dev/null
+++ b/Socket/SocketHandle.ct
@@ -0,0 +1,50 @@
+// $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 "SocketHandle.ih"
+
+// Custom includes
+#include <boost/lexical_cast.hpp>
+
+#define prefix_
+///////////////////////////////ct.p////////////////////////////////////////
+
+template <class T>
+prefix_ satcom::lib::detail::ConvertibleString &
+satcom::lib::detail::ConvertibleString::operator+=(ConvertibleString const & other)
+{
+    if (!empty())
+        this->std::string::operator+=(", ");
+    this->std::string::operator+=(other);
+    return *this;
+}
+
+///////////////////////////////ct.e////////////////////////////////////////
+#undef prefix_
+
+
+// Local Variables:
+// mode: c++
+// c-file-style: "satcom"
+// End:
diff --git a/Socket/SocketHandle.cti b/Socket/SocketHandle.cti
index 6e3f6a8e7..dde5bf78f 100644
--- a/Socket/SocketHandle.cti
+++ b/Socket/SocketHandle.cti
@@ -26,6 +26,8 @@
 
 // Custom includes
 #include <typeinfo>
+#include <boost/lexical_cast.hpp>
+#include "Utils/TypeInfo.hh"
 
 #define prefix_ inline
 ///////////////////////////////cti.p///////////////////////////////////////
@@ -156,6 +158,7 @@ prefix_ bool satcom::lib::check_socket_cast(Source handle)
 template <class SocketPolicy>
 prefix_ void satcom::lib::SocketHandle<SocketPolicy>::state(SocketStateMap & map, unsigned lod)
 {
+    map["handle"] = prettyName(typeid(*this));
     body().state(map,lod);
 }
 
@@ -167,6 +170,13 @@ prefix_ std::string satcom::lib::SocketHandle<SocketPolicy>::dumpState(unsigned
     return detail::dumpState(map);
 }
 
+///////////////////////////////////////////////////////////////////////////
+// satcom::lib::detail::ConvertibleString
+
+template <class T>
+prefix_ satcom::lib::detail::ConvertibleString::ConvertibleString(T const & other)
+    : std::string(boost::lexical_cast<std::string>(other))
+{}
 
 ///////////////////////////////cti.e///////////////////////////////////////
 #undef prefix_
diff --git a/Socket/SocketHandle.hh b/Socket/SocketHandle.hh
index 66999db1f..f56e7d486 100644
--- a/Socket/SocketHandle.hh
+++ b/Socket/SocketHandle.hh
@@ -20,6 +20,9 @@
 // Free Software Foundation, Inc.,
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+// TODO: Create a SocketHandleBase class and move some non-Policy
+// dependent code there
+
 #ifndef HH_SocketHandle_
 #define HH_SocketHandle_ 1
 
@@ -76,8 +79,8 @@ namespace lib {
         static SocketHandle cast_static(FileHandle handle);
         static SocketHandle cast_dynamic(FileHandle handle);
 
-        void state(SocketStateMap & map, unsigned lod);
-        std::string dumpState(unsigned lod);
+        void state(SocketStateMap & map, unsigned lod=0);
+        std::string dumpState(unsigned lod=0);
 
     protected:
         explicit SocketHandle(std::auto_ptr<SocketProtocol> protocol, bool isServer);
@@ -108,7 +111,7 @@ namespace lib {
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "SocketHandle.cci"
-//#include "SocketHandle.ct"
+#include "SocketHandle.ct"
 #include "SocketHandle.cti"
 #endif
 
diff --git a/Socket/SocketHandle.ih b/Socket/SocketHandle.ih
index 9cd96a296..f2e2a328d 100644
--- a/Socket/SocketHandle.ih
+++ b/Socket/SocketHandle.ih
@@ -37,6 +37,17 @@ namespace lib {
     class SocketProtocol;
 
     namespace detail {
+
+        class ConvertibleString : public std::string
+        {
+        public:
+            ConvertibleString();
+            ConvertibleString(bool v);
+            template <class T>
+            ConvertibleString(T const & other);
+            template <class T>
+            ConvertibleString & operator+= (ConvertibleString const & other);
+        };
         
         struct StateMapOrdering
             : public std::binary_function<std::string,std::string,bool>
@@ -46,7 +57,7 @@ namespace lib {
         
     }
 
-    typedef std::map< std::string, std::string, detail::StateMapOrdering > SocketStateMap;
+    typedef std::map< std::string, detail::ConvertibleString, detail::StateMapOrdering > SocketStateMap;
 
     namespace detail {
         std::string dumpState(SocketStateMap const & map);
diff --git a/Socket/SocketHandle.test.cc b/Socket/SocketHandle.test.cc
index c0e40cfd5..cc2def83c 100644
--- a/Socket/SocketHandle.test.cc
+++ b/Socket/SocketHandle.test.cc
@@ -86,6 +86,15 @@ BOOST_AUTO_UNIT_TEST(socketHandle)
     BOOST_CHECK_THROW( satcom::lib::dynamic_socket_cast<SomeSocketHandle>(
                            satcom::lib::FileHandle(FDHandle())),
                        std::bad_cast );
+
+    BOOST_CHECK_EQUAL( myh.dumpState(), 
+                       "handle: satcom::lib::SocketHandle<satcom::lib::SocketPolicy<satcom::lib::test::SomeAddressingPolicy, satcom::lib::test::SomeFramingPolicy, satcom::lib::test::SomeCommunicationPolicy, satcom::lib::test::SomeReadPolicy, satcom::lib::test::SomeWritePolicy, satcom::lib::test::SomeBufferingPolicy> >\n"
+                       "file.handle: -1\n"
+                       "file.refcount: 3\n"
+                       "socket.policy: satcom::lib::SocketPolicy<satcom::lib::test::SomeAddressingPolicy, satcom::lib::test::SomeFramingPolicy, satcom::lib::test::SomeCommunicationPolicy, satcom::lib::test::SomeReadPolicy, satcom::lib::test::SomeWritePolicy, satcom::lib::test::SomeBufferingPolicy>\n"
+                       "socket.protocol: satcom::lib::test::SomeProtocol\n"
+                       "socket.server: false\n" );
+
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
-- 
GitLab