Server.hh
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Console_Server_
18 #define HH_SENF_Utils_Console_Server_ 1
19 
20 // Custom includes
21 #include <set>
22 #include <boost/scoped_ptr.hpp>
26 #include <senf/Utils/Logger.hh>
28 #include "ScopedDirectory.hh"
29 #include "Executor.hh"
30 
31 #include "Server.ih"
32 //-/////////////////////////////////////////////////////////////////////////////////////////////////
33 
34 namespace senf {
35 namespace console {
36 
37  class Client;
38 
53  class Server
55  {
57  SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
58  public:
59  //-////////////////////////////////////////////////////////////////////////
60  // Types
61 
62  typedef detail::ServerHandle ServerHandle;
63 
65 
66  //-////////////////////////////////////////////////////////////////////////
67 
68  static Server & start(senf::INet4SocketAddress const & address);
70  static Server & start(senf::INet6SocketAddress const & address);
72 
73  std::string const & name() const;
74 
75  Server & name(std::string const & name);
76 
78  Server & welcomeMessage(std::string const & message);
79 
81  DirectoryNode & root() const;
82 
83  Server & root(DirectoryNode & root);
84 
87  Mode mode() const;
88 
90  Server & mode(Mode mode);
91 
105  void stop();
106 
109  protected:
110 
111  private:
112  Server(ServerHandle handle);
113 
114  static Server & start(ServerHandle handle);
115 
116  void newClient(int event);
117  void removeClient(Client & client);
118  boost::shared_ptr<DirectoryNode> consoleSelf(std::ostream & os);
119  static DirectoryNode & sysConsoleDir();
120 
121  ServerHandle handle_;
122  scheduler::FdEvent event_;
123  DirectoryNode::ptr root_;
124  static ScopedDirectory<> sysConsoleDir_;
125  Mode mode_;
126 
127  typedef std::set< boost::intrusive_ptr<Client> > Clients;
128  Clients clients_;
129  std::string name_;
130  std::string welcomeMsg_;
131 
132  friend class Client;
133  };
134 
143  class Client
144  : public senf::intrusive_refcount,
145  private boost::base_from_member< detail::SocketStreamOStream >,
147  {
148  typedef boost::base_from_member< detail::SocketStreamOStream > out_t;
149 
152 
153  static const unsigned INTERACTIVE_TIMEOUT = 500; // milliseconds;
154 
155  public:
156  typedef Server::ServerHandle::ClientHandle ClientHandle;
157 
158  ~Client();
159 
160  void stop();
161 
163  std::string const & name() const;
164 
166  ClientHandle handle() const;
167  std::ostream & stream();
168 
172  std::string promptString() const;
173  DirectoryNode & root() const;
174  DirectoryNode & cwd() const;
175 
177  Server::Mode mode() const;
178 
179  void write(std::string const & data) const;
181 
184  std::string const & backtrace() const;
185  unsigned width() const;
186 
190  static Client & get(std::ostream & os);
192 
199  static unsigned getWidth(std::ostream & os, unsigned defaultWidth = 0,
200  unsigned minWidth = 0);
202 
209  void setProperty(std::string const & key, std::string const & value);
210  std::string getProperty(std::string const & key, std::string const & defaultValue) const;
211  boost::optional<std::string> getProperty(std::string const & key) const;
212 
213  private:
214  Client(Server & server, ClientHandle handle);
215 
216  void setInteractive();
217  void setNoninteractive();
218 
219  size_t handleInput(std::string input, bool incremental = false);
220  virtual void v_write(senf::log::time_type timestamp, std::string const & stream,
221  std::string const & area, unsigned level,
222  std::string const & message);
223 
224  void dumpProperties(std::ostream & os) const;
225 
226  ScopedDirectory<> dir_;
227  Server & server_;
228  ClientHandle handle_;
229  scheduler::FdEvent readevent_;
230  scheduler::TimerEvent timer_;
231  CommandParser parser_;
232  Executor executor_;
233  std::string name_;
234  boost::scoped_ptr<detail::ClientReader> reader_;
235  Server::Mode mode_;
236  std::string backtrace_;
237  typedef std::map<std::string, std::string> PropertyMap;
238  PropertyMap properties_;
239 
240  friend class Server;
241  friend class detail::ClientReader;
242  friend class detail::SocketStreamSink;
243 
244  class SysBacktrace
245  {
246  SysBacktrace();
247  static void backtrace(std::ostream & os);
248  static SysBacktrace instance_;
249  };
250 
251  };
252 
256  std::ostream & operator<<(std::ostream & os, Client const & client);
257 
261  std::ostream & operator<<(std::ostream & os, Client * client);
262 
263 }}
264 
265 //-/////////////////////////////////////////////////////////////////////////////////////////////////
266 #include "Server.cci"
267 //#include "Server.ct"
268 //#include "Server.cti"
269 #endif
270 
271 
272 // Local Variables:
273 // mode: c++
274 // fill-column: 100
275 // comment-column: 40
276 // c-file-style: "senf"
277 // indent-tabs-mode: nil
278 // ispell-local-dictionary: "american"
279 // compile-command: "scons -u test"
280 // End:
ScopedDirectory public header.
friend class Client
Definition: Server.hh:132
static Server & start(senf::INet4SocketAddress const &address)
Start server on given IPv4 address/port.
Definition: Server.cc:85
Parse commands.
Definition: Parse.hh:602
#define SENF_LOG_CLASS_AREA()
boost::shared_ptr< DirectoryNode > ptr
Definition: Node.hh:414
Config/console tree directory node.
Definition: Node.hh:406
Mode mode() const
Get mode.
Server::ServerHandle::ClientHandle ClientHandle
Definition: Server.hh:156
DirectoryNode member proxy.
Definition: Config.hh:28
std::ostream & operator<<(std::ostream &os, Token const &token)
Definition: Parse.cc:129
void stop()
Stop the server.
Execute config/console commands.
Definition: Executor.hh:48
Server client instance.
Definition: Server.hh:143
#define SENF_LOG_DEFAULT_LEVEL(level)
DirectoryNode & root() const
Get root node.
std::string const & name() const
Get server name.
Executor public header.
Interactive console server.
Definition: Server.hh:53
void backtrace(std::ostream &os, int numEntries)
Server & welcomeMessage(std::string const &message)
Set server welcome message.
Definition: Server.cc:153
detail::ServerHandle ServerHandle
Definition: Server.hh:62