7bca747032fa68d39656411f8868b5d61cfeb4b5
[senf.git] / Console / Server.hh
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 public header */
25
26 #ifndef HH_Server_
27 #define HH_Server_ 1
28
29 // Custom includes
30 #include <set>
31 #include <boost/utility.hpp>
32 #include <boost/scoped_ptr.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include "../Utils/intrusive_refcount.hh"
35 #include "../Socket/Protocols/INet/TCPSocketHandle.hh"
36 #include "../Socket/ServerSocketHandle.hh"
37 #include "../Scheduler/Scheduler.hh"
38 #include "../Scheduler/ReadHelper.hh"
39 #include "Parse.hh"
40 #include "Executor.hh"
41 #include "../Socket/Protocols/INet/INetAddressing.hh"
42 #include "../Utils/Logger.hh"
43
44 //#include "Server.mpp"
45 #include "Server.ih"
46 ///////////////////////////////hh.p////////////////////////////////////////
47
48 namespace senf {
49 namespace console {
50
51     class Client;
52
53     /** \brief Interactive console server
54
55         This class provides an interactive console TCP server.
56
57         \todo Add readline support
58         \todo Add interactivity detection using timeout
59       */
60     class Server
61         : boost::noncopyable
62     {
63         SENF_LOG_CLASS_AREA();
64         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
65     public:
66         ///////////////////////////////////////////////////////////////////////////
67         // Types
68
69         typedef senf::ServerSocketHandle<
70             senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy, 
71                                     senf::UnspecifiedAddressingPolicy>::policy > ServerHandle;
72
73         ~Server();
74
75         static Server & start(senf::INet4SocketAddress const & address);
76                                         ///< Start server on given IPv4 address/port
77         static Server & start(senf::INet6SocketAddress const & address);
78                                         ///< Start server on given IPv6 address/port
79
80         void name(std::string const & name); ///< Set server name
81                                         /**< This information is used in the prompt string. */
82
83     protected:
84
85     private:
86         Server(ServerHandle handle);
87
88         static void start(ServerHandle handle);
89
90         void newClient(Scheduler::EventId event);
91         void removeClient(Client & client);
92         
93         ServerHandle handle_;
94         
95         typedef std::set< boost::intrusive_ptr<Client> > Clients;
96         Clients clients_;
97         std::string name_;
98         
99         static boost::scoped_ptr<Server> instance_;
100         
101         friend class Client;
102     };
103     
104     /** \brief Server client instance
105
106         Whenever a new client connects, a new instance of this class is created. This class shows a
107         command prompt, receives the commands, parses them and then passes (using a CommandParser)
108         and passes the commands to an Executor instance.
109
110         \fixme Fix Client::clientData implementation
111         \fixme Don't register a new ReadHelper every round
112         \fixme Ensure, that output errors (or any errors) in the console don't terminate the
113             application
114      */
115     class Client
116         : public senf::intrusive_refcount, 
117           private boost::base_from_member< detail::NonblockingSocketOStream >,
118           public senf::log::IOStreamTarget
119     {
120         typedef boost::base_from_member< detail::NonblockingSocketOStream > out_t;
121
122         SENF_LOG_CLASS_AREA();
123         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
124     public:
125         typedef Server::ServerHandle::ClientSocketHandle ClientHandle;
126
127         ~Client();
128
129         void stopClient();              ///< Stop the client
130                                         /**< This will close the client socket. */
131
132     protected:
133         
134     private:
135         Client(ClientHandle handle, std::string const & name);
136
137         void clientData(ReadHelper<ClientHandle>::ptr helper);
138         void showPrompt();
139
140         virtual void v_write(boost::posix_time::ptime timestamp, std::string const & stream, 
141                              std::string const & area, unsigned level, 
142                              std::string const & message);
143         
144         ClientHandle handle_;
145         std::string tail_;
146         CommandParser parser_;
147         Executor executor_;
148         std::string name_;
149         unsigned promptLen_;
150         std::string lastCommand_;
151
152         friend class Server;
153     };
154
155 }}
156
157 ///////////////////////////////hh.e////////////////////////////////////////
158 #include "Server.cci"
159 //#include "Server.ct"
160 //#include "Server.cti"
161 #endif
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"
172 // End: