removed needless Boost.Iostreams dependency
[senf.git] / senf / Utils / Console / Server.cc
index 48ac147..e39a44f 100644 (file)
     \brief Server non-inline non-template implementation */
 
 #include "Server.hh"
-#include "Server.ih"
+//#include "Server.ih"
 
 // Custom includes
-#include <errno.h>
-#include <iostream>
 #include <boost/algorithm/string/trim.hpp>
-#include <boost/iostreams/device/file_descriptor.hpp>
-#include <boost/iostreams/stream.hpp>
 #include <boost/bind.hpp>
-#include <senf/Utils/senfassert.hh>
 #include <senf/Utils/membind.hh>
 #include <senf/Utils/Logger/SenfLog.hh>
+#include <senf/Version.hh>
 #include "LineEditor.hh"
 #include "ScopedDirectory.hh"
 #include "Sysdir.hh"
+#include "SysInfo.hh"
 #include "ParsedCommand.hh"
 
 //#include "Server.mpp"
 #define prefix_
-///////////////////////////////cc.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
+
+#ifdef SENF_DEBUG
+#   define BUILD_TYPE "development"
+#else
+#   define BUILD_TYPE "production"
+#endif
+
+namespace {
+    senf::console::SysInfo::Proxy addSysInfo (
+            "SENF: The Simple and Extensible Network Framework\n"
+            "  © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research\n"
+            "  Contact: senf-dev@lists.berlios.de\n"
+            "  Version: " SENF_LIB_VERSION " Revision number: " SENF_REVISION "\n"
+            "  Build-type: " BUILD_TYPE ", SenfLog compile time limit: " +
+            senf::str(senf::log::LEVELNAMES[senf::SenfLog::compileLimit::value]), 0);
+}
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::detail::NonBlockingSocketSink
 
 prefix_ std::streamsize senf::console::detail::NonblockingSocketSink::write(const char * s,
@@ -57,13 +70,11 @@ prefix_ std::streamsize senf::console::detail::NonblockingSocketSink::write(cons
             client_.write(data);
         }
     }
-    catch (SystemException & ex) {
-        ;
-    }
+    catch (...) {}
     return n;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::Server
 
 prefix_ senf::console::Server &
@@ -116,15 +127,15 @@ prefix_ void senf::console::Server::removeClient(Client & client)
                 try {
                     log << client.handle().peer();
                 }
-                catch (senf::SystemException ex) {
-                    log << "(unknown)";
+                catch (senf::SystemException & ex) {
+                    log << "(dead socket)";
                 }
             }));
     // THIS DELETES THE CLIENT INSTANCE !!
     clients_.erase(boost::intrusive_ptr<Client>(&client));
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::detail::DumbClientReader
 
 prefix_ senf::console::detail::DumbClientReader::DumbClientReader(Client & client)
@@ -149,7 +160,7 @@ senf::console::detail::DumbClientReader::clientData(senf::ReadHelper<ClientHandl
 
     std::string data (tail_ + helper->data());
     tail_ = helper->tail();
-    boost::trim(data);                  // Gets rid of superfluous  \r or \n characters
+    boost::trim(data); // Gets rid of superfluous  \r or \n characters
     handleInput(data);
 
     showPrompt();
@@ -164,9 +175,9 @@ prefix_ void senf::console::detail::DumbClientReader::showPrompt()
     prompt += " ";
 
     stream() << std::flush;
-    handle().write(prompt);
     promptLen_ = prompt.size();
     promptActive_ = true;
+    v_write(prompt);
 }
 
 prefix_ void senf::console::detail::DumbClientReader::v_disablePrompt()
@@ -185,7 +196,24 @@ prefix_ void senf::console::detail::DumbClientReader::v_enablePrompt()
 
 prefix_ void senf::console::detail::DumbClientReader::v_write(std::string const & data)
 {
-    handle().write(data);
+    try {
+        handle().write(data);
+    }
+    catch (senf::ExceptionMixin & ex) {
+        SENF_LOG(("unexpected failure writing to socket:" << ex.message()));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
+    catch (std::exception & ex) {
+        SENF_LOG(("unexpected failure writing to socket:" << ex.what()));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
+    catch (...) {
+        SENF_LOG(("unexpected failure writing to socket: unknown exception"));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
 }
 
 prefix_ unsigned senf::console::detail::DumbClientReader::v_width()
@@ -194,7 +222,7 @@ prefix_ unsigned senf::console::detail::DumbClientReader::v_width()
     return 80;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::detail::NoninteractiveClientReader
 
 prefix_
@@ -213,7 +241,24 @@ prefix_ void senf::console::detail::NoninteractiveClientReader::v_enablePrompt()
 
 prefix_ void senf::console::detail::NoninteractiveClientReader::v_write(std::string const & data)
 {
-    handle().write(data);
+    try {
+        handle().write(data);
+    }
+    catch (senf::ExceptionMixin & ex) {
+        SENF_LOG(("unexpected failure writing to socket:" << ex.message()));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
+    catch (std::exception & ex) {
+        SENF_LOG(("unexpected failure writing to socket:" << ex.what()));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
+    catch (...) {
+        SENF_LOG(("unexpected failure writing to socket: unknown exception"));
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+    }
 }
 
 prefix_ unsigned senf::console::detail::NoninteractiveClientReader::v_width()
@@ -240,7 +285,7 @@ senf::console::detail::NoninteractiveClientReader::newData(int event)
     stream() << std::flush;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::Client
 
 prefix_ senf::console::Client::Client(Server & server, ClientHandle handle)
@@ -309,7 +354,8 @@ prefix_ std::string::size_type senf::console::Client::handleInput(std::string da
         // handled gracefully by the ClientReader. We cannot call stop() here, since we are called
         // from the client reader callback and that will continue executing after stop() has been
         // called. stop() however will delete *this instance ... BANG ...
-        handle_.facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD);
+        try { handle_.facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
     }
     catch (std::exception & ex) {
         std::string msg (ex.what());
@@ -349,14 +395,12 @@ prefix_ unsigned senf::console::Client::getWidth(std::ostream & os, unsigned def
     return rv < minWidth ? defaultWidth : rv;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::Client::SysBacktrace
 
 prefix_ senf::console::Client::SysBacktrace::SysBacktrace()
 {
-    namespace fty = senf::console::factory;
-
-    sysdir().add("backtrace", fty::Command(&SysBacktrace::backtrace)
+    sysdir().add("backtrace", factory::Command(&SysBacktrace::backtrace)
                  .doc("Display the backtrace of the last error / exception in this console") );
 }
 
@@ -371,7 +415,7 @@ prefix_ void senf::console::Client::SysBacktrace::backtrace(std::ostream & os)
 
 senf::console::Client::SysBacktrace senf::console::Client::SysBacktrace::instance_;
 
-///////////////////////////////cc.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 //#include "Server.mpp"