X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Console%2FServer.cc;h=13820cffbbc0fd5a4902805cf276b7404ced6840;hb=6e417a227d417cef20d55af94e2474fbe9c280b8;hp=4d6eb2a14b7a11e0f1668fd4437d4d4fa7f01fcf;hpb=9a782796586d1f6708e1baab64f2140c3c7972e8;p=senf.git diff --git a/Console/Server.cc b/Console/Server.cc index 4d6eb2a..13820cf 100644 --- a/Console/Server.cc +++ b/Console/Server.cc @@ -64,26 +64,20 @@ 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(); -} - -prefix_ senf::console::Server & senf::console::Server::instance() -{ - SENF_ASSERT( instancePtr() ); - return *instancePtr(); + return server; } prefix_ boost::scoped_ptr & senf::console::Server::instancePtr() @@ -95,13 +89,14 @@ prefix_ boost::scoped_ptr & senf::console::Server::instan return instance; } -prefix_ void senf::console::Server::start(ServerHandle handle) +prefix_ senf::console::Server & senf::console::Server::start(ServerHandle 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) @@ -118,7 +113,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() )); } @@ -131,72 +126,110 @@ 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) - : out_t(handle), senf::log::IOStreamTarget(out_t::member), - handle_ (handle), name_ (name), promptLen_(0) +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) ); - route< senf::SenfLog, senf::log::NOTICE >(); -} - -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); + ReadHelper::dispatch( handle(), 16384u, ReadUntil("\n"), + senf::membind(&DumbClientReader::clientData, this) ); } -prefix_ void senf::console::Client::clientData(ReadHelper::ptr helper) +prefix_ void +senf::console::detail::DumbClientReader::clientData(senf::ReadHelper::ptr helper) { - promptLen_ = 0; - if (helper->error() || handle_.eof()) { + if (helper->error() || handle().eof()) { // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS stopClient(); return; } + + 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() << prompt << std::flush; + 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(); +} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::Client + +prefix_ senf::console::Client::Client(Server & server, ClientHandle handle, + std::string const & name) + : out_t(handle), senf::log::IOStreamTarget(out_t::member), server_ (server), + handle_ (handle), name_ (name), reader_ (0) +{ + reader_.reset( new detail::DumbClientReader (*this) ); + route< senf::SenfLog, senf::log::NOTICE >(); +} + +prefix_ senf::console::Client::~Client() +{} + +prefix_ void senf::console::Client::stop() +{ + // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS + server_.removeClient(*this); +} + +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_t::member)))) - out_t::member << "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; } catch (std::exception & ex) { - out_t::member << ex.what() << std::endl; + stream() << ex.what() << std::endl; } catch (...) { - out_t::member << "unidentified error (unknown exception thrown)" << std::endl; + stream() << "unidentified error (unknown exception thrown)" << std::endl; } - - showPrompt(); - ReadHelper::dispatch( handle_, 16384u, ReadUntil("\n"), - senf::membind(&Client::clientData, this) ); -} - -prefix_ void senf::console::Client::showPrompt() -{ - std::string path (executor_.cwd().path()); - out_t::member << name_ << ":" << path << "# " << std::flush; - promptLen_ = name_.size() + 1 + path.size() + 1; } prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp, @@ -204,11 +237,9 @@ prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp, std::string const & area, unsigned level, std::string const & message) { - if (promptLen_) - out_t::member << '\r' << std::string(' ', promptLen_) << '\r'; + reader_->disablePrompt(); IOStreamTarget::v_write(timestamp, stream, area, level, message); - if (promptLen_) - showPrompt(); + reader_->enablePrompt(); } ///////////////////////////////cc.e////////////////////////////////////////