Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / NT_Service / ntsvc.cpp
blob451bf5e303fd29c5378d74db27f3b32c87849090
2 //=============================================================================
3 /**
4 * @file ntsvc.cpp
6 * This is the implementation of the NT service. It beeps every 2
7 * seconds until the service is stopped.
9 * @author Gonzalo Diethelm <gonzo@cs.wustl.edu> and Steve Huston <shuston@riverace.com>
11 //=============================================================================
14 #include "ace/Reactor.h"
15 #include "ntsvc.h"
17 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
19 Service::Service ()
21 // Remember the Reactor instance.
22 reactor (ACE_Reactor::instance ());
25 Service::~Service ()
27 if (ACE_Reactor::instance ()->cancel_timer(this) == -1)
28 ACE_ERROR ((LM_ERROR,
29 "Service::~Service failed to cancel_timer.\n"));
32 // This method is called when the service gets a control request. It
33 // handles requests for stop and shutdown by calling terminate ().
34 // All others get handled by calling up to inherited::handle_control.
36 void
37 Service::handle_control (DWORD control_code)
39 if (control_code == SERVICE_CONTROL_SHUTDOWN
40 || control_code == SERVICE_CONTROL_STOP)
42 report_status (SERVICE_STOP_PENDING);
44 ACE_DEBUG ((LM_INFO,
45 ACE_TEXT ("Service control stop requested\n")));
46 stop_ = 1;
47 reactor ()->notify (this,
48 ACE_Event_Handler::EXCEPT_MASK);
50 else
51 inherited::handle_control (control_code);
54 // This is just here to be the target of the notify from above... it
55 // doesn't do anything except aid on popping the reactor off its wait
56 // and causing a drop out of handle_events.
58 int
59 Service::handle_exception (ACE_HANDLE)
61 return 0;
64 // Beep every two seconds. This is what this NT service does...
66 int
67 Service::handle_timeout (const ACE_Time_Value &tv,
68 const void *)
70 ACE_UNUSED_ARG (tv);
71 MessageBeep (MB_OK);
72 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%T (%t): Beep...\n")));
73 return 0;
76 // This is the main processing function for the Service. It sets up
77 // the initial configuration and runs the event loop until a shutdown
78 // request is received.
80 int
81 Service::svc ()
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT ("Service::svc\n")));
86 // As an NT service, we come in here in a different thread than the
87 // one which created the reactor. So in order to do anything, we
88 // need to own the reactor. If we are not a service, report_status
89 // will return -1.
90 if (report_status (SERVICE_RUNNING) == 0)
91 reactor ()->owner (ACE_Thread::self ());
93 this->stop_ = 0;
95 // Schedule a timer every two seconds.
96 ACE_Time_Value tv (2, 0);
97 ACE_Reactor::instance ()->schedule_timer (this, 0, tv, tv);
99 while (!this->stop_)
100 reactor ()->handle_events ();
102 // Cleanly terminate connections, terminate threads.
103 ACE_DEBUG ((LM_DEBUG,
104 ACE_TEXT ("Shutting down\n")));
105 reactor ()->cancel_timer (this);
106 return 0;
109 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */