Service Class Reference

Basic functionality for a system service, such as a daemon on UNIX or an "NT Service" on Windows. More...

#include <Service.h++>

Inheritance diagram for Service:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Service (int argc, char **argv, const String &version, const String &compileDate="", const String &compileTime="")
 Construct a new Service.
virtual ~Service ()
 Destructor.

Protected Member Functions

void setDescription (const String &desc)
 Set the service description.
void setWorkingDir (const String &dir)
 On POSIX systems, set the working directory for the service daemon process.
virtual bool processOption (char opt, const String &longOpt, const char *arg=NULL)
 Process a command-line option.
void detach () throw (SystemException)
 Detach the process from the console.
virtual void run ()=0
 The body of the service.
void testShutdown ()
 Test for asynchronous termination requests.
virtual void shutdown ()=0
 Shutdown handler.
bool isDebug () const
 Test if debug (foreground) mode is enabled.
bool registerOption (char opt, const String &longOpt, const String &argName, const String &desc)
 Register a command-line option.
bool parseOptions (int argc, char **argv, int &index)
 Parse the command line options.
virtual void printUsage ()
 Print the usage text.
void printError (const char *message,...)
 Print an error message.

Detailed Description

Basic functionality for a system service, such as a daemon on UNIX or an "NT Service" on Windows.

On POSIX systems, the service recognizes the --pidfile command line switch, whose argument is the path of a file to which the daemon process's PID should be written.

On Windows, the service recognizes the following command line switches:

--name=name
Specifies the name of the service; this is the logical name by which the service is known to Windows.
--displayname=name
Specifies the display name of the service; this is the name by which the service is known to the Service Control Manager (SCM), and appears in the "Name" column in the Service Manager applet.
--desc=description
Specifies a brief description for the service; this description appears in the "Description" column in the Service Manager applet.
--install
Installs the service into the Service Control Manager.
--uninstall
Uninstalls the service from the Service Control Manager.
--start
Starts the service.
--stop
Stops the service.

On all systems, the following switch is recognized:

--debug
Indicates that the service should run in debug (foreground) mode. In this mode, the detach() function passes control directly to the run() function without first detaching from the console. In addition, a keyboard interrupt (typically Control-C) will cause a termination request.

This class is implicitly a singleton; there can be only one instantiation of the class per process. The class should be subclassed and instantiated within the program's main() function.

Author:
Mark Lindner

Constructor & Destructor Documentation

Service ( int  argc,
char **  argv,
const String version,
const String compileDate = "",
const String compileTime = "" 
)

Construct a new Service.

Parameters:
argc The argument count (from main()).
argv The argument list (from main()).
version The optional version of the program; if not supplied, it defaults to "0.0".
compileDate The optional compile date of the program; typically provided by the compiler-defined macro __DATE__.
compileTime The optional compile time of the program; typically provided by the compiler-defined macro __TIME__.
~Service (  )  [virtual]

Destructor.


Member Function Documentation

void detach (  )  throw (SystemException) [protected]

Detach the process from the console.

This method must be called from the constructor once command line option processing and any other necessary initialization is complete. On POSIX systems, it forks a daemon process, while on Windows it passes control to the Service Control Manager (SCM) subsystem. On all platforms, execution resumes in the run() method, where the body of the service must be implemented.

Exceptions:
SystemException If the detach operation fails.
bool isDebug (  )  const [inline, protected]

Test if debug (foreground) mode is enabled.

bool parseOptions ( int  argc,
char **  argv,
int &  index 
) [protected, inherited]

Parse the command line options.

This method should be called from the constructor at the appropriate time, after any application-specific options have been registered and any other necessary initialization has been completed.

Parameters:
argc The argument count.
argv The argument list.
index Upon return, the index of the first non-option argument.
Returns:
true if the options were parsed successfully, or false if there was a parsing error. (Parsing errors are reported to stderr as a side-effect.)
void printError ( const char *  message,
  ... 
) [protected, inherited]

Print an error message.

The program name is automatically prepended to the message.

void printUsage (  )  [protected, virtual, inherited]

Print the usage text.

bool processOption ( char  opt,
const String longOpt,
const char *  arg = NULL 
) [protected, virtual]

Process a command-line option.

This method is called for each registered option that is encountered on the command line during the call to parseOptions().

The Application class registers the "--help" and "--version" command line options automatically. Therefore a subclass should override processOption() such that if it does not recognize the option, it will call Application::processOption() to give it a chance to process it.

Parameters:
opt The one-character switch, or NUL if not applicable.
longOpt The long name for the switch, or String::null if not applicable.
arg The argument for the switch, or NULL if not applicable.
Returns:
true if the option was processed successfully, false otherwise.

Reimplemented from Application.

bool registerOption ( char  opt,
const String longOpt,
const String argName,
const String desc 
) [protected, inherited]

Register a command-line option.

The option may have a short (one character) form and/or a long form. A typical invocation might look like this:

registerOption('o', "output", "file", "Specify @@ as the output file.");

The corresponding usage help text for this switch would appear as:

 -o <file>
 --output=<file>    - Specify <file> as the output file.
 
Parameters:
opt The one-character switch (e.g., 'v' for "-v"), or NUL if not applicable.
longOpt The long name for the switch (e.g., "verbose" for "--verbose"), or String::null if not applicable.
argName A token describing the argument for the switch, or the empty string "" if the switch does not accept an argument.
desc The description of the option, used in generating the usage text. Any occurrence of the token "@@" within the description will be replaced with a stylized form of the argument name, either by underlining the token or by enclosing it within angle brackets.
Returns:
true if the option was registered successfully, false otherwise. Attempts to register an existing option will fail.
virtual void run (  )  [protected, pure virtual]

The body of the service.

This method must be implemented to provide the actual service processing loop. It must periodically call testShutdown() to ensure that an asynchronous shutdown request will be processed in a timely manner.

void setDescription ( const String desc  )  [inline, protected]

Set the service description.

On Windows, this is used for the description in the Service Manager applet when the service is installed; on other platforms, it serves no purpose.

Parameters:
desc The new description.
void setWorkingDir ( const String dir  )  [inline, protected]

On POSIX systems, set the working directory for the service daemon process.

The default working directory is the root directory ("/").

Parameters:
dir The working directory.
virtual void shutdown (  )  [protected, pure virtual]

Shutdown handler.

The method must be overriden to provide cleanup and shutdown logic for the service. It is invoked as a side effect of calling testShutdown() when the service has been stopped via a TERM signal (UNIX), the Service Manager applet (Windows), or through use of the "--stop" command line switch (Windows). This function should not call exit() or otherwise terminate the process.

Reimplemented from Application.

void testShutdown (  )  [protected]

Test for asynchronous termination requests.

The service must regularly call this method to check for termination requests. The method will delegate to shutdown() if it has determined that a termination request is pending.


The documentation for this class was generated from the following files:
Generated on Sat Nov 26 16:49:09 2011 for libcommonc++ by  doxygen 1.6.3