summaryrefslogtreecommitdiff
path: root/ACE/examples/NT_Service/ntsvc.cpp
blob: 9fd677630acd36e8102138f09aa06d91e107e937 (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
104
105
106
107
108
109
110
111
112

//=============================================================================
/**
 *  @file    ntsvc.cpp
 *
 *  $Id$
 *
 *    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/Reactor.h"
#include "ntsvc.h"

#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)

Service::Service (void)
{
  // Remember the Reactor instance.
  reactor (ACE_Reactor::instance ());
}

Service::~Service (void)
{
  if (ACE_Reactor::instance ()->cancel_timer(this) == -1)
    ACE_ERROR ((LM_ERROR,
                "Service::~Service failed to cancel_timer.\n"));
}

// 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,
                  ACE_TEXT ("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 *)
{
  ACE_UNUSED_ARG (tv);
  MessageBeep (MB_OK);
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%T (%t): Beep...\n")));
  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,
              ACE_TEXT ("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 ());

  this->stop_ = 0;

  // Schedule a timer every two seconds.
  ACE_Time_Value tv (2, 0);
  ACE_Reactor::instance ()->schedule_timer (this, 0, tv, tv);

  while (!this->stop_)
    reactor ()->handle_events ();

  // Cleanly terminate connections, terminate threads.
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Shutting down\n")));
  reactor ()->cancel_timer (this);
  return 0;
}

#endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */