• QtService
  • QtService Class

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

    Header: #include <QtService>
    Inherits: QtServiceBase
    Inherited By:

    nymeaserver::NymeaService

    Public Functions

    QtService(int argc, char ** argv, const QString & name)
    ~QtService()

    Protected Functions

    Application * application() const

    Reimplemented Protected Functions

    virtual void createApplication(int & argc, char ** argv)
    virtual int executeApplication()

    Additional Inherited Members

    Detailed Description

    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>
    {
    public:
        MyService(int argc, char **argv);
        ~MyService();
    
    protected:
        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 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 and QtServiceController.

    Member Function Documentation

    QtService::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 QtServiceBase().

    QtService::~QtService()

    Destroys the service object.

    [protected] Application * QtService::application() const

    Returns a pointer to the application object.

    [virtual protected] void QtService::createApplication(int & argc, char ** argv)

    Reimplemented from QtServiceBase::createApplication().

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

    [virtual protected] int QtService::executeApplication()

    Reimplemented from QtServiceBase::executeApplication().