BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Public Types | Public Member Functions | Protected Member Functions
BeeeOn::Tool Class Referenceabstract

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>

Inheritance diagram for BeeeOn::Tool:
BeeeOn::StoppableLoop BeeeOn::Loggable BeeeOn::CredentialsTool

Public Types

typedef Poco::SharedPtr< ToolPtr
 
- 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
 
StopControlstopControl ()
 
- 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)
 

Detailed Description

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.

Member Function Documentation

string Tool::command ( ) const
protected
Returns
original command line
virtual void BeeeOn::Tool::main ( ConsoleSession session,
const std::vector< std::string > &  args 
)
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.

void Tool::parseAndRun ( )
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.

bool Tool::shouldStop ( ) const
protected
Returns
true if the tool should stop its processing
void Tool::start ( )
overridevirtual

Start the tool in a separate thread. It parses the command and passes the args vector into the main method.

Implements BeeeOn::StoppableLoop.

void Tool::startSession ( const std::vector< std::string > &  args)
protected

Start a console session and execute main. Exceptions thrown from main are not propagated.

void Tool::stop ( )
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.

StopControl & Tool::stopControl ( )
protected
Returns
underlying StopControl instance.

The documentation for this class was generated from the following files: