libmoost
/home/mhx/git/github/libmoost/include/moost/service/remote_shell.h File Reference
#include <iostream>
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include "appender.h"
Include dependency graph for remote_shell.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  moost::service::remote_shell_iface
 Interface for remote command shell. More...
class  moost::service::remote_shell_server
 Compiler firewall for remote_shell_server_impl. More...
class  moost::service::remote_shell< HandlerType >
 A remote command shell used for interacting with the user. More...

Namespaces

namespace  moost
 

Creates a unique temporary directory; removed on scope exit.


namespace  moost::service

Detailed Description

Copyright © 2008-2013 Last.fm Limited

This file is part of libmoost.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Remote command line shell and drop-in replacement for the moost::shell template. The handler needs to implement the handle_command(), get_prompt() and show_help() methods as defined by remote_shell_iface, but there's no need for it to inherit from that interface. remote_shell_iface is only used internally and for documentation purposes. You should not use it as a base class for your handler classes.

class my_handler
{
public:
   void start();
   void stop();

   bool handle_command(string& rv, const string& cmd, const string& args)
   {
      // handle command with args, rv is a container for a message to send back to the user
      return false;   // return true if command was handled, false otherwise
   }

   std::string get_prompt() const
   {
      return "my_handler> ";
   }

   std::string show_help() const
   {
      return "This is my_handler's help\n";
   }
};

int main(void)
{
   my_handler hdl;
   moost::service::remote_shell<my_handler> shell(hdl);

   // use the following to integrate the remote shell with log4cxx
   shell.set_appender_factory(moost::service::appender_factory_ptr(
      new moost::service::log4cxx_appender_factory(default_log_level)
   ));

   // set the port the shell is listening on for remote connections
   shell.set_listen_port(42001);

   // disable stderr forwarding by default
   shell.set_default_stderr_state(false);

   hdl.start();
   shell.run();
   hdl.stop();

   return 0;
}

To connect to the remote shell, use something like this:

rlwrap nc myhost 42001

Multiple concurrent connections are explicitly supported. Each connection will have its own session with separate log appenders. A connection can be closed using the exit, quit, or bye commands. This will not leave the run() method. To leave the run() method in order to shut down the application, use the shutdown command.

Definition in file remote_shell.h.