eb1fac130c6eccb52b2392c1d356c42f979405c7
[senf.git] / Console / Server.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Server non-inline non-template implementation */
25
26 #include "Server.hh"
27 //#include "Server.ih"
28
29 // Custom includes
30 #include <unistd.h>
31 #include <iostream>
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"
39
40 //#include "Server.mpp"
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 prefix_ senf::console::Server &
45 senf::console::Server::start(senf::INet4SocketAddress const & address)
46 {
47     senf::TCPv4ServerSocketHandle handle (address);
48     senf::console::Server::start(handle);
49     SENF_LOG((Server::SENFLogArea)(log::NOTICE)( 
50                  "Console server started at " << address ));
51     return *instance_;
52 }
53
54 prefix_ senf::console::Server &
55 senf::console::Server::start(senf::INet6SocketAddress const & address)
56 {
57     senf::TCPv6ServerSocketHandle handle (address);
58     senf::console::Server::start(handle);
59     SENF_LOG((Server::SENFLogArea)(log::NOTICE)( 
60                  "Console server started at " << address ));
61     return *instance_;
62 }
63
64 ///////////////////////////////////////////////////////////////////////////
65 // senf::console::Server
66
67 boost::scoped_ptr<senf::console::Server> senf::console::Server::instance_;
68
69 prefix_ void senf::console::Server::start(ServerHandle handle)
70 {
71     SENF_ASSERT( ! instance_ );
72     instance_.reset(new Server(handle));
73 }
74
75 prefix_ senf::console::Server::Server(ServerHandle handle)
76     : handle_ (handle)
77 {
78     Scheduler::instance().add( handle_, senf::membind(&Server::newClient, this) );
79 }
80
81 prefix_ senf::console::Server::~Server()
82 {
83     Scheduler::instance().remove(handle_);
84 }
85
86 prefix_ void senf::console::Server::newClient(Scheduler::EventId event)
87 {
88     ServerHandle::ClientSocketHandle client (handle_.accept());
89     boost::intrusive_ptr<Client> p (new Client(client, name_));
90     clients_.insert( p );
91     SENF_LOG(( "Registered new client " << p.get() ));
92 }
93
94 prefix_ void senf::console::Server::removeClient(Client & client)
95 {
96     SENF_LOG(( "Disposing client " << & client ));
97     // THIS DELETES THE CLIENT INSTANCE !!
98     clients_.erase(boost::intrusive_ptr<Client>(&client));
99 }
100
101 ///////////////////////////////////////////////////////////////////////////
102 // senf::console::Client
103
104 prefix_ senf::console::Client::Client(ClientHandle handle, std::string const & name)
105     : out_t(::dup(handle.fd())), senf::log::IOStreamTarget(out_t::member),
106       handle_ (handle), name_ (name), promptLen_(0)
107 {
108     showPrompt();
109     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
110                                         senf::membind(&Client::clientData, this) );
111     route< senf::SenfLog, senf::log::NOTICE >();
112 }
113
114 prefix_ senf::console::Client::~Client()
115 {}
116
117 prefix_ void senf::console::Client::stopClient()
118 {
119     // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
120     Server::instance_->removeClient(*this);
121 }
122
123 prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::ptr helper)
124 {
125     promptLen_ = 0;
126     if (helper->error() || handle_.eof()) {
127         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
128         stopClient();
129         return;
130     }
131
132     std::string data (tail_ + helper->data());
133     tail_ = helper->tail();
134     boost::trim(data); // Gets rid of superfluous  \r or \n characters
135
136     try {
137         if (! parser_.parse(data, boost::bind<void>(boost::ref(executor_), _1, 
138                                                     boost::ref(out_t::member))))
139             out_t::member << "syntax error" << std::endl;
140     }
141     catch (Executor::ExitException &) {
142         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
143         stopClient();
144         return;
145     }
146     catch (std::exception & ex) {
147         out_t::member << ex.what() << std::endl;
148     }
149     catch (...) {
150         out_t::member << "unidentified error (unknown exception thrown)" << std::endl;
151     }
152
153     showPrompt();
154     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
155                                         senf::membind(&Client::clientData, this) );
156 }
157
158 prefix_ void senf::console::Client::showPrompt()
159 {
160     std::string path (executor_.cwd().path());
161     out_t::member << name_ << ":" << path << "# " << std::flush;
162     promptLen_ = name_.size() + 1 + path.size() + 1;
163 }
164
165 prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp,
166                                             std::string const & stream,
167                                             std::string const & area, unsigned level,
168                                             std::string const & message)
169 {
170     if (promptLen_)
171         out_t::member << '\r' << std::string(' ', promptLen_) << '\r';
172     IOStreamTarget::v_write(timestamp, stream, area, level, message);
173     if (promptLen_)
174         showPrompt();
175 }
176
177 ///////////////////////////////cc.e////////////////////////////////////////
178 #undef prefix_
179 //#include "Server.mpp"
180
181 \f
182 // Local Variables:
183 // mode: c++
184 // fill-column: 100
185 // comment-column: 40
186 // c-file-style: "senf"
187 // indent-tabs-mode: nil
188 // ispell-local-dictionary: "american"
189 // compile-command: "scons -u test"
190 // End: