Console: Implement ObjectDirectory proxy
[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
45 //#include "Server.mpp"
46 ///////////////////////////////hh.p////////////////////////////////////////
47
48 namespace senf {
49 namespace console {
50
51     class Client;
52
53     /** \brief
54         ///\fixme Use special non-blocking streambuf
55       */
56     class Server
57         : boost::noncopyable
58     {
59         SENF_LOG_CLASS_AREA();
60         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
61     public:
62         ///////////////////////////////////////////////////////////////////////////
63         // Types
64
65         typedef senf::ServerSocketHandle<
66             senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy, 
67                                     senf::UnspecifiedAddressingPolicy>::policy > ServerHandle;
68
69         ~Server();
70
71         static Server & start(senf::INet4SocketAddress const & address);
72         static Server & start(senf::INet6SocketAddress const & address);
73
74         void name(std::string const & name);
75
76     protected:
77
78     private:
79         Server(ServerHandle handle);
80
81         static void start(ServerHandle handle);
82
83         void newClient(Scheduler::EventId event);
84         void removeClient(Client & client);
85         
86         ServerHandle handle_;
87         
88         typedef std::set< boost::intrusive_ptr<Client> > Clients;
89         Clients clients_;
90         std::string name_;
91         
92         static boost::scoped_ptr<Server> instance_;
93         
94         friend class Client;
95     };
96     
97     /** \brief
98
99         \fixme Fix Client::clientData implementation
100             Remove the 'dup' needed here so we don't close the same fd twice (see Client constructor)
101             Make output non-blocking
102             Don't register a new ReadHelper every round
103      */
104     class Client
105         : public senf::intrusive_refcount
106     {
107         SENF_LOG_CLASS_AREA();
108         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
109     public:
110         typedef Server::ServerHandle::ClientSocketHandle ClientHandle;
111
112         ~Client();
113
114         void stopClient();
115
116     protected:
117         
118     private:
119         Client(ClientHandle handle, std::string const & name);
120
121         void clientData(ReadHelper<ClientHandle>::ptr helper);
122         void showPrompt();
123         
124         ClientHandle handle_;
125         std::string tail_;
126         CommandParser parser_;
127         Executor executor_;
128         std::string name_;
129
130         typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink> fdostream;
131         fdostream out_;
132
133         friend class Server;
134     };
135
136 }}
137
138 ///////////////////////////////hh.e////////////////////////////////////////
139 #include "Server.cci"
140 //#include "Server.ct"
141 //#include "Server.cti"
142 #endif
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // comment-column: 40
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // End: