libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef FM_LAST_MOOST_SERVICE_REMOTE_SHELL 00029 #define FM_LAST_MOOST_SERVICE_REMOTE_SHELL 00030 00102 #include <iostream> 00103 #include <boost/asio.hpp> 00104 #include <boost/function.hpp> 00105 #include <boost/shared_ptr.hpp> 00106 #include <boost/noncopyable.hpp> 00107 00108 #include "appender.h" 00109 00110 namespace moost { namespace service { 00111 00115 class remote_shell_iface 00116 { 00117 public: 00129 virtual bool handle_command(std::string& _return, const std::string& cmd, const std::string& args) = 0; 00130 00136 virtual std::string get_prompt() const = 0; 00137 00145 virtual std::string show_help() const = 0; 00146 }; 00147 00148 class remote_shell_server_impl; 00149 00153 class remote_shell_server : public boost::noncopyable 00154 { 00155 private: 00156 remote_shell_server_impl *m_impl; 00157 00158 public: 00159 remote_shell_server(boost::shared_ptr<boost::asio::io_service> ios); 00160 ~remote_shell_server(); 00161 void run(remote_shell_iface *); 00162 void stop(const std::string&); 00163 void set_appender_factory(appender_factory_ptr); 00164 void set_default_stdout_state(bool); 00165 void set_default_stderr_state(bool); 00166 void set_listen_port(unsigned short); 00167 void set_pre_shutdown_function(boost::function0<void>&); 00168 void enable_local_shell(bool); 00169 }; 00170 00178 template <class HandlerType> 00179 class remote_shell : private remote_shell_iface 00180 { 00181 private: 00182 HandlerType& m_hdl; 00183 remote_shell_server m_srv; 00184 00185 public: 00186 remote_shell(HandlerType& hdl) 00187 : m_hdl(hdl), m_srv(boost::shared_ptr<boost::asio::io_service>(new boost::asio::io_service)) 00188 { 00189 } 00190 00191 remote_shell(HandlerType& hdl, boost::shared_ptr<boost::asio::io_service> ios) 00192 : m_hdl(hdl), m_srv(ios) 00193 { 00194 } 00195 00196 virtual std::string get_prompt() const 00197 { 00198 return m_hdl.get_prompt(); 00199 } 00200 00201 virtual std::string show_help() const 00202 { 00203 return m_hdl.show_help(); 00204 } 00205 00206 virtual bool handle_command(std::string& _return, const std::string& cmd, const std::string& args) 00207 { 00208 return m_hdl.handle_command(_return, cmd, args); 00209 } 00210 00211 void run() 00212 { 00213 m_srv.run(this); 00214 } 00215 00216 void set_appender_factory(appender_factory_ptr app_factory) 00217 { 00218 m_srv.set_appender_factory(app_factory); 00219 } 00220 00221 void set_default_stdout_state(bool enabled) 00222 { 00223 m_srv.set_default_stdout_state(enabled); 00224 } 00225 00226 void set_default_stderr_state(bool enabled) 00227 { 00228 m_srv.set_default_stderr_state(enabled); 00229 } 00230 00231 void set_listen_port(unsigned short port) 00232 { 00233 m_srv.set_listen_port(port); 00234 } 00235 00236 void set_pre_shutdown_function(boost::function0<void> func) 00237 { 00238 m_srv.set_pre_shutdown_function(func); 00239 } 00240 00241 void enable_local_shell(bool enabled = true) 00242 { 00243 m_srv.enable_local_shell(enabled); 00244 } 00245 00246 void stop(const std::string& msg) 00247 { 00248 m_srv.stop(msg); 00249 } 00250 }; 00251 00252 } } 00253 00254 #endif 00255