Logo Search packages:      
Sourcecode: poco version File versions  Download package

Poco::Util::ServerApplication Class Reference

#include <ServerApplication.h>

Inheritance diagram for Poco::Util::ServerApplication:

Poco::Util::Application Poco::Util::Subsystem Poco::RefCountedObject EchoServer HTTPFormServer HTTPSTimeServer HTTPTimeServer TimeServer

List of all members.

Detailed Description

A subclass of the Application class that is used for implementing server applications.

A ServerApplication allows for the application to run as a Windows service or as a Unix daemon without the need to add extra code.

For a ServerApplication to work both from the command line and as a daemon or service, a few rules must be met:

int main(int argc, char** argv) { MyServerApplication app; return app.run(argc, argv); }

The POCO_SERVER_MAIN macro can be used to implement main(argc, argv). If POCO has been built with POCO_WIN32_UTF8, POCO_SERVER_MAIN supports Unicode command line arguments.

On Windows platforms, an application built on top of the ServerApplication class can be run both from the command line or as a service.

To run an application as a Windows service, it must be registered with the Windows Service Control Manager (SCM). To do this, the application can be started from the command line, with the /registerService option specified. This causes the application to register itself with the SCM, and then exit. Similarly, an application registered as a service can be unregistered, by specifying the /unregisterService option. The file name of the application executable (excluding the .exe suffix) is used as the service name. Additionally, a more user-friendly name can be specified, using the /displayName option (e.g., /displayName="Demo Service").

An application can determine whether it is running as a service by checking for the "application.runAsService" configuration property.

if (config().getBool("application.runAsService", false)) { do service specific things }

Note that the working directory for an application running as a service is the Windows system directory (e.g., C:). Take this into account when working with relative filesystem paths.

An application registered as a Windows service can be started with the NET START <name> command and stopped with the NET STOP <name> command. Alternatively, the Services MMC applet can be used.

On Unix platforms, an application built on top of the ServerApplication class can be optionally run as a daemon. A daemon, when launched, immediately forks off a background process that does the actual work. After launching the background process, the foreground process exits.

After the initialization is complete, but before entering the main() method, the current working directory for the daemon process is changed to the root directory ("/"), as it is common practice for daemon processes. Therefore, be careful when working with files, as relative paths may not point to where you expect them point to.

An application can determine whether it is running as a daemon by checking for the "application.runAsDaemon" configuration property.

if (config().getBool("application.runAsDaemon", false)) { do daemon specific things }

Definition at line 52 of file ServerApplication.h.

Public Types

enum  ConfigPriority { PRIO_APPLICATION = -100, PRIO_DEFAULT = 0, PRIO_SYSTEM = 100 }
enum  ExitCode {

Public Member Functions

void addSubsystem (Subsystem *pSubsystem)
 Creates the Application and calls init(argc, argv).
std::string commandName () const
LayeredConfigurationconfig () const
 Returns the command name used to invoke the application.
void duplicate () const
template<class C>
C & getSubsystem () const
void init (const std::vector< std::string > &args)
void init (int argc, char *argv[])
bool initialized () const
bool isInteractive () const
 Destroys the ServerApplication.
void loadConfiguration (const std::string &path, int priority=PRIO_DEFAULT)
int loadConfiguration (int priority=PRIO_DEFAULT)
Poco::Loggerlogger () const
 Returns the application's configuration.
const char * name () const
const OptionSetoptions () const
int referenceCount () const
void release () const
 Increments the object's reference count.
int run (int argc, char **argv)
void setUnixOptions (bool flag)
const Poco::TimestampstartTime () const
void stopOptionsProcessing ()
 Returns the application uptime.
Poco::Timespan uptime () const
 Returns the application start time (UTC).
 ~ServerApplication ()
 Creates the ServerApplication.

Static Public Member Functions

static Applicationinstance ()
 Returns the application's option set.

Protected Member Functions

void defineOptions (OptionSet &options)
bool findFile (Poco::Path &path) const
void handleOption (const std::string &name, const std::string &value)
void init ()
void initialize (Application &self)
virtual int main (const std::vector< std::string > &args)
 Sets the logger used by the application.
void reinitialize (Application &self)
int run ()
void setLogger (Poco::Logger &logger)
void uninitialize ()
void waitForTerminationRequest ()

Static Protected Member Functions

static void terminate ()


class Application
 Destroys the Subsystem.

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

Generated by  Doxygen 1.6.0   Back to index