X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Console%2FServer.cc;h=ac9f034c693a644d01ded11c9141dbc4163cb7aa;hb=489be2bbd4d03259a17df14e4962a4524cd5b654;hp=569a7cff32961119f1b90cf3b811cc1edda8f73b;hpb=5e9e6057a4e5c1241ff3f1b75b0f797eb570725d;p=senf.git diff --git a/Console/Server.cc b/Console/Server.cc index 569a7cf..ac9f034 100644 --- a/Console/Server.cc +++ b/Console/Server.cc @@ -24,10 +24,9 @@ \brief Server non-inline non-template implementation */ #include "Server.hh" -//#include "Server.ih" +#include "Server.ih" // Custom includes -#include #include #include #include @@ -36,40 +35,71 @@ #include "../Utils/senfassert.hh" #include "../Utils/membind.hh" #include "../Utils/Logger/SenfLog.hh" +#include "Readline.hh" //#include "Server.mpp" #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////// +// senf::console::detail::NonBlockingSocketSink + +prefix_ std::streamsize senf::console::detail::NonblockingSocketSink::write(const char * s, + std::streamsize n) +{ + try { + if (client_.handle().writeable()) { + std::string data (s, n); + client_.translate(data); + client_.handle().write( data ); + } + } + catch (SystemException & ex) { + ; + } + return n; +} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::Server + prefix_ senf::console::Server & senf::console::Server::start(senf::INet4SocketAddress const & address) { senf::TCPv4ServerSocketHandle handle (address); - senf::console::Server::start(handle); + Server & server (senf::console::Server::start(handle)); SENF_LOG((Server::SENFLogArea)(log::NOTICE)( "Console server started at " << address )); - return *instance_; + return server; } prefix_ senf::console::Server & senf::console::Server::start(senf::INet6SocketAddress const & address) { senf::TCPv6ServerSocketHandle handle (address); - senf::console::Server::start(handle); + Server & server (senf::console::Server::start(handle)); SENF_LOG((Server::SENFLogArea)(log::NOTICE)( "Console server started at " << address )); - return *instance_; + return server; } -/////////////////////////////////////////////////////////////////////////// -// senf::console::Server - -boost::scoped_ptr senf::console::Server::instance_; +prefix_ boost::scoped_ptr & senf::console::Server::instancePtr() +{ + // We cannot make 'instance' a global or class-static variable, since it will then be destructed + // at an unknown time which may fail if the scheduler or the file-handle pool allocators have + // already been destructed. + static boost::scoped_ptr instance; + return instance; +} -prefix_ void senf::console::Server::start(ServerHandle handle) +prefix_ senf::console::Server & senf::console::Server::start(ServerHandle handle) { - SENF_ASSERT( ! instance_ ); - instance_.reset(new Server(handle)); + // Uah .... ensure the scheduler is created before the instance pointer so it get's destructed + // AFTER it. + (void) senf::Scheduler::instance(); + SENF_ASSERT( ! instancePtr() ); + instancePtr().reset(new Server(handle)); + return * instancePtr(); } prefix_ senf::console::Server::Server(ServerHandle handle) @@ -86,7 +116,7 @@ prefix_ senf::console::Server::~Server() prefix_ void senf::console::Server::newClient(Scheduler::EventId event) { ServerHandle::ClientSocketHandle client (handle_.accept()); - boost::intrusive_ptr p (new Client(client, name_)); + boost::intrusive_ptr p (new Client(*this, client, name_)); clients_.insert( p ); SENF_LOG(( "Registered new client " << p.get() )); } @@ -99,62 +129,142 @@ prefix_ void senf::console::Server::removeClient(Client & client) } /////////////////////////////////////////////////////////////////////////// -// senf::console::Client +// senf::console::detail::DumbClientReader -prefix_ senf::console::Client::Client(ClientHandle handle, std::string const & name) - : handle_ (handle), name_ (name), out_(::dup(handle.fd())) +prefix_ senf::console::detail::DumbClientReader::DumbClientReader(Client & client) + : ClientReader(client), promptLen_ (0), promptActive_ (false) { showPrompt(); - ReadHelper::dispatch( handle_, 16384u, ReadUntil("\n"), - senf::membind(&Client::clientData, this) ); + ReadHelper::dispatch( handle(), 16384u, ReadUntil("\n"), + senf::membind(&DumbClientReader::clientData, this) ); } -prefix_ senf::console::Client::~Client() -{} - -prefix_ void senf::console::Client::stopClient() -{ - // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS - Server::instance_->removeClient(*this); -} - -prefix_ void senf::console::Client::clientData(ReadHelper::ptr helper) +prefix_ void +senf::console::detail::DumbClientReader::clientData(senf::ReadHelper::ptr helper) { - if (helper->error() || handle_.eof()) { + if (helper->error() || handle().eof()) { // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS stopClient(); return; } - -# warning fix Client::clientData implementation - // Remove the 'dup' needed here so we don't close the same fd twice (see Client constructor) - // Make output non-blocking - // Don't register a new ReadHelper every round + + promptLen_ = 0; + promptActive_ = false; 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(); + ReadHelper::dispatch( handle(), 16384u, ReadUntil("\n"), + senf::membind(&DumbClientReader::clientData, this) ); + +} + +prefix_ void senf::console::detail::DumbClientReader::showPrompt() +{ + std::string prompt (promptString()); + + stream() << std::flush; + handle().write(prompt); + promptLen_ = prompt.size(); + promptActive_ = true; +} + +prefix_ void senf::console::detail::DumbClientReader::v_disablePrompt() +{ + if (promptActive_ && promptLen_ > 0) { + stream() << '\r' << std::string(' ', promptLen_) << '\r'; + promptLen_ = 0; + } +} + +prefix_ void senf::console::detail::DumbClientReader::v_enablePrompt() +{ + if (promptActive_ && ! promptLen_) + showPrompt(); +} + +prefix_ void senf::console::detail::DumbClientReader::v_translate(std::string & data) +{} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::Client + +prefix_ senf::console::Client::Client(Server & server, ClientHandle handle, + std::string const & name) + : out_t(boost::ref(*this)), senf::log::IOStreamTarget(out_t::member), server_ (server), + handle_ (handle), name_ (name), reader_ (new detail::SafeReadlineClientReader (*this)) +{ + executor_.autocd(true).autocomplete(true); + handle_.facet().nodelay(); + // route< senf::SenfLog, senf::log::NOTICE >(); +} + +prefix_ void senf::console::Client::translate(std::string & data) +{ + reader_->translate(data); +} + +prefix_ void senf::console::Client::handleInput(std::string data) +{ + if (data.empty()) + data = lastCommand_; + else + lastCommand_ = data; try { - if (! parser_.parse(data, boost::bind(boost::ref(executor_), _1, boost::ref(out_)))) - out_ << "syntax error" << std::endl; + if (! parser_.parse(data, boost::bind( boost::ref(executor_), + boost::ref(stream()), + _1 )) ) + stream() << "syntax error" << std::endl; } catch (Executor::ExitException &) { - // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS - stopClient(); + // This generates an EOF condition on the Handle. This EOF condition is expected + // to be handled gracefully by the ClientReader. We cannot call stop() here, since we + // are called from the client reader callback and that will continue executing even if we + // call stop here ... + handle_.facet().shutdown(senf::TCPSocketProtocol::ShutRD); return; - } - - showPrompt(); - ReadHelper::dispatch( handle_, 16384u, ReadUntil("\n"), - senf::membind(&Client::clientData, this) ); + } + catch (std::exception & ex) { + stream() << ex.what() << std::endl; + } + catch (...) { + stream() << "unidentified error (unknown exception thrown)" << std::endl; + } } -prefix_ void senf::console::Client::showPrompt() +prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp, + std::string const & stream, + std::string const & area, unsigned level, + std::string const & message) { - out_ << name_ << ":" << executor_.cwd().path() << "# " << std::flush; + reader_->disablePrompt(); + IOStreamTarget::v_write(timestamp, stream, area, level, message); + out_t::member << std::flush; + reader_->enablePrompt(); } +prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client const & client) +{ + // typedef senf::ClientSocketHandle::policy > v4Socket; + if( senf::check_socket_cast( client.handle())) { + os<( client.handle()).peer(); + } + else if( senf::check_socket_cast( client.handle())) { + os<( client.handle()).peer(); + } + else{ + os<<((void *)&client); + } + return os; +} +prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client * client) +{ + return os<<*client; +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "Server.mpp"