ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
Classes | Public Member Functions | List of all members
daq::AsyncProcess Class Reference

Represents a subprocess as an asynchronous operation. More...

#include <asyncProcess.hpp>

+ Inheritance diagram for daq::AsyncProcess:

Public Member Functions

 AsyncProcess (boost::asio::io_context &ctx, std::vector< std::string > args)
 Constructor. More...
 
virtual ~AsyncProcess () noexcept
 
boost::future< int > Initiate () override
 Starts process and asynchronous operations that read stdout and stderr. More...
 
std::optional< pid_t > GetPid () const noexcept override
 Get PID. More...
 
std::error_code Abort () noexcept override
 Aborts the operation by terminating process which completes the operation. More...
 
std::error_code Signal (int sig) noexcept override
 Send signal to process. More...
 
bool IsRunning () const noexcept override
 
std::vector< std::string > const & GetArguments () const noexcept override
 
- Public Member Functions inherited from daq::AsyncProcessIf
virtual ~AsyncProcessIf ()
 

Signals

boost::signals2::connection ConnectStdout (SigOutStream::slot_type const &slot) override
 Signal type for stdout/stderr signals. More...
 
boost::signals2::connection ConnectStderr (SigOutStream::slot_type const &slot) override
 Signal type for stdout/stderr signals. More...
 
using SigOutStream = boost::signals2::signal< void(pid_t, std::string const &)>
 Signal type for stdout/stderr signals. More...
 

Additional Inherited Members

- Public Types inherited from daq::AsyncProcessIf
using SigOutStream = boost::signals2::signal< void(pid_t, std::string const &)>
 Signal type for stdout/stderr signals. More...
 

Detailed Description

Represents a subprocess as an asynchronous operation.

Once constructed the operation is initiated (only once) with Initiate() which starts the process and returns a boost::future object that will receive exit code when process terminates and all output has been read.

Note
All completion handlers have been executed and no signals will be emitted after future has received the value or exception, it is safe to delete AsyncProcess after this.

Operation can be aborted with Abort() which will terminate process and set future with exceptional result.

boost::process is pretty buggy so be very careful making changes to this. Examples:

Definition at line 124 of file asyncProcess.hpp.

Member Typedef Documentation

◆ SigOutStream

using daq::AsyncProcessIf::SigOutStream = boost::signals2::signal<void(pid_t, std::string const&)>

Signal type for stdout/stderr signals.

Definition at line 82 of file asyncProcess.hpp.

Constructor & Destructor Documentation

◆ AsyncProcess()

daq::AsyncProcess::AsyncProcess ( boost::asio::io_context &  ctx,
std::vector< std::string >  args 
)
explicit

Constructor.

Note
Does not start the process or any other asynchronous operations. This is done in AsyncProcess::Initiate().
Parameters
ctxio_context instance to use.
argsCommand line arguments. First argument specify the file to be executed.

Definition at line 19 of file asyncProcess.cpp.

◆ ~AsyncProcess()

daq::AsyncProcess::~AsyncProcess ( )
virtualnoexcept

Definition at line 27 of file asyncProcess.cpp.

Member Function Documentation

◆ Abort()

std::error_code daq::AsyncProcess::Abort ( )
overridevirtualnoexcept

Aborts the operation by terminating process which completes the operation.

If process is not running this will not do anything.

Returns
Error code of operation. If process is not running, because it has not been started for with AsyncProcess::Initiate() for example, it returns std::errc::no_such_process.

Implements daq::AsyncProcessIf.

Definition at line 62 of file asyncProcess.cpp.

◆ ConnectStderr()

boost::signals2::connection daq::AsyncProcess::ConnectStderr ( SigOutStream::slot_type const &  slot)
inlineoverridevirtual

Signal type for stdout/stderr signals.

Implements daq::AsyncProcessIf.

Definition at line 188 of file asyncProcess.hpp.

◆ ConnectStdout()

boost::signals2::connection daq::AsyncProcess::ConnectStdout ( SigOutStream::slot_type const &  slot)
inlineoverridevirtual

Signal type for stdout/stderr signals.

Implements daq::AsyncProcessIf.

Definition at line 185 of file asyncProcess.hpp.

◆ GetArguments()

std::vector<std::string> const& daq::AsyncProcess::GetArguments ( ) const
inlineoverridevirtualnoexcept
Returns
arguments used when launching process.

Implements daq::AsyncProcessIf.

Definition at line 176 of file asyncProcess.hpp.

◆ GetPid()

std::optional< pid_t > daq::AsyncProcess::GetPid ( ) const
overridevirtualnoexcept

Get PID.

Returns
pid of process if process is running, otherwise nullopt.

Implements daq::AsyncProcessIf.

Definition at line 72 of file asyncProcess.cpp.

◆ Initiate()

boost::future< int > daq::AsyncProcess::Initiate ( )
overridevirtual

Starts process and asynchronous operations that read stdout and stderr.

This can only be called once.

Note
Caller is responsible for keeping AsyncProcess alive until result is set on future.
Returns
Future that will receive process exit code after process has terminated and all output has been read.

Implements daq::AsyncProcessIf.

Reimplemented in daq::RsyncAsyncProcess.

Definition at line 31 of file asyncProcess.cpp.

◆ IsRunning()

bool daq::AsyncProcess::IsRunning ( ) const
overridevirtualnoexcept
Returns
true if process is running.
false otherwise.

Implements daq::AsyncProcessIf.

Definition at line 89 of file asyncProcess.cpp.

◆ Signal()

std::error_code daq::AsyncProcess::Signal ( int  sig)
overridevirtualnoexcept

Send signal to process.

Unlike Abort() this can be used to gracefully terminate application by sending e.g. SIGTERM.

Parameters
sigSignal to send.
Returns
Error code of operation. If process is not running, because it has not been started for with AsyncProcess::Initiate() for example, it returns std::errc::no_such_process.

Implements daq::AsyncProcessIf.

Definition at line 79 of file asyncProcess.cpp.


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