Console: Implement skeleton executor
[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 "../Utils/senfassert.hh"
36 #include "../Utils/membind.hh"
37 #include "../Utils/Logger/SenfLog.hh"
38
39 //#include "Server.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 prefix_ void senf::console::start(senf::INet4SocketAddress const & address)
44 {
45     senf::TCPv4ServerSocketHandle handle (address);
46     handle.protocol().reuseaddr();
47     senf::console::detail::Server::start(handle);
48     SENF_LOG((detail::Server::SENFLogArea)(log::NOTICE)( 
49                  "Console server started at " << address ));
50 }
51
52 prefix_ void senf::console::start(senf::INet6SocketAddress const & address)
53 {
54     senf::TCPv6ServerSocketHandle handle (address);
55     handle.protocol().reuseaddr();
56     senf::console::detail::Server::start(handle);
57     SENF_LOG((detail::Server::SENFLogArea)(log::NOTICE)( 
58                  "Console server started at " << address ));
59 }
60
61 ///////////////////////////////////////////////////////////////////////////
62 // senf::console::detail::Server
63
64 boost::scoped_ptr<senf::console::detail::Server> senf::console::detail::Server::instance_;
65
66 prefix_ void senf::console::detail::Server::start(ServerHandle handle)
67 {
68     SENF_ASSERT( ! instance_ );
69     instance_.reset(new Server(handle));
70 }
71
72 prefix_ senf::console::detail::Server::Server(ServerHandle handle)
73     : handle_ (handle)
74 {
75     Scheduler::instance().add( handle_, senf::membind(&Server::newClient, this) );
76 }
77
78 prefix_ senf::console::detail::Server::~Server()
79 {
80     Scheduler::instance().remove(handle_);
81 }
82
83 prefix_ void senf::console::detail::Server::newClient(Scheduler::EventId event)
84 {
85     ServerHandle::ClientSocketHandle client (handle_.accept());
86     boost::intrusive_ptr<Client> p (new Client(client));
87     clients_.insert( p );
88     SENF_LOG(( "Registered new client " << p.get() ));
89 }
90
91 prefix_ void senf::console::detail::Server::removeClient(Client & client)
92 {
93     SENF_LOG(( "Disposing client " << & client ));
94     // THIS DELETES THE CLIENT INSTANCE !!
95     clients_.erase(boost::intrusive_ptr<Client>(&client));
96 }
97
98 ///////////////////////////////////////////////////////////////////////////
99 // senf::console::detail::Client
100
101 prefix_ senf::console::detail::Client::Client(ClientHandle handle)
102     : handle_ (handle), out_(::dup(handle.fd()))
103 {
104     out_ << "# " << std::flush;
105     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
106                                         senf::membind(&Client::clientData, this) );
107 }
108
109 prefix_ senf::console::detail::Client::~Client()
110 {}
111
112 prefix_ void senf::console::detail::Client::stopClient()
113 {
114     // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
115     Server::instance_->removeClient(*this);
116 }
117
118 prefix_ void senf::console::detail::Client::clientData(ReadHelper<ClientHandle>::ptr helper)
119 {
120     if (helper->error() || handle_.eof()) {
121         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
122         stopClient();
123         return;
124     }
125
126 #   warning fix Client::clientData implementation
127     // Remove the 'dup' needed here so we don't close the same fd twice (see Client constructor)
128     // Make output non-blocking
129     // Don't register a new ReadHelper every round
130
131     std::string data (tail_ + helper->data());
132     tail_ = helper->tail();
133     boost::trim(data); // Gets rid of superfluous  \r or \n characters
134
135     ParseCommandInfo command;
136     if (parser_.parseCommand(data, command))
137         executor_(command, out_);
138     else 
139         out_ << "syntax error" << std::endl;
140
141     out_ << "# " << std::flush;
142     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
143                                         senf::membind(&Client::clientData, this) );
144 }
145
146 ///////////////////////////////cc.e////////////////////////////////////////
147 #undef prefix_
148 //#include "Server.mpp"
149
150 \f
151 // Local Variables:
152 // mode: c++
153 // fill-column: 100
154 // comment-column: 40
155 // c-file-style: "senf"
156 // indent-tabs-mode: nil
157 // ispell-local-dictionary: "american"
158 // compile-command: "scons -u test"
159 // End: