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