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