summaryrefslogtreecommitdiff
path: root/ACE/examples/Service_Configurator/Misc/Timer_Service.cpp
blob: d7bcdd15989ccbee6a41ff2b891cb3bf2da91171 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// $Id$

#include "ace/OS_NS_string.h"
#include "Timer_Service.h"
#include "ace/Log_Msg.h"

ACE_RCSID(Misc, Timer_Service, "$Id$")

Timer_Service_1::Timer_Service_1 (void)
{
  ACE_OS::strcpy (this->name_,
                  ACE_TEXT ("Timer_Service_1"));
}

int
Timer_Service_1::init (int argc, ACE_TCHAR *argv[])
{
  ACE_DEBUG ((LM_DEBUG,
	      ACE_TEXT ("in Timer_Service::init, argv[0] = %s, argc == %d\n"),
	      argv[0], argc));

  // Printout the <argv> values for sanity's sake.
  for (int i = 0; i < argc; i++)
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("argv[%d] = %s\n"),
                i, argv[i]));

  int interval = Timer_Service_1::TIMEOUT;

  if (argc > 1)
    {
      // If the second argument exists use this as the interval for
      // the periodic timer.  Otherwise, go off every TIMEOUT seconds.

      interval = ACE_OS::atoi (argv[1]);

      if (interval == 0)
        interval = Timer_Service_1::TIMEOUT;
    }

  if (argc > 2)
    {
      // If the third argument exists use it to control the maximum
      // number of timeouts.
      this->max_timeouts_ = ACE_OS::atoi (argv[2]);

      if (this->max_timeouts_ == 0)
        this->max_timeouts_ = Timer_Service_1::MAX_TIMEOUTS;
    }

  this->cur_timeouts_ = 0;

  // If the fourth argument exists take this as an indication to
  // enable tracing.
#if defined (ACE_HAS_TRACE)
  if (argc > 3)
    ACE_Trace::start_tracing ();
  else
    ACE_Trace::stop_tracing ();
#endif /* ACE_HAS_TRACE */

  // Register the timer to go off in 1 second, and then to go off
  // every <interval> seconds.
  if (ACE_Reactor::instance ()->schedule_timer
      (this,
       0,
       ACE_Time_Value (1),
       ACE_Time_Value (interval)) == -1)
    return -1;
  else
    return 0;
}

int
Timer_Service_1::handle_timeout (const ACE_Time_Value &tv,
                                 const void *)
{
  ACE_DEBUG ((LM_DEBUG,
	      ACE_TEXT ("(%x) in %s::handle_timeout sec = %d, usec = %d")
	      ACE_TEXT (" cur_timeouts = %d, max_timeouts = %d\n"),
              this,
              this->name_,
	      tv.sec (),
              tv.usec (),
	      this->cur_timeouts_,
              this->max_timeouts_));

  this->cur_timeouts_++;

  if (this->cur_timeouts_ == this->max_timeouts_)
    // Shutdown the test.
    return -1;
  else
    return 0;
}

int
Timer_Service_1::handle_close (ACE_HANDLE,
                               ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("closing down the timer test\n")));

  // Remove ourselves from the timer queue.
  ACE_Reactor::instance ()->cancel_timer (this);

  ACE_Reactor::end_event_loop();
  return 0;
}

Timer_Service_2::Timer_Service_2 (void)
{
  ACE_OS::strcpy (this->name_,
                  ACE_TEXT ("Timer_Service_2"));
}

Timer_Service_3::Timer_Service_3 (void)
{
  ACE_OS::strcpy (this->name_,
                  ACE_TEXT ("Timer_Service_3"));
}

// Define the object that describes the service.
ACE_STATIC_SVC_DEFINE (Timer_Service_1,
		       ACE_TEXT ("Timer_Service_1"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (Timer_Service_1),
		       ACE_Service_Type::DELETE_THIS | ACE_Service_Type::DELETE_OBJ,
                       0)

// The following are "Factories" used by the <ACE_Service_Config> and
// svc.conf file to dynamically initialize the state of the Timer
// Services.
ACE_SVC_FACTORY_DEFINE (Timer_Service_1)
ACE_SVC_FACTORY_DEFINE (Timer_Service_2)
ACE_SVC_FACTORY_DEFINE (Timer_Service_3)