Utils/Logger: Remove dependency on libboost_datetime
[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         Client & client() const;
60         
61     private:
62         Client & client_;
63     };
64
65     typedef boost::iostreams::stream<NonblockingSocketSink> NonblockingSocketOStream;
66
67     typedef senf::ServerSocketHandle<
68         senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy, 
69                                 senf::UnspecifiedAddressingPolicy>::policy > ServerHandle;
70
71     /** \brief Internal: Generic client interface
72
73         The ClientReader encapsulates the interaction of a single network client with the user: It
74         manages prompt display and reading an interactive command.
75      */
76     class ClientReader
77     {
78     public:
79         typedef ServerHandle::ClientSocketHandle ClientHandle;
80
81         virtual ~ClientReader() = 0;
82
83         // Called by subclasses to get information from the Client
84
85         Client & client() const;
86         std::string promptString() const;
87         ClientHandle handle() const;
88         std::ostream & stream() const;
89
90         // Called by subclasses to perform actions in the Client
91
92         void stopClient();
93         void handleInput(std::string const & input) const;
94
95         // Called by the Client
96
97         void disablePrompt();
98         void enablePrompt();
99         void translate(std::string & data);
100
101     protected:
102         ClientReader(Client & client);
103
104     private:
105         virtual void v_disablePrompt() = 0;
106         virtual void v_enablePrompt() = 0;
107         virtual void v_translate(std::string & data) = 0;
108
109         Client & client_;
110     };
111
112     /** \brief Internal: Primitive ClientReader implementation
113         
114         This implementation uses the cooked telnet mode to read lines from the console. It does not
115         support explicit line editing or any other advanced features.
116      */
117     class DumbClientReader
118         : public ClientReader
119     {
120     public:
121         DumbClientReader(Client & client);
122
123     private:
124         virtual void v_disablePrompt();
125         virtual void v_enablePrompt();
126         virtual void v_translate(std::string & data);
127
128         void clientData(senf::ReadHelper<ClientHandle>::ptr helper);
129         void showPrompt();
130
131         std::string tail_;
132         unsigned promptLen_;
133         bool promptActive_;
134     };
135     
136 }}}
137
138 ///////////////////////////////ih.e////////////////////////////////////////
139 #endif
140
141 \f
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // End: