Packets: static assertion for duplicate tlv parser registration
[senf.git] / senf / Utils / Console / Server.cc
index 188ffbc..aef6bc5 100644 (file)
@@ -2,38 +2,38 @@
 //
 // Copyright (C) 2008
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
-//     Stefan Bund <g0dil@berlios.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.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// 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.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// 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.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Stefan Bund <g0dil@berlios.de>
 
 /** \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 "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"
+            "  © 2006-2011 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", 0);
+            "  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
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
+// senf::console::detail::SocketStreamSink
 
-prefix_ std::streamsize senf::console::detail::NonblockingSocketSink::write(const char * s,
+prefix_ std::streamsize senf::console::detail::SocketStreamSink::write(const char * s,
                                                                             std::streamsize n)
 {
+// since handle is now non blocking we done check for writeable
     try {
-        if (client_.handle().writeable()) {
+//        if (client_.handle().writeable()) {
             std::string data (s, n);
             client_.write(data);
-        }
+//        }
     }
     catch (...) {}
     return n;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::Server
 
 prefix_ senf::console::Server &
@@ -123,7 +133,7 @@ prefix_ void senf::console::Server::removeClient(Client & client)
                 try {
                     log << client.handle().peer();
                 }
-                catch (senf::SystemException ex) {
+                catch (senf::SystemException & ex) {
                     log << "(dead socket)";
                 }
             }));
@@ -131,7 +141,7 @@ prefix_ void senf::console::Server::removeClient(Client & client)
     clients_.erase(boost::intrusive_ptr<Client>(&client));
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::detail::DumbClientReader
 
 prefix_ senf::console::detail::DumbClientReader::DumbClientReader(Client & client)
@@ -156,7 +166,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();
@@ -218,15 +228,18 @@ prefix_ unsigned senf::console::detail::DumbClientReader::v_width()
     return 80;
 }
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::detail::NoninteractiveClientReader
 
 prefix_
 senf::console::detail::NoninteractiveClientReader::NoninteractiveClientReader(Client & client)
-    : ClientReader (client),
+    : ClientReader (client), streamBufferMaxSize_( 1024*1024),
       readevent_ ("senf::console::detail::NoninteractiveClientReader",
                   senf::membind(&NoninteractiveClientReader::newData, this),
-                  handle(), senf::scheduler::FdEvent::EV_READ)
+                  handle(), senf::scheduler::FdEvent::EV_READ),
+      writeevent_ ("senf::console::detail::NoninteractiveClientReader",
+                  membind(&NoninteractiveClientReader::writeHandler, this), handle(),
+                  scheduler::FdEvent::EV_WRITE, false)
 {}
 
 prefix_ void senf::console::detail::NoninteractiveClientReader::v_disablePrompt()
@@ -235,26 +248,27 @@ prefix_ void senf::console::detail::NoninteractiveClientReader::v_disablePrompt(
 prefix_ void senf::console::detail::NoninteractiveClientReader::v_enablePrompt()
 {}
 
+prefix_ void senf::console::detail::NoninteractiveClientReader::streamBufferMaxSize(SendQueue::size_type size)
+{
+    streamBufferMaxSize_ = size;
+}
+
+prefix_ senf::console::detail::NoninteractiveClientReader::SendQueue::size_type
+senf::console::detail::NoninteractiveClientReader::streamBufferMaxSize()
+    const
+{
+    return streamBufferMaxSize_;
+}
+
 prefix_ void senf::console::detail::NoninteractiveClientReader::v_write(std::string const & 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 (...) {}
-    }
+    if( sendQueue_.size() > streamBufferMaxSize_)
+        return;
+    sendQueue_.insert( sendQueue_.end(), data.begin(), data.end());
+    writeHandler(scheduler::FdEvent::EV_WRITE);
+    if (! sendQueue_.empty())
+        writeevent_.enable();
+
 }
 
 prefix_ unsigned senf::console::detail::NoninteractiveClientReader::v_width()
@@ -281,7 +295,41 @@ senf::console::detail::NoninteractiveClientReader::newData(int event)
     stream() << std::flush;
 }
 
-///////////////////////////////////////////////////////////////////////////
+prefix_ void
+senf::console::detail::NoninteractiveClientReader::writeHandler(int event)
+{
+    if (event != senf::scheduler::FdEvent::EV_WRITE) {
+        writeevent_.disable();
+        readevent_.disable();
+        try { handle().facet<senf::TCPSocketProtocol>().shutdown(senf::TCPSocketProtocol::ShutRD); }
+        catch (...) {}
+        return;
+    }
+    try {
+        sendQueue_.erase(sendQueue_.begin(),
+                handle().write(boost::make_iterator_range(sendQueue_.begin(), sendQueue_.end())));
+    }
+    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 (...) {}
+    }
+    if (sendQueue_.empty())
+        writeevent_.disable();
+
+}
+
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::console::Client
 
 prefix_ senf::console::Client::Client(Server & server, ClientHandle handle)
@@ -297,7 +345,8 @@ prefix_ senf::console::Client::Client(Server & server, ClientHandle handle)
               false),
       name_ (server.name()), reader_ (), mode_ (server.mode())
 {
-    handle_.facet<senf::TCPSocketProtocol>().nodelay();
+    handle_.facet<senf::TCPSocketProtocol>().nodelay(true);
+    handle_.blocking(false);
     executor_.chroot(root());
     switch (mode_) {
     case Server::Interactive :
@@ -327,7 +376,14 @@ prefix_ void senf::console::Client::setNoninteractive()
     readevent_.disable();
     timer_.disable();
     mode_ = Server::Noninteractive;
-    reader_.reset(new detail::NoninteractiveClientReader(*this));
+    detail::NoninteractiveClientReader * newReader (new detail::NoninteractiveClientReader(*this));
+    reader_.reset( newReader);
+    consoleDir().add( "streamBuffer", senf::console::factory::Command( senf::membind(
+            SENF_MEMFNP( detail::NoninteractiveClientReader::SendQueue::size_type, detail::NoninteractiveClientReader, streamBufferMaxSize, () const ),
+            newReader )));
+    consoleDir().add( "streamBuffer", senf::console::factory::Command( senf::membind(
+            SENF_MEMFNP( void, detail::NoninteractiveClientReader, streamBufferMaxSize, (detail::NoninteractiveClientReader::SendQueue::size_type) ),
+            newReader )));
 }
 
 prefix_ std::string::size_type senf::console::Client::handleInput(std::string data,
@@ -360,7 +416,7 @@ prefix_ std::string::size_type senf::console::Client::handleInput(std::string da
             backtrace_ = msg.substr(0,i);
             msg = msg.substr(i+4);
         } else
-            backtrace_.clear();
+
         stream() << msg << std::endl;
     }
     catch (...) {
@@ -391,14 +447,11 @@ 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") );
 }
 
@@ -413,7 +466,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"