4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Server non-inline non-template implementation */
32 #include <boost/algorithm/string/trim.hpp>
33 #include <boost/iostreams/device/file_descriptor.hpp>
34 #include <boost/iostreams/stream.hpp>
35 #include <boost/bind.hpp>
36 #include "../Utils/senfassert.hh"
37 #include "../Utils/membind.hh"
38 #include "../Utils/Logger/SenfLog.hh"
40 //#include "Server.mpp"
42 ///////////////////////////////cc.p////////////////////////////////////////
44 ///////////////////////////////////////////////////////////////////////////
45 // senf::console::detail::NonBlockingSocketSink
47 prefix_ std::streamsize senf::console::detail::NonblockingSocketSink::write(const char * s,
51 if (handle_.writeable())
52 handle_.write(s, s+n);
54 catch (SystemException & ex) {
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::console::Server
63 prefix_ senf::console::Server &
64 senf::console::Server::start(senf::INet4SocketAddress const & address)
66 senf::TCPv4ServerSocketHandle handle (address);
67 Server & server (senf::console::Server::start(handle));
68 SENF_LOG((Server::SENFLogArea)(log::NOTICE)(
69 "Console server started at " << address ));
73 prefix_ senf::console::Server &
74 senf::console::Server::start(senf::INet6SocketAddress const & address)
76 senf::TCPv6ServerSocketHandle handle (address);
77 Server & server (senf::console::Server::start(handle));
78 SENF_LOG((Server::SENFLogArea)(log::NOTICE)(
79 "Console server started at " << address ));
83 prefix_ boost::scoped_ptr<senf::console::Server> & senf::console::Server::instancePtr()
85 // We cannot make 'instance' a global or class-static variable, since it will then be destructed
86 // at an unknown time which may fail if the scheduler or the file-handle pool allocators have
87 // already been destructed.
88 static boost::scoped_ptr<senf::console::Server> instance;
92 prefix_ senf::console::Server & senf::console::Server::start(ServerHandle handle)
94 // Uah .... ensure the scheduler is created before the instance pointer so it get's destructed
96 (void) senf::Scheduler::instance();
97 SENF_ASSERT( ! instancePtr() );
98 instancePtr().reset(new Server(handle));
99 return * instancePtr();
102 prefix_ senf::console::Server::Server(ServerHandle handle)
105 Scheduler::instance().add( handle_, senf::membind(&Server::newClient, this) );
108 prefix_ senf::console::Server::~Server()
110 Scheduler::instance().remove(handle_);
113 prefix_ void senf::console::Server::newClient(Scheduler::EventId event)
115 ServerHandle::ClientSocketHandle client (handle_.accept());
116 boost::intrusive_ptr<Client> p (new Client(*this, client, name_));
117 clients_.insert( p );
118 SENF_LOG(( "Registered new client " << p.get() ));
121 prefix_ void senf::console::Server::removeClient(Client & client)
123 SENF_LOG(( "Disposing client " << & client ));
124 // THIS DELETES THE CLIENT INSTANCE !!
125 clients_.erase(boost::intrusive_ptr<Client>(&client));
128 ///////////////////////////////////////////////////////////////////////////
129 // senf::console::Client
131 prefix_ senf::console::Client::Client(Server & server, ClientHandle handle,
132 std::string const & name)
133 : out_t(handle), senf::log::IOStreamTarget(out_t::member), server_ (server),
134 handle_ (handle), name_ (name), promptLen_(0)
137 ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
138 senf::membind(&Client::clientData, this) );
139 route< senf::SenfLog, senf::log::NOTICE >();
142 prefix_ senf::console::Client::~Client()
145 prefix_ void senf::console::Client::stopClient()
147 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
148 server_.removeClient(*this);
151 prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::ptr helper)
154 if (helper->error() || handle_.eof()) {
155 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
160 std::string data (tail_ + helper->data());
161 tail_ = helper->tail();
162 boost::trim(data); // Gets rid of superfluous \r or \n characters
170 if (! parser_.parse(data, boost::bind<void>( boost::ref(executor_),
171 boost::ref(out_t::member),
173 out_t::member << "syntax error" << std::endl;
175 catch (Executor::ExitException &) {
176 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
180 catch (std::exception & ex) {
181 out_t::member << ex.what() << std::endl;
184 out_t::member << "unidentified error (unknown exception thrown)" << std::endl;
188 ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
189 senf::membind(&Client::clientData, this) );
192 prefix_ void senf::console::Client::showPrompt()
194 std::string path (executor_.cwd().path());
195 out_t::member << name_ << ":" << path << "# " << std::flush;
196 promptLen_ = name_.size() + 1 + path.size() + 1;
199 prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp,
200 std::string const & stream,
201 std::string const & area, unsigned level,
202 std::string const & message)
205 out_t::member << '\r' << std::string(' ', promptLen_) << '\r';
206 IOStreamTarget::v_write(timestamp, stream, area, level, message);
211 ///////////////////////////////cc.e////////////////////////////////////////
213 //#include "Server.mpp"
219 // comment-column: 40
220 // c-file-style: "senf"
221 // indent-tabs-mode: nil
222 // ispell-local-dictionary: "american"
223 // compile-command: "scons -u test"