Console: Documentation fixes
[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         \idea To support blocking commands, we could give the Client 'suspend()' and 'resume()'
60             members. suspend() would probably throw some kind of exception to transfer control back
61             to the Client instance. on resume(), the command would be called again, maybe setting
62             some flag or something. Example for use: Host name resolution: Here we can just built
63             our own little host-name cache. When the name is not found, we ask the resolver to
64             resolve it and call 'resume' when the name is found. Since it is in the cache now, the
65             command will now complete.
66         
67         \implementation We do \e not provide an \c instance() member so we can easily later extend
68             the server to allow registering more than one instance, e.g. with each instance on a
69             differently firewalled port and with different security restrictions.
70       */
71     class Server
72         : boost::noncopyable
73     {
74         SENF_LOG_CLASS_AREA();
75         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
76     public:
77         ///////////////////////////////////////////////////////////////////////////
78         // Types
79         
80         typedef detail::ServerHandle ServerHandle;
81
82         ~Server();
83
84         static Server & start(senf::INet4SocketAddress const & address);
85                                         ///< Start server on given IPv4 address/port
86         static Server & start(senf::INet6SocketAddress const & address);
87                                         ///< Start server on given IPv6 address/port
88         void name(std::string const & name); ///< Set server name
89                                         /**< This information is used in the prompt string. */
90
91     protected:
92
93     private:
94         Server(ServerHandle handle);
95
96         static Server & start(ServerHandle handle);
97         static boost::scoped_ptr<Server> & instancePtr();
98
99         void newClient(Scheduler::EventId event);
100         void removeClient(Client & client);
101         
102         ServerHandle handle_;
103         
104         typedef std::set< boost::intrusive_ptr<Client> > Clients;
105         Clients clients_;
106         std::string name_;
107         
108         friend class Client;
109     };
110     
111     /** \brief Server client instance
112
113         Whenever a new client connects, a new instance of this class is created. This class shows a
114         command prompt, receives the commands, parses them and then passes (using a CommandParser)
115         and passes the commands to an Executor instance.
116      */
117     class Client
118         : public senf::intrusive_refcount, 
119           private boost::base_from_member< detail::NonblockingSocketOStream >,
120           public senf::log::IOStreamTarget
121     {
122         typedef boost::base_from_member< detail::NonblockingSocketOStream > out_t;
123
124         SENF_LOG_CLASS_AREA();
125         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
126
127     public:
128         typedef Server::ServerHandle::ClientSocketHandle ClientHandle;
129
130         ~Client();
131
132         void stop();                    ///< Stop the client
133                                         /**< This will close the client socket. */
134
135         std::string const & name() const;
136         ClientHandle handle() const;
137         std::ostream & stream();
138         std::string promptString() const;
139
140     protected:
141         
142     private:
143         Client(Server & server, ClientHandle handle, std::string const & name);
144
145         void translate(std::string & data);
146         void handleInput(std::string input);
147         virtual void v_write(boost::posix_time::ptime timestamp, std::string const & stream, 
148                              std::string const & area, unsigned level, 
149                              std::string const & message);
150         
151         Server & server_;
152         ClientHandle handle_;
153         CommandParser parser_;
154         Executor executor_;
155         std::string name_;
156         std::string lastCommand_;
157         boost::scoped_ptr<detail::ClientReader> reader_;
158
159         friend class Server;
160         friend class detail::ClientReader;
161         friend class detail::NonblockingSocketSink;
162     };
163
164 }}
165
166 ///////////////////////////////hh.e////////////////////////////////////////
167 #include "Server.cci"
168 //#include "Server.ct"
169 //#include "Server.cti"
170 #endif
171
172 \f
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // comment-column: 40
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // End: