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 */
27 //#include "Server.ih"
32 #include <boost/algorithm/string/trim.hpp>
33 #include <boost/iostreams/device/file_descriptor.hpp>
34 #include <boost/iostreams/stream.hpp>
35 #include "../Utils/senfassert.hh"
36 #include "../Utils/membind.hh"
37 #include "../Utils/Logger/SenfLog.hh"
39 //#include "Server.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
43 prefix_ senf::console::Server &
44 senf::console::Server::start(senf::INet4SocketAddress const & address)
46 senf::TCPv4ServerSocketHandle handle (address);
47 senf::console::Server::start(handle);
48 SENF_LOG((Server::SENFLogArea)(log::NOTICE)(
49 "Console server started at " << address ));
53 prefix_ senf::console::Server &
54 senf::console::Server::start(senf::INet6SocketAddress const & address)
56 senf::TCPv6ServerSocketHandle handle (address);
57 senf::console::Server::start(handle);
58 SENF_LOG((Server::SENFLogArea)(log::NOTICE)(
59 "Console server started at " << address ));
63 ///////////////////////////////////////////////////////////////////////////
64 // senf::console::Server
66 boost::scoped_ptr<senf::console::Server> senf::console::Server::instance_;
68 prefix_ void senf::console::Server::start(ServerHandle handle)
70 SENF_ASSERT( ! instance_ );
71 instance_.reset(new Server(handle));
74 prefix_ senf::console::Server::Server(ServerHandle handle)
77 Scheduler::instance().add( handle_, senf::membind(&Server::newClient, this) );
80 prefix_ senf::console::Server::~Server()
82 Scheduler::instance().remove(handle_);
85 prefix_ void senf::console::Server::newClient(Scheduler::EventId event)
87 ServerHandle::ClientSocketHandle client (handle_.accept());
88 boost::intrusive_ptr<Client> p (new Client(client, name_));
90 SENF_LOG(( "Registered new client " << p.get() ));
93 prefix_ void senf::console::Server::removeClient(Client & client)
95 SENF_LOG(( "Disposing client " << & client ));
96 // THIS DELETES THE CLIENT INSTANCE !!
97 clients_.erase(boost::intrusive_ptr<Client>(&client));
100 ///////////////////////////////////////////////////////////////////////////
101 // senf::console::Client
103 prefix_ senf::console::Client::Client(ClientHandle handle, std::string const & name)
104 : handle_ (handle), name_ (name), out_(::dup(handle.fd()))
106 out_ << name_ << "# " << std::flush;
107 ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
108 senf::membind(&Client::clientData, this) );
111 prefix_ senf::console::Client::~Client()
114 prefix_ void senf::console::Client::stopClient()
116 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
117 Server::instance_->removeClient(*this);
120 prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::ptr helper)
122 if (helper->error() || handle_.eof()) {
123 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
128 # warning fix Client::clientData implementation
129 // Remove the 'dup' needed here so we don't close the same fd twice (see Client constructor)
130 // Make output non-blocking
131 // Don't register a new ReadHelper every round
133 std::string data (tail_ + helper->data());
134 tail_ = helper->tail();
135 boost::trim(data); // Gets rid of superfluous \r or \n characters
137 if (data == "exit") {
138 // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
143 ParseCommandInfo command;
144 if (parser_.parseCommand(data, command))
145 executor_(command, out_);
147 out_ << "syntax error" << std::endl;
149 out_ << name_ << "# " << std::flush;
150 ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
151 senf::membind(&Client::clientData, this) );
154 ///////////////////////////////cc.e////////////////////////////////////////
156 //#include "Server.mpp"
162 // comment-column: 40
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"