summaryrefslogtreecommitdiff
path: root/examples/NT_Service/ntsvc.cpp
blob: b08f5ba0cab3c12dc264fc7f92ed6d09b2603eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// $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 <gonzo@cs.wustl.edu> 
//    and Steve Huston <shuston@riverace.com>
//
// ============================================================================

#include "ace/OS.h"
#include "ace/Reactor.h"
#include "ntsvc.h"

Service::Service (void)
{
  // 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 (void)
{
  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;
}