diff options
author | Steve Huston <shuston@riverace.com> | 1998-12-15 14:25:55 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 1998-12-15 14:25:55 +0000 |
commit | 5efa67b4c88c257482c2be72c0636bfa0a4fb3b4 (patch) | |
tree | 607aa770bc81c3e9de52ce23b5e4fcd97fca406b /examples/NT_Service | |
parent | afeb1d23f940595c4b493cb4ff37a917ed208cfa (diff) | |
download | ATCD-5efa67b4c88c257482c2be72c0636bfa0a4fb3b4.tar.gz |
Sample application to illustrate how to create and use an NT service using ACE.
Diffstat (limited to 'examples/NT_Service')
-rw-r--r-- | examples/NT_Service/NT_Service.dsp | 101 | ||||
-rw-r--r-- | examples/NT_Service/NT_Service.dsw | 29 | ||||
-rw-r--r-- | examples/NT_Service/main.cpp | 198 | ||||
-rw-r--r-- | examples/NT_Service/ntsvc.cpp | 100 | ||||
-rw-r--r-- | examples/NT_Service/ntsvc.h | 60 |
5 files changed, 488 insertions, 0 deletions
diff --git a/examples/NT_Service/NT_Service.dsp b/examples/NT_Service/NT_Service.dsp new file mode 100644 index 00000000000..295b6011d0f --- /dev/null +++ b/examples/NT_Service/NT_Service.dsp @@ -0,0 +1,101 @@ +# Microsoft Developer Studio Project File - Name="NT_Service" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 5.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=NT_Service - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "NT_Service.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "NT_Service.mak" CFG="NT_Service - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "NT_Service - Win32 Release" (based on\
+ "Win32 (x86) Console Application")
+!MESSAGE "NT_Service - Win32 Debug" (based on\
+ "Win32 (x86) Console Application")
+!MESSAGE
+
+# Begin Project
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "NT_Service - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c
+# SUBTRACT CPP /YX
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 ace.lib user32.lib advapi32.lib /nologo /subsystem:console /machine:I386 /libpath:"..\..\ace"
+
+!ELSEIF "$(CFG)" == "NT_Service - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /I "..\.." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c
+# SUBTRACT CPP /YX
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 aced.lib user32.lib advapi32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\..\ace"
+
+!ENDIF
+
+# Begin Target
+
+# Name "NT_Service - Win32 Release"
+# Name "NT_Service - Win32 Debug"
+# Begin Source File
+
+SOURCE=.\main.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\ntsvc.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\ntsvc.h
+# End Source File
+# End Target
+# End Project
diff --git a/examples/NT_Service/NT_Service.dsw b/examples/NT_Service/NT_Service.dsw new file mode 100644 index 00000000000..27e63183d37 --- /dev/null +++ b/examples/NT_Service/NT_Service.dsw @@ -0,0 +1,29 @@ +Microsoft Developer Studio Workspace File, Format Version 5.00
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
+
+###############################################################################
+
+Project: "NT_Service"=".\NT_Service.dsp" - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+}}}
+
+###############################################################################
+
+Global:
+
+Package=<5>
+{{{
+}}}
+
+Package=<3>
+{{{
+}}}
+
+###############################################################################
+
diff --git a/examples/NT_Service/main.cpp b/examples/NT_Service/main.cpp new file mode 100644 index 00000000000..b4198c08f9f --- /dev/null +++ b/examples/NT_Service/main.cpp @@ -0,0 +1,198 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// examples/NT_Service +// +// = FILENAME +// main.cpp +// +// = DESCRIPTION +// This is the main program - it just hands control off to the process +// instance to figure out what to do. +// +// = AUTHOR +// Gonzalo Diethelm and Steve Huston +// +// ============================================================================ + +#include "ace/Get_Opt.h" +#include "ntsvc.h" + + +class Process +{ +public: + Process(); + ~Process(); + + int run(int argc, char* argv[]); + +private: + void parse_args(int argc, char* argv[]); + void print_usage_and_die(); + +private: + char progname[128]; + + int opt_install; + int opt_remove; + int opt_start; + int opt_kill; + int opt_type; + int opt_debug; + + int opt_startup; +}; + +typedef ACE_Singleton<Process, ACE_Mutex> PROCESS; + + + + +Process::Process() +: opt_install(0), + opt_remove(0), + opt_start(0), + opt_kill(0), + opt_type(0), + opt_debug(0), + opt_startup(0) +{ + ACE_OS::strcpy(progname, "service"); + ACE::init(); +} + +Process::~Process() +{ + ACE::fini(); +} + +void Process::print_usage_and_die() +{ + ACE_DEBUG((LM_INFO, "Usage: %s" + " -in -r -s -k -tn -d\n" + " -i: Install this program as an NT service, with specified startup\n" + " -r: Remove this program from the Service Manager\n" + " -s: Start the service\n" + " -k: Kill the service\n" + " -t: Set startup for an existing service\n" + " -d: Debug; run as a regular application\n", + progname)); + ACE_OS::exit(0); +} + +void Process::parse_args(int argc, char* argv[]) +{ + ACE_Get_Opt get_opt(argc, argv, "i:rskt:d"); + int c; + while ((c = get_opt()) != -1) + switch (c) + { + case 'i': + opt_install = 1; + opt_startup = ACE_OS::atoi(get_opt.optarg); + break; + case 'r': + opt_remove = 1; + break; + case 's': + opt_start = 1; + break; + case 'k': + opt_kill = 1; + break; + case 't': + opt_type = 1; + opt_startup = ACE_OS::atoi(get_opt.optarg); + break; + case 'd': + opt_debug = 1; + break; + default: + print_usage_and_die(); + break; + } +} + +// Define a function to handle Ctrl+C to cleanly shut this down. +static BOOL __stdcall ConsoleHandler(DWORD ctrlType) +{ + SERVICE::instance()->handle_control(SERVICE_CONTROL_STOP); + return TRUE; +} + +ACE_NT_SERVICE_DEFINE(Beeper, Service, "Annoying Beeper Service") + +int Process::run(int argc, char* argv[]) +{ + SERVICE::instance()->name("Beeper", "Annoying Beeper Service"); + + parse_args(argc, argv); + + if (opt_install && ! opt_remove) + { + if (opt_startup > 0) + { + return SERVICE::instance()->insert(opt_startup); + } + else + { + return SERVICE::instance()->insert(); + } + } + + if (opt_remove && ! opt_install) + { + return SERVICE::instance()->remove(); + } + + if (opt_start && opt_kill) + { + print_usage_and_die(); + } + + if (opt_start) + { + return SERVICE::instance()->start_svc(); + } + + if (opt_kill) + { + return SERVICE::instance()->stop_svc(); + } + + if (opt_type) + { + return SERVICE::instance()->startup(opt_startup); + } + + + // If we get here, we either run the app in debug mode + // (-d) or are being called from the service manager + // to start the service. + + + if (opt_debug) + { + SetConsoleCtrlHandler(&ConsoleHandler, 1); + SERVICE::instance()->svc(); + } + else + { + ACE_NT_SERVICE_RUN(Beeper, SERVICE::instance(), ret) + if (ret == 0) + { + ACE_ERROR((LM_ERROR, "%p\n", "Couldn't start service")); + } + } + + return 0; +} + + +int main(int argc, char* argv[]) +{ + return PROCESS::instance()->run(argc, argv); +} diff --git a/examples/NT_Service/ntsvc.cpp b/examples/NT_Service/ntsvc.cpp new file mode 100644 index 00000000000..b34ca4ba02c --- /dev/null +++ b/examples/NT_Service/ntsvc.cpp @@ -0,0 +1,100 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// examples/NT_Service +// +// = FILENAME +// ntsvc.cpp +// +// = DESCRIPTION +// This is the implementation of the NT service. It beeps every 2 +// seconds until the service is stopped. +// +// = AUTHOR +// Gonzalo Diethelm and Steve Huston +// +// ============================================================================ + +#include "ace/OS.h" +#include "ace/Reactor.h" + +#include "ntsvc.h" + +Service::Service() +{ + + // Remember the Reactor instance. + reactor(ACE_Reactor::instance()); + + // Schedule a timer every two seconds. + ACE_Time_Value tv(2, 0); + ACE_Reactor::instance()->schedule_timer(this, 0, tv, tv); +} + + +// This method is called when the service gets a control request. It +// handles requests for stop and shutdown by calling terminate(). All +// others get handled by calling up to inherited::handle_control. +void Service::handle_control(DWORD control_code) +{ + + if (control_code == SERVICE_CONTROL_SHUTDOWN || + control_code == SERVICE_CONTROL_STOP) + { + report_status(SERVICE_STOP_PENDING); + ACE_DEBUG((LM_INFO, "Service control stop requested\n")); + stop_ = 1; + reactor()->notify(this, ACE_Event_Handler::EXCEPT_MASK); + } + else + { + inherited::handle_control(control_code); + } +} + +// This is just here to be the target of the notify from above... it +// doesn't do anything except aid on popping the reactor off its wait +// and causing a drop out of handle_events. +int Service::handle_exception(ACE_HANDLE) +{ + return 0; +} + + +// Beep every two seconds. +// This is what this NT service does... +int Service::handle_timeout(const ACE_Time_Value& tv, + const void*) +{ + MessageBeep(MB_OK); + return 0; +} + +// This is the main processing function for the Service. It sets up +// the initial configuration and runs the event loop until a shutdown +// request is received. +int Service::svc() +{ + ACE_DEBUG((LM_DEBUG, "Service::svc\n")); + + // As an NT service, we come in here in a different thread than the + // one which created the reactor. So in order to do anything, we + // need to own the reactor. If we are not a service, report_status + // will return -1. + if (report_status(SERVICE_RUNNING) == 0) + { + reactor()->owner(ACE_Thread::self()); + } + + stop_ = 0; + while (! stop_) + { + reactor()->handle_events(); + } + + // Cleanly terminate connections, terminate threads. + ACE_DEBUG((LM_DEBUG, "Shutting down\n")); + return 0; +} diff --git a/examples/NT_Service/ntsvc.h b/examples/NT_Service/ntsvc.h new file mode 100644 index 00000000000..2d74beab53b --- /dev/null +++ b/examples/NT_Service/ntsvc.h @@ -0,0 +1,60 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// examples/NT_Service +// +// = FILENAME +// ntsvc.h +// +// = DESCRIPTION +// This is the definition of the sample NT Service class. +// +// = AUTHOR +// Gonzalo Diethelm and Steve Huston +// +// ============================================================================ + +#ifndef NTSVC_H_ +#define NTSVC_H_ + +#include "ace/Event_Handler.h" +#include "ace/NT_Service.h" +#include "ace/Singleton.h" +#include "ace/Synch.h" + +class Service : public ACE_NT_Service +{ +public: + Service(); + + // We override handle_control() because it handles stop requests privately. + virtual void handle_control(DWORD control_code); + + // We override handle_exception() so a 'stop' control code can pop + // the reactor off of its wait. + virtual int handle_exception(ACE_HANDLE h); + + // This is a virtual method inherited from ACE_NT_Service. + virtual int svc(); + + // Where the real work is done: + virtual int handle_timeout(const ACE_Time_Value& tv, + const void* arg = 0); + +private: + typedef ACE_NT_Service inherited; + +private: + int stop_; +}; + + +// Define a singleton class as a way to insure that there's only one +// Service instance in the program, and to protect against access from +// multiple threads. The first reference to it at runtime creates it, +// and the ACE_Object_Manager deletes it at run-down. +typedef ACE_Singleton<Service, ACE_Mutex> SERVICE; + +#endif /* #ifndef NTSVC_H_ */ |