00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef HH_SENF_Scheduler_Console_Server_
00027 #define HH_SENF_Scheduler_Console_Server_ 1
00028
00029
00030 #include <set>
00031 #include <boost/utility.hpp>
00032 #include <boost/scoped_ptr.hpp>
00033 #include <senf/Scheduler/FdEvent.hh>
00034 #include <senf/Scheduler/TimerEvent.hh>
00035 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
00036 #include <senf/Utils/Logger.hh>
00037 #include <senf/Utils/intrusive_refcount.hh>
00038 #include "Executor.hh"
00039
00040
00041 #include "Server.ih"
00042
00043
00044 namespace senf {
00045 namespace console {
00046
00047 class Client;
00048
00063 class Server
00064 : public senf::intrusive_refcount
00065 {
00066 SENF_LOG_CLASS_AREA();
00067 SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
00068 public:
00069
00070
00071
00072 typedef detail::ServerHandle ServerHandle;
00073
00074 enum Mode { Automatic, Interactive, Noninteractive };
00075
00076
00077
00078 static Server & start(senf::INet4SocketAddress const & address);
00080 static Server & start(senf::INet6SocketAddress const & address);
00082
00083 std::string const & name() const;
00084
00086 Server & name(std::string const & name);
00087
00089 DirectoryNode & root() const;
00090
00091 Server & root(DirectoryNode & root);
00092
00095 Mode mode() const;
00096
00098 Server & mode(Mode mode);
00099
00113 void stop();
00114
00117 protected:
00118
00119 private:
00120 Server(ServerHandle handle);
00121
00122 static Server & start(ServerHandle handle);
00123
00124 void newClient(int event);
00125 void removeClient(Client & client);
00126
00127 ServerHandle handle_;
00128 scheduler::FdEvent event_;
00129 DirectoryNode::ptr root_;
00130 Mode mode_;
00131
00132 typedef std::set< boost::intrusive_ptr<Client> > Clients;
00133 Clients clients_;
00134 std::string name_;
00135
00136 friend class Client;
00137 };
00138
00147 class Client
00148 : public senf::intrusive_refcount,
00149 private boost::base_from_member< detail::NonblockingSocketOStream >,
00150 public senf::log::IOStreamTarget
00151 {
00152 typedef boost::base_from_member< detail::NonblockingSocketOStream > out_t;
00153
00154 SENF_LOG_CLASS_AREA();
00155 SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
00156
00157 static const unsigned INTERACTIVE_TIMEOUT = 500;
00158
00159 public:
00160 typedef Server::ServerHandle::ClientHandle ClientHandle;
00161
00162 ~Client();
00163
00164 void stop();
00165
00167 std::string const & name() const;
00168
00170 ClientHandle handle() const;
00171 std::ostream & stream();
00172
00176 std::string promptString() const;
00177 DirectoryNode & root() const;
00178 DirectoryNode & cwd() const;
00179
00181 Server::Mode mode() const;
00182
00183 void write(std::string const & data) const;
00185
00188 std::string const & backtrace() const;
00189 unsigned width() const;
00190
00194 static Client & get(std::ostream & os);
00196
00203 static unsigned getWidth(std::ostream & os, unsigned defaultWidth = 0,
00204 unsigned minWidth = 0);
00206
00213 protected:
00214
00215 private:
00216 Client(Server & server, ClientHandle handle);
00217
00218 void setInteractive();
00219 void setNoninteractive();
00220
00221 size_t handleInput(std::string input, bool incremental = false);
00222 virtual void v_write(senf::log::time_type timestamp, std::string const & stream,
00223 std::string const & area, unsigned level,
00224 std::string const & message);
00225
00226 Server & server_;
00227 ClientHandle handle_;
00228 scheduler::FdEvent readevent_;
00229 scheduler::TimerEvent timer_;
00230 CommandParser parser_;
00231 Executor executor_;
00232 std::string name_;
00233 boost::scoped_ptr<detail::ClientReader> reader_;
00234 Server::Mode mode_;
00235 std::string backtrace_;
00236
00237 friend class Server;
00238 friend class detail::ClientReader;
00239 friend class detail::NonblockingSocketSink;
00240
00241 class SysBacktrace
00242 {
00243 SysBacktrace();
00244 static void backtrace(std::ostream & os);
00245 static SysBacktrace instance_;
00246 };
00247
00248 };
00249
00253 std::ostream & operator<<(std::ostream & os, Client const & client);
00254
00258 std::ostream & operator<<(std::ostream & os, Client * client);
00259
00260 }}
00261
00262
00263 #include "Server.cci"
00264
00265
00266 #endif
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277