QtService< Application > Class Template Reference

The QtService is a convenient template class that allows you to create a service for a particular application type. More...

Inheritance diagram for QtService< Application >:
Collaboration diagram for QtService< Application >:

Public Member Functions

 QtService (int argc, char **argv, const QString &name)
 ~QtService ()
 Public Member Functions inherited from QtServiceBase
 QtServiceBase (int argc, char **argv, const QString &name)
virtual ~QtServiceBase ()
QString serviceName () const
QString serviceDescription () const
void setServiceDescription (const QString &description)
QtServiceController::StartupType startupType () const
void setStartupType (QtServiceController::StartupType startupType)
ServiceFlags serviceFlags () const
void setServiceFlags (ServiceFlags flags)
int exec ()
void logMessage (const QString &message, MessageType type=Success, int id=0, uint category=0, const QByteArray &data=QByteArray())

Protected Member Functions

Application * application () const
virtual void createApplication (int &argc, char **argv)
virtual int executeApplication ()
 Protected Member Functions inherited from QtServiceBase
virtual void start ()=0
virtual void stop ()
virtual void pause ()
virtual void resume ()
virtual void processCommand (int code)

Additional Inherited Members

 Public Types inherited from QtServiceBase
enum  MessageType { Success = 0, Error, Warning, Information }
enum  ServiceFlag { Default = 0x00, CanBeSuspended = 0x01, CannotBeStopped = 0x02, NeedsStopOnShutdown = 0x04 }
 Static Public Member Functions inherited from QtServiceBase
static QtServiceBaseinstance ()

Detailed Description

template<typename Application>
class QtService< Application >

The QtService is a convenient template class that allows you to create a service for a particular application type.

A Windows service or Unix daemon (a "service"), is a program that runs "in the background" independently of whether a user is logged in or not. A service is often set up to start when the machine boots up, and will typically run continuously as long as the machine is on.

Services are usually non-interactive console applications. User interaction, if required, is usually implemented in a separate, normal GUI application that communicates with the service through an IPC channel. For simple communication, QtServiceController::sendCommand() and QtService::processCommand() may be used, possibly in combination with a shared settings file. For more complex, interactive communication, a custom IPC channel should be used, e.g. based on Qt's networking classes. (In certain circumstances, a service may provide a GUI itself, ref. the "interactive" example documentation).

{Note:} On Unix systems, this class relies on facilities provided by the QtNetwork module, provided as part of the {Qt Open Source Edition} and certain {Qt Commercial Editions}.

The QtService class functionality is inherited from QtServiceBase, but in addition the QtService class binds an instance of QtServiceBase with an application type.

Typically, you will create a service by subclassing the QtService template class. For example:

class MyService : public QtService<QApplication>
MyService(int argc, char **argv);
void start();
void stop();
void pause();
void resume();
void processCommand(int code);

The application type can be QCoreApplication for services without GUI, QApplication for services with GUI or you can use your own custom application type.

You must reimplement the QtServiceBase::start() function to perform the service's work. Usually you create some main object on the heap which is the heart of your service.

In addition, you might want to reimplement the QtServiceBase::pause(), QtServiceBase::processCommand(), QtServiceBase::resume() and QtServiceBase::stop() to intervene the service's process on controller requests. You can control any given service using an instance of the QtServiceController class which also allows you to control services from separate applications. The mentioned functions are all virtual and won't do anything unless they are reimplemented.

Your custom service is typically instantiated in the application's main function. Then the main function will call your service's exec() function, and return the result of that call. For example:

int main(int argc, char **argv)
MyService service(argc, argv);
return service.exec();

When the exec() function is called, it will parse the {serviceSpecificArguments} {service specific arguments} passed in argv, perform the required actions, and exit.

If none of the arguments is recognized as service specific, exec() will first call the createApplication() function, then executeApplication() and finally the start() function. In the end, exec() returns while the service continues in its own process waiting for commands from the service controller.

See also
QtServiceBase, QtServiceController

Definition at line 163 of file qtservice.h.

Constructor & Destructor Documentation

◆ QtService()

template<typename Application >
QtService< Application >::QtService ( int  argc,
char **  argv,
const QString &  name 

Constructs a QtService object called name. The argc and argv parameters are parsed after the exec() function has been called. Then they are passed to the application's constructor.

There can only be one QtService object in a process.

See also

Definition at line 166 of file qtservice.h.

◆ ~QtService()

template<typename Application >
QtService< Application >::~QtService ( )

Destroys the service object.

Definition at line 169 of file qtservice.h.

Member Function Documentation

◆ application()

template<typename Application >
Application * QtService< Application >::application ( ) const

Returns a pointer to the application object.

Definition at line 174 of file qtservice.h.

◆ createApplication()

template<typename Application >
void QtService< Application >::createApplication ( int &  argc,
char **  argv 

Creates application object of type Application passing argc and argv to its constructor.

Implements QtServiceBase.

Definition at line 177 of file qtservice.h.

◆ executeApplication()

template<typename Application >
int QtService< Application >::executeApplication ( )

Implements QtServiceBase.

Definition at line 184 of file qtservice.h.

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