2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief Server internal header */
17 #ifndef IH_SENF_Scheduler_Console_Server_
18 #define IH_SENF_Scheduler_Console_Server_ 1
21 #include <boost/iostreams/concepts.hpp>
22 #include <boost/iostreams/stream.hpp>
23 #include <senf/Scheduler/ReadHelper.hh>
24 #include <senf/Socket/Protocols/INet/TCPSocketHandle.hh>
25 #include <senf/Utils/Logger/SenfLog.hh>
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 typedef boost::intrusive_ptr<Server> ptr;
45 static void add(ptr server);
46 static void remove(ptr server);
48 static ServerManager & instance();
50 typedef std::set<ptr> Servers;
53 friend class senf::console::Server;
56 /** \brief Internal: Nonblocking boost::iostreams::sink
58 The sink discards data if the output socket would.
60 \fixme Don't throw exceptions ... set stream error indicator (if at all)
62 class SocketStreamSink
63 : public boost::iostreams::sink
66 SocketStreamSink(Client & client);
67 std::streamsize write(const char * s, std::streamsize n);
69 Client & client() const;
75 typedef boost::iostreams::stream<SocketStreamSink> SocketStreamOStream;
77 typedef senf::ServerSocketHandle<
78 senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy,
79 senf::BSDAddressingPolicy>::policy > ServerHandle;
81 /** \brief Internal: Generic client interface
83 The ClientReader encapsulates the interaction of a single network client with the user: It
84 manages prompt display and reading an interactive command.
89 SENF_LOG_CLASS_AREA();
91 typedef ServerHandle::ClientHandle ClientHandle;
93 virtual ~ClientReader() = 0;
95 // Called by subclasses to get information from the Client
97 Client & client() const;
98 std::string promptString() const;
99 ClientHandle handle() const;
100 std::ostream & stream() const;
102 // Called by subclasses to perform actions in the Client
105 std::string::size_type handleInput(std::string const & input, bool incremental=false) const;
107 // Called by the Client
109 void disablePrompt();
111 void write(std::string const & data);
112 unsigned width() const;
115 ClientReader(Client & client);
118 virtual void v_disablePrompt() = 0;
119 virtual void v_enablePrompt() = 0;
120 virtual void v_write(std::string const & data) = 0;
121 virtual unsigned v_width() const = 0;
126 /** \brief Internal: Primitive ClientReader implementation
128 This implementation uses the cooked telnet mode to read lines from the console. It does not
129 support explicit line editing or any other advanced features.
131 class DumbClientReader
132 : public ClientReader
135 DumbClientReader(Client & client);
138 virtual void v_disablePrompt();
139 virtual void v_enablePrompt();
140 virtual void v_write(std::string const & data);
141 virtual unsigned v_width() const;
143 void clientData(senf::ReadHelper<ClientHandle>::ptr helper);
151 /** \brief Internal: Primitive ClientReader implementation
153 This implementation uses the cooked telnet mode to read lines from the console. It does not
154 support explicit line editing or any other advanced features.
156 class NoninteractiveClientReader
157 : public ClientReader
160 NoninteractiveClientReader(Client & client);
162 typedef std::vector<char> SendQueue;
164 void streamBufferMaxSize(SendQueue::size_type size);
165 SendQueue::size_type streamBufferMaxSize() const;
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;
173 void newData(int event);
174 void writeHandler( int event);
176 SendQueue::size_type streamBufferMaxSize_;
177 SendQueue sendQueue_;
178 scheduler::FdEvent readevent_;
179 scheduler::FdEvent writeevent_;
185 //-/////////////////////////////////////////////////////////////////////////////////////////////////
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"