BeeeOn Gateway
v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
|
The purpose of the class Tool is to provide a common set of features for loops that are to be used as command line tools. We can reuse the dependency-injection infrastructure to start a simple tool for some additional operation that is to be done out of the scoped of the main application. More...
#include <Tool.h>
Public Types | |
typedef Poco::SharedPtr< Tool > | Ptr |
Public Types inherited from BeeeOn::StoppableLoop | |
typedef Poco::SharedPtr < StoppableLoop > | Ptr |
Public Member Functions | |
Tool (bool terminate=true, bool repeat=false) | |
void | setTerminate (bool terminate) |
void | setRepeat (bool repeat) |
void | setCommand (const std::string &command) |
void | setConsole (Console::Ptr console) |
void | start () override |
void | stop () override |
Protected Member Functions | |
virtual void | main (ConsoleSession &session, const std::vector< std::string > &args)=0 |
bool | shouldStop () const |
void | parseAndRun () |
void | startSession (const std::vector< std::string > &args) |
std::string | command () const |
StopControl & | stopControl () |
Protected Member Functions inherited from BeeeOn::Loggable | |
void | setupLogger (Poco::Logger *logger=0) const |
Poco::Logger & | logger () const |
Loggable (const ClassInfo &info) | |
Loggable (const std::type_info &info) | |
Additional Inherited Members | |
Static Protected Member Functions inherited from BeeeOn::Loggable | |
static Poco::Logger & | forMethod (const char *name) |
static Poco::Logger & | forClass (const ClassInfo &info) |
static Poco::Logger & | forClass (const std::type_info &info) |
template<typename T > | |
static Poco::Logger & | forInstance (const T *i) |
static void | configureSimple (Poco::Logger &logger, const std::string &level) |
static void | logException (Poco::Logger &logger, const Poco::Message::Priority priority, const Poco::Exception &e, const char *file, size_t line) |
The purpose of the class Tool is to provide a common set of features for loops that are to be used as command line tools. We can reuse the dependency-injection infrastructure to start a simple tool for some additional operation that is to be done out of the scoped of the main application.
Such a tool parses the command line and executes commands based on the command line options. The command line is injected via method setCommand(), usually we use dependency-injection for this purpose.
Any Tool implementation can be executed via the LoopRunner or as a standalone loop. When running in a non-standalone mode, it must be configured to not terminate its process on exit via setTerminate(false). Otherwise, such loop would terminate all other threads on exit.
Each tool has a configurable Console implementation. The default one is the StdConsole. Some Console implementations allows to start the tool multiple times. Thus, opening a session leads to a new execution of the tool's main. Each tool implementation should be stateless at the class level.
|
protected |
|
protectedpure virtual |
The main method of the tool that is executed in the Tool's loop thread with pre-parsed args. The original command line can be obtained by method command() if needed.
All I/O should be done via the given ConsoleSession instance. Exceptions are cougth by the caller of the main method.
Implemented in BeeeOn::CredentialsTool.
|
protected |
Tool's loop thread entry point. It parses command line arguments, creates a new console session and execute the main method. If the main method fails, the session is closed and the tool can be terminated if the m_terminate property is set to true. Otherwise, the method would repeat again waiting for a new session.
|
protected |
|
overridevirtual |
Start the tool in a separate thread. It parses the command and passes the args vector into the main method.
Implements BeeeOn::StoppableLoop.
|
protected |
Start a console session and execute main. Exceptions thrown from main are not propagated.
|
overridevirtual |
Request the tool thread to stop. This can be useful especially when it is doing some long running procedure (I/O). Otherwise, a tool usually exits quickly (unless configured repeatable).
Implements BeeeOn::StoppableLoop.
|
protected |