Console: Documentation fixes
[senf.git] / 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_Server_
27 #define IH_Server_ 1
28
29 // Custom includes
30 #include <boost/iostreams/concepts.hpp>
31 #include <boost/iostreams/stream.hpp>
32
33 ///////////////////////////////ih.p////////////////////////////////////////
34
35 namespace senf {
36 namespace console {
37
38     class Server;
39     class Client;
40
41 namespace detail {
42
43     /** \brief Internal: Nonblocking boost::iostreams::sink
44
45         The sink discards data if the output socket would.
46      */
47     class NonblockingSocketSink 
48         : public boost::iostreams::sink
49     {
50     public:
51         typedef ClientSocketHandle< 
52             MakeSocketPolicy<StreamFramingPolicy,
53                              WriteablePolicy,
54                              ConnectedCommunicationPolicy>::policy > Handle;
55
56         NonblockingSocketSink(Client & client);
57         std::streamsize write(const char * s, std::streamsize n);
58         
59     private:
60         Client & client_;
61     };
62
63     typedef boost::iostreams::stream<NonblockingSocketSink> NonblockingSocketOStream;
64
65     typedef senf::ServerSocketHandle<
66         senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy, 
67                                 senf::UnspecifiedAddressingPolicy>::policy > ServerHandle;
68
69     /** \brief Internal: Generic client interface
70
71         The ClientReader encapsulates the interaction of a single network client with the user: It
72         manages prompt display and reading an interactive command.
73      */
74     class ClientReader
75     {
76     public:
77         typedef ServerHandle::ClientSocketHandle ClientHandle;
78
79         virtual ~ClientReader() = 0;
80
81         Client & client() const;
82         std::string promptString() const;
83         ClientHandle handle() const;
84         std::ostream & stream() const;
85         void stopClient();
86         
87         void handleInput(std::string const & input) const;
88
89         void disablePrompt();
90         void enablePrompt();
91         void translate(std::string & data);
92
93     protected:
94         ClientReader(Client & client);
95
96     private:
97         virtual void v_disablePrompt() = 0;
98         virtual void v_enablePrompt() = 0;
99         virtual void v_translate(std::string & data) = 0;
100
101         Client & client_;
102     };
103
104     /** \brief Internal: Primitive ClientReader implementation
105         
106         This implementation uses the cooked telnet mode to read lines from the console. It does not
107         support explicit line editing or any other advanced features.
108      */
109     class DumbClientReader
110         : public ClientReader
111     {
112     public:
113         DumbClientReader(Client & client);
114
115     private:
116         virtual void v_disablePrompt();
117         virtual void v_enablePrompt();
118         virtual void v_translate(std::string & data);
119
120         void clientData(senf::ReadHelper<ClientHandle>::ptr helper);
121         void showPrompt();
122
123         std::string tail_;
124         unsigned promptLen_;
125         bool promptActive_;
126     };
127     
128 }}}
129
130 ///////////////////////////////ih.e////////////////////////////////////////
131 #endif
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // End: