fceb6c72ea4701c1a4649827920208b420ce9946
[senf.git] / senf / Utils / Console / Server.ih
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 internal header */
25
26 #ifndef IH_SENF_Scheduler_Console_Server_
27 #define IH_SENF_Scheduler_Console_Server_ 1
28
29 // Custom includes
30 #include <boost/iostreams/concepts.hpp>
31 #include <boost/iostreams/stream.hpp>
32 #include <set>
33 #include <senf/Utils/Logger/SenfLog.hh>
34
35 ///////////////////////////////ih.p////////////////////////////////////////
36
37 namespace senf {
38 namespace console {
39
40     class Server;
41     class Client;
42
43 namespace detail {
44
45     class ServerManager
46     {
47     public:
48         typedef boost::intrusive_ptr<Server> ptr;
49
50     protected:
51
52     private:
53         static void add(ptr server);
54         static void remove(ptr server);
55
56         static ServerManager & instance();
57
58         typedef std::set<ptr> Servers;
59         Servers servers_;
60
61         friend class senf::console::Server;
62     };
63
64     /** \brief Internal: Nonblocking boost::iostreams::sink
65
66         The sink discards data if the output socket would.
67
68         \fixme Don't throw exceptions ... set stream error indicator (if at all)
69      */
70     class NonblockingSocketSink
71         : public boost::iostreams::sink
72     {
73     public:
74         NonblockingSocketSink(Client & client);
75         std::streamsize write(const char * s, std::streamsize n);
76
77         Client & client() const;
78
79     private:
80         Client & client_;
81     };
82
83     typedef boost::iostreams::stream<NonblockingSocketSink> NonblockingSocketOStream;
84
85     typedef senf::ServerSocketHandle<
86         senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy,
87                                 senf::BSDAddressingPolicy>::policy > ServerHandle;
88
89     /** \brief Internal: Generic client interface
90
91         The ClientReader encapsulates the interaction of a single network client with the user: It
92         manages prompt display and reading an interactive command.
93      */
94     class ClientReader
95     {
96     public:
97         SENF_LOG_CLASS_AREA();
98
99         typedef ServerHandle::ClientHandle ClientHandle;
100
101         virtual ~ClientReader() = 0;
102
103         // Called by subclasses to get information from the Client
104
105         Client & client() const;
106         std::string promptString() const;
107         ClientHandle handle() const;
108         std::ostream & stream() const;
109
110         // Called by subclasses to perform actions in the Client
111
112         void stopClient();
113         std::string::size_type handleInput(std::string const & input, bool incremental=false) const;
114
115         // Called by the Client
116
117         void disablePrompt();
118         void enablePrompt();
119         void write(std::string const & data);
120         unsigned width() const;
121
122     protected:
123         ClientReader(Client & client);
124
125     private:
126         virtual void v_disablePrompt() = 0;
127         virtual void v_enablePrompt() = 0;
128         virtual void v_write(std::string const & data) = 0;
129         virtual unsigned v_width() const = 0;
130
131         Client & client_;
132     };
133
134     /** \brief Internal: Primitive ClientReader implementation
135
136         This implementation uses the cooked telnet mode to read lines from the console. It does not
137         support explicit line editing or any other advanced features.
138      */
139     class DumbClientReader
140         : public ClientReader
141     {
142     public:
143         DumbClientReader(Client & client);
144
145     private:
146         virtual void v_disablePrompt();
147         virtual void v_enablePrompt();
148         virtual void v_write(std::string const & data);
149         virtual unsigned v_width() const;
150
151         void clientData(senf::ReadHelper<ClientHandle>::ptr helper);
152         void showPrompt();
153
154         std::string tail_;
155         unsigned promptLen_;
156         bool promptActive_;
157     };
158
159     /** \brief Internal: Primitive ClientReader implementation
160
161         This implementation uses the cooked telnet mode to read lines from the console. It does not
162         support explicit line editing or any other advanced features.
163      */
164     class NoninteractiveClientReader
165         : public ClientReader
166     {
167     public:
168         NoninteractiveClientReader(Client & client);
169
170     private:
171         virtual void v_disablePrompt();
172         virtual void v_enablePrompt();
173         virtual void v_write(std::string const & data);
174         virtual unsigned v_width() const;
175
176         void newData(int event);
177
178         scheduler::FdEvent readevent_;
179         std::string buffer_;
180     };
181
182 }}}
183
184 ///////////////////////////////ih.e////////////////////////////////////////
185 #endif
186
187 \f
188 // Local Variables:
189 // mode: c++
190 // fill-column: 100
191 // comment-column: 40
192 // c-file-style: "senf"
193 // indent-tabs-mode: nil
194 // ispell-local-dictionary: "american"
195 // compile-command: "scons -u test"
196 // End: