QtService  1.1.0
A platform independent library to easily create system services and use some of their features
Public Types | Public Member Functions | Protected Slots | Protected Member Functions | List of all members
QtService::ServiceBackend Class Referenceabstract

The interface that needs to be implemented to provide the backend for the service engine. More...

#include <servicebackend.h>

+ Inheritance diagram for QtService::ServiceBackend:

Public Types

enum  ServiceCommand {
  StartCommand, StopCommand, ReloadCommand, PauseCommand,
  ResumeCommand
}
 The standard service commands that the library can handle. More...
 

Public Member Functions

 ServiceBackend (Service *service)
 Constructor with the service instance the backend was created for.
 
virtual int runService (int &argc, char **argv, int flags)=0
 Is called as the services main function by the library from Service::exec. More...
 
virtual void quitService ()=0
 Is called by Service::quit to stop the service programatically. More...
 
virtual void reloadService ()=0
 Is called by Service::reload to reload the service programatically. More...
 
virtual QList< int > getActivatedSockets (const QByteArray &name)
 Is called by Service::getSockets and Service::getSocket to get the activated sockets. More...
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Protected Slots

virtual void signalTriggered (int signal)
 Is called by the library if a unix signal or windows console signal was triggered. More...
 
void processServiceCommand (QtService::ServiceBackend::ServiceCommand code)
 Calls the Service standard methods for the given command code. More...
 
QVariant processServiceCallbackImpl (const QByteArray &kind, const QVariantList &args={})
 Calls a special command as service callback synchronously. More...
 

Protected Member Functions

QtService::Serviceservice () const
 The Service instance this backend was created with.
 
template<typename TRet , typename... TArgs>
TRet processServiceCallback (const QByteArray &kind, TArgs... args)
 Calls a special command as service callback synchronously. More...
 
template<typename... TArgs>
void processServiceCallback (const QByteArray &kind, TArgs... args)
 Calls a special command as service callback synchronously. More...
 
bool registerForSignal (int signal)
 Register for a unix/windows signal your backend wants to handle. More...
 
bool unregisterFromSignal (int signal)
 Unregister from a unix/windows signal your backend doesn't want to handle anymore. More...
 
bool preStartService ()
 Calls the Service::preStart method synchronously. More...
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The interface that needs to be implemented to provide the backend for the service engine.

This class is only needed when implementing your own service backend. It's what the Service uses internally to properly run and interact with the service systems

See also
Service, ServicePlugin

Definition at line 15 of file servicebackend.h.

Member Enumeration Documentation

◆ ServiceCommand

The standard service commands that the library can handle.

Enumerator
StartCommand 

Service was started. Will lead to Service::onStart beeing called.

StopCommand 

Service should stop. Will lead to Service::onStop beeing called.

ReloadCommand 

Service should reload. Will lead to Service::onReload beeing called.

PauseCommand 

Service should pause. Will lead to Service::onPause beeing called.

ResumeCommand 

Service was resumed. Will lead to Service::onResume beeing called.

Definition at line 21 of file servicebackend.h.

Member Function Documentation

◆ getActivatedSockets()

QtService::ServiceBackend::getActivatedSockets ( const QByteArray name)
virtual

Is called by Service::getSockets and Service::getSocket to get the activated sockets.

Parameters
nameThe name of the socket(s) to be retrieved, or a null name for the default socket.
Returns
A list with all sockets found for that name, or an empty list if non were found

If your service backends supports socket activation, implement this method to make the available. It is either called with a name passed to Service::getSockets, or with a null bytearray if the default socket (Service::getSocket) was requested. Sockets returned from this method should be ready to use, aka in the listening state (this is typically the case for bound sockets)

See also
Service::getSockets, Service::getSocket, QByteArray::isNull

◆ preStartService()

QtService::ServiceBackend::preStartService ( )
protected

Calls the Service::preStart method synchronously.

Returns
true if startup can be continued, false to cancle early

You should call this method right after creating the QCoreApplication object and after setting up logging. It's a wrapper that calls Service::preStart and returns it's value. If false is returned the service cannot start properly and you should exit as soon as possible.

Note
If you need to complete the startup and enter the running state, do so. But if false was returned instead of calling the start command simply quit the service without ever starting. If you implement it that way, make shure neither the start nor the stop command are ever triggered
See also
Service::preStart

◆ processServiceCallback() [1/2]

template<typename TRet , typename... TArgs>
TRet QtService::ServiceBackend::processServiceCallback ( const QByteArray kind,
TArgs...  args 
)
protected

Calls a special command as service callback synchronously.

Template Parameters
TRetThe return type
TArgsGeneric arguments types
Parameters
kindThe kind of callback to be called
argsThe arguments of the callback
Returns
The return of the callback

Wrapper that calls Service::onCallback and returns it's value. This method call is, unlike other commands, synchronous.

See also
ServiceBackend::processServiceCallback, Service::onCallback

Definition at line 91 of file servicebackend.h.

◆ processServiceCallback() [2/2]

template<typename... TArgs>
void QtService::ServiceBackend::processServiceCallback ( const QByteArray kind,
TArgs...  args 
)
protected

Calls a special command as service callback synchronously.

Template Parameters
TArgsGeneric arguments types
Parameters
kindThe kind of callback to be called
argsThe arguments of the callback
Returns
The return of the callback

Wrapper that calls Service::onCallback and returns it's value. This method call is, unlike other commands, synchronous.

See also
ServiceBackend::processServiceCallback, Service::onCallback

Definition at line 97 of file servicebackend.h.

◆ processServiceCallbackImpl

QtService::ServiceBackend::processServiceCallbackImpl ( const QByteArray kind,
const QVariantList &  args = {} 
)
protectedslot

Calls a special command as service callback synchronously.

Parameters
kindThe kind of callback to be called
argsThe arguments of the callback
Returns
The return of the callback

Wrapper that calls Service::onCallback and returns it's value. This method call is, unlike other commands, synchronous.

See also
ServiceBackend::processServiceCallback, Service::onCallback

◆ processServiceCommand

QtService::ServiceBackend::processServiceCommand ( QtService::ServiceBackend::ServiceCommand  code)
protectedslot

Calls the Service standard methods for the given command code.

Parameters
codeThe command code to be executed

This method calls the correspondig onCommand method of the Service instance. This method takes care of both sync and async calls. For usage, you should always assume asynchronous runs. In other words, do not complete the operation after this method returns. Instead connect to the coresponding done signal and continue from there.

Note
Connections to those signals should be done before calling this method, as they might be emitted before the method returns! Use queued connections if that would conflict with your implementation

The methods called and signals to connect to for each command are:

Command Method Signal
ServiceBackend::StartCommand Service::onStart Service::started
ServiceBackend::StopCommand Service::onStop Service::stopped
ServiceBackend::ReloadCommand Service::onReload Service::reloaded
ServiceBackend::PauseCommand Service::onPause Service::paused
ServiceBackend::ResumeCommand Service::onResume Service::resumed
See also
ServiceBackend::ServiceCommand, ServiceBackend::service

◆ quitService()

QtService::ServiceBackend::quitService ( )
pure virtual

Is called by Service::quit to stop the service programatically.

Your implementation should perform a graceful service exit. This means a call to this method should behave the same as stopping the service from the service manager.

See also
Service::quit, Service::onStop

◆ registerForSignal()

QtService::ServiceBackend::registerForSignal ( int  signal)
protected

Register for a unix/windows signal your backend wants to handle.

Parameters
signalThe signal to register for
Returns
true if the signal was (or already has been) registered

You need to call this method to register a signal you want to handle. After registering it, it will be delivered via ServiceBackend::signalTriggered as soon as it is triggered.

See also
ServiceBackend::signalTriggered, ServiceBackend::unregisterFromSignal

◆ reloadService()

QtService::ServiceBackend::reloadService ( )
pure virtual

Is called by Service::reload to reload the service programatically.

Your implementation should perform a service reload. This means a call to this method should behave the same as reloading the service from the service manager. If reloading is not supported by your backend, simply call processServiceCommand(ReloadCommand); to properly handle the reload with doing anything service-manager related.

See also
Service::reload, Service::onReload

◆ runService()

QtService::ServiceBackend::runService ( int &  argc,
char **  argv,
int  flags 
)
pure virtual

Is called as the services main function by the library from Service::exec.

Parameters
argcThe argc from Service, i.e. from the main
argvThe argv from Service, i.e. from the main
flagsThe flags from Service, i.e. from the main
Returns
The return value from QCoreApplication::exec

This method should perform all the required startup stuff for the service, prepare logging etc. What must do is to create a QCoreApplication (or another application) and eventually run QCoreApplication::exec - as this method should also block until execution finished. You should return the exit code form the main if possible.

Returning from this method early should typically be done only in case of an error and an error exit code returned.

Internally, you must use ServiceBackend::processServiceCommand (and ServiceBackend::processServiceCallback) at the appropriate points in your implementation to call the corresponding service methods.

See also
Service::exec, Service::Service

◆ signalTriggered

QtService::ServiceBackend::signalTriggered ( int  signal)
protectedvirtualslot

Is called by the library if a unix signal or windows console signal was triggered.

Parameters
signalThe signal that was triggered

If you need to handle signals, this is the easiest way. Please note that this method is called asynchronously. So if you need signal handlers that are synchronous, you must implement them yourself. The signal values are the standard defined values of signals for all platforms, i.e. the SIGINT signal on unix is the same as that define from the signal.h header.

In order to be able to catch signals with this method they must be registered first. Use ServiceBackend::registerForSignal to do so.

See also
ServiceBackend::registerForSignal, ServiceBackend::unregisterFromSignal

◆ unregisterFromSignal()

QtService::ServiceBackend::unregisterFromSignal ( int  signal)
protected

Unregister from a unix/windows signal your backend doesn't want to handle anymore.

Parameters
signalThe signal to unregister from
Returns
true if the signal was (or already has been) unregistered

After unregistering, the "default signal handler" will be used again to handle such a signal. In other words the state before the registration is recovered.

See also
ServiceBackend::signalTriggered, ServiceBackend::registerForSignal

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