service_root Class Reference

Provides a reusable base class for a Win32 service. More...

#include <service_root.h>

Inheritance diagram for service_root:

Inheritance graph
[legend]
Collaboration diagram for service_root:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 service_root (const istring &service_name, const istring &display_name, const string_array &dependencies)
 constructs the root portion of a service for the "service_name".
virtual ~service_root ()
 IMPLEMENT_CLASS_NAME ("service_root")
int initialize (int argc, char **argv, const istring &program_name)
 this function should be invoked by the main program of the service.
virtual void perform_service (int argc, char **argv)=0
 this must be overridden by derived classes to provide a service.
bool should_exit () const
 returns true if this service should exit now.
void stop_service ()
 signals the service to stop running as soon as possible.
const istringservice_name () const
const istringdisplay_name () const
const string_arraydependencies () const
bool normal_app () const
 returns true if service is running in stand-alone mode.
int error_code () const
 allows the per service last error to be examined.
void error_code (int new_code)
 allows the per service last error to be set.
int instruct (int argc, char **argv)
 print the instructions on how to use the service.
u_int current_status ()
 reports the current status for this service.
bool report_status (u_int current_state, u_int exit_code, u_int wait_hint)
 sets the current status of the service and tells the SCM.
void log_event (const istring &message) const
 allows a message to be logged to the system event log.
void install (bool set_auto, const istring &user, const istring &password)
 puts the service into NT's list of services.
void remove ()
 takes the service out of NT's list. this also causes it to be stopped.
void standalone (int argc, char **argv)
 runs in normal mode, not as a service.

Static Public Member Functions

static service_rootget_root ()
 returns the last global root object that checked in.

Detailed Description

Provides a reusable base class for a Win32 service.

NOTE: to create a service based on this class is quite simple. one needs to provide a class derived from the service_root (named for example, "razzle_service") and to override the perform_service() method. then, starting the service is especially simple... an example main program for the service is:

      #include "my_service.h"
      int main(int argc, char **argv) {
        my_service serv;
        return serv.initialize(argc, argv)
      } 
the initialize() method takes care of invoking the service properly and handling control requests from the SCM (Service Control Manager). when that function returns, the operation of the service is over.

Definition at line 57 of file service_root.h.


Constructor & Destructor Documentation

service_root::service_root ( const istring service_name,
const istring display_name,
const string_array dependencies 
)

constructs the root portion of a service for the "service_name".

the "service_name" is how the operating system represents the name for this service, while the "display_name" is what is shown to the user. if this service depends on others, they should be listed in the "dependencies". the list of names will be turned into the string form expected by the operating system.

Definition at line 69 of file service_root.cpp.

References SET_DEFAULT_COMBO_LOGGER.

service_root::~service_root (  )  [virtual]

Definition at line 87 of file service_root.cpp.

References NIL, rendezvous::unlock(), and WHACK().


Member Function Documentation

service_root::IMPLEMENT_CLASS_NAME ( "service_root"   ) 

int service_root::initialize ( int  argc,
char **  argv,
const istring program_name 
)

this function should be invoked by the main program of the service.

it parses the command line parameters in "argc" and "argv" to determine what kind of a service is being requested. it then begins the operation of the service and eventually invokes the perform_service() method. the value returned by this function can be used as the return value from your main() function. the "program_name" is an optional argument; if it's non-empty, then a mutex based on the name will be created and the service will check that it is not already running.

Definition at line 226 of file service_root.cpp.

References _control_handle, _service_status, ini_configurator::APPLICATION_DIRECTORY, current_status(), command_line::entries(), error_code(), command_line::find(), FUNCDEF, command_line::get(), get_root(), install(), instruct(), array< contents >::length(), configurator::load(), LOG, log_event(), NIL, perform_service(), remove(), report_status(), configurator::RETURN_ONLY, istring::s(), SERVICE_CONTROL_INTERROGATE, SERVICE_CONTROL_STOP, service_name(), SERVICE_START_PENDING, SERVICE_STOP_PENDING, SERVICE_STOPPED, standalone(), stop_service(), command_parameter::text(), and unlink.

Referenced by main().

virtual void service_root::perform_service ( int  argc,
char **  argv 
) [pure virtual]

this must be overridden by derived classes to provide a service.

it begins the operations that are specific to the service being offered. perform_service() can rely on the lower-level service support having begun execution normally and can be assured that the services this one depends on have already started. this function should not exit until should_exit() returns true; at that point, it should exit as quickly as possible.

Implemented in empty_service, and simple_service.

Referenced by initialize(), and standalone().

bool service_root::should_exit (  )  const [inline]

returns true if this service should exit now.

this should be checked periodically by the perform_service() method so that it will return in a timely manner. after the return, the service will automatically be shut down.

Definition at line 92 of file service_root.h.

void service_root::stop_service (  ) 

signals the service to stop running as soon as possible.

this merely ensures that the should_exit() method returns true.

Definition at line 117 of file service_root.cpp.

Referenced by initialize().

const istring & service_root::service_name (  )  const

Definition at line 98 of file service_root.cpp.

Referenced by initialize(), install(), log_event(), and remove().

const istring & service_root::display_name (  )  const

Definition at line 100 of file service_root.cpp.

Referenced by install(), remove(), and standalone().

const string_array & service_root::dependencies (  )  const

Definition at line 119 of file service_root.cpp.

bool service_root::normal_app (  )  const [inline]

returns true if service is running in stand-alone mode.

Definition at line 106 of file service_root.h.

int service_root::error_code (  )  const [inline]

allows the per service last error to be examined.

Definition at line 109 of file service_root.h.

Referenced by initialize().

void service_root::error_code ( int  new_code  )  [inline]

allows the per service last error to be set.

Definition at line 111 of file service_root.h.

int service_root::instruct ( int  argc,
char **  argv 
)

print the instructions on how to use the service.

Definition at line 122 of file service_root.cpp.

References log_base::log(), command_line::program_name(), program_wide_logger(), and filename::rootname().

Referenced by initialize().

u_int service_root::current_status (  ) 

reports the current status for this service.

the returned value is compatible with the report_status() method.

Definition at line 102 of file service_root.cpp.

Referenced by initialize(), and remove().

bool service_root::report_status ( u_int  current_state,
u_int  exit_code,
u_int  wait_hint 
)

sets the current status of the service and tells the SCM.

the "current_state" reports what the service is doing. the "exit_code" is an error to report, if any. the "wait_hint" is the worst case time estimate to the next checkpoint.

Definition at line 140 of file service_root.cpp.

References log_event(), SERVICE_RUNNING, SERVICE_START_PENDING, and SERVICE_STOPPED.

Referenced by initialize().

void service_root::log_event ( const istring message  )  const

allows a message to be logged to the system event log.

Definition at line 176 of file service_root.cpp.

References log_base::log(), NIL, program_wide_logger(), service_name(), and istring::SPRINTF.

Referenced by initialize(), and report_status().

service_root & service_root::get_root (  )  [static]

returns the last global root object that checked in.

this is needed by some of the static methods.

Definition at line 111 of file service_root.cpp.

Referenced by initialize().

void service_root::install ( bool  set_auto,
const istring user,
const istring password 
)

puts the service into NT's list of services.

if "set_auto" is true, then the service is marked as automatically started rather than as manually started. if the "user" is non-empty, then the service is set to run under that id using the "password". the "user" must be a bare user name without any domain.

Definition at line 401 of file service_root.cpp.

References display_name(), istring::find(), FUNCDEF, istring::iequals(), log_base::log(), LOG, portable::module_name(), negative(), NIL, program_wide_logger(), istring::s(), service_name(), portable::system_error_text(), istring::t(), and WHACK().

Referenced by initialize().

void service_root::remove (  ) 

takes the service out of NT's list. this also causes it to be stopped.

Definition at line 477 of file service_root.cpp.

References log_base::CRLF_AT_END, current_status(), display_name(), log_base::eol(), FUNCDEF, log_base::log(), NIL, log_base::NO_ENDING, program_wide_logger(), SERVICE_CONTROL_STOP, service_name(), SERVICE_STOP_PENDING, SERVICE_STOPPED, portable::sleep_ms(), and portable::system_error_text().

Referenced by initialize().

void service_root::standalone ( int  argc,
char **  argv 
)

runs in normal mode, not as a service.

this will not work in exactly the same way as a service, but most functionality should be usable.

Definition at line 534 of file service_root.cpp.

References display_name(), log_base::log(), perform_service(), and program_wide_logger().

Referenced by initialize().


The documentation for this class was generated from the following files:
Generated on Fri Sep 5 04:30:51 2008 for HOOPLE Libraries by  doxygen 1.5.1