summaryrefslogtreecommitdiff
path: root/examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp
blob: 1927e27f128dfb32f4bfebd49e207c8ed0ca25b9 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

//=============================================================================
/**
*  @file    Reactor_Timer_Queue_Test.cpp
 *
 *  $Id$
 *
 *  This example tests the timer queue mechanism of ACE_Reactor.
 *
 *
 *  @author Nanbor Wang <nw1@cs.wustl.edu> and Sergio Flores-Gaitan <sergio@cs.wustl.edu>
 */
//=============================================================================


#include "ace/OS_NS_sys_time.h"
#include "ace/Thread_Manager.h"
#include "ace/Select_Reactor.h"
#include "ace/Reactor.h"
#include "ace/Timer_Heap.h"

#include "Driver.h"
#include "Reactor_Timer_Queue_Test.h"



void
Reactor_Timer_Handler::set_timer_id (long tid)
{
  this->tid_ = tid;
}

int
Reactor_Timer_Handler::handle_timeout (const ACE_Time_Value &,
                                       const void *)
{
  ACE_Time_Value txv = ACE_OS::gettimeofday ();
  ACE_DEBUG ((LM_DEBUG,
              "\nTimer #%d fired at %d.%06d (%T)!\n",
              this->tid_,
              txv.sec (),
              txv.usec ()));
  delete this;

  return 0;
}

Input_Handler::Input_Handler (ACE_Timer_Queue *tq,
                              Reactor_Timer_Queue_Test_Driver &timer_queue_driver)
  : done_ (0),
    driver_ (timer_queue_driver)
{
  this->tq_ = tq;
}

int
Input_Handler::done (void)
{
  return this->done_;
}

int
Input_Handler::schedule_timer (void *argument)
{
  int delay = *(int *) argument;
  Reactor_Timer_Handler *th;
  long tid;

  th = new Reactor_Timer_Handler;
  if (th != 0)
    {
      tid = this->reactor ()->schedule_timer (th,
                                              0,
                                              ACE_Time_Value (0, delay));
      if (tid == -1)
        ACE_DEBUG ((LM_DEBUG,
                    "Unable to schedule timer\n"));
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                      "Timer #%d schedule to fire after %d usec from now.\n",
                      tid,
                      delay));
          th->set_timer_id (tid);
        }
    }
  else
    ACE_ERROR_RETURN ((LM_ERROR,
                       "not enough memory?\n"),
                      -1);
  return tid;
}

int
Input_Handler::cancel_timer (void *argument)
{
  int id = *(int *) argument;
  return this->reactor ()->cancel_timer (id);
}

int
Input_Handler::shutdown_timer (void *)
{
  this->done_ = 1;
  ACE_DEBUG ((LM_DEBUG,
              "Shutting down event loop\n"));
  return -1;
}

int
Input_Handler::list_timer (void *)
{
  ACE_Timer_Queue_Iterator &iter = this->tq_->iter ();
  ACE_DEBUG ((LM_DEBUG,
              "\n\nTimers in queue:\n"));

  for (; !iter.isdone (); iter.next ())
    {
      ACE_Timer_Node *tn = iter.item ();
      ACE_DEBUG ((LM_DEBUG, "Timer #%d: %d.%06d\n",
                  tn->get_timer_id (),
                  tn->get_timer_value ().sec (),
                  tn->get_timer_value ().usec ()));
    }
  return 0;
}

int
Input_Handler::handle_input (ACE_HANDLE)
{
  return driver_.get_next_request ();
}

Reactor_Timer_Queue_Test_Driver::Reactor_Timer_Queue_Test_Driver (void)
  : thandler_ (&timer_queue_, *this)
{
}

Reactor_Timer_Queue_Test_Driver::~Reactor_Timer_Queue_Test_Driver (void)
{
  // unhook our timer queue
  ACE_Reactor::instance ()->timer_queue (0);
}

int
Reactor_Timer_Queue_Test_Driver::display_menu (void)
{
  static char menu[] =
    "\n*****\n"
    "1) Schedule timer <usec>\n"
    "2) Cancel timer <id>\n"
    "3) List all timers\n"
    "4) Shutdown program\n"
    "Enter selection:";

  ACE_DEBUG ((LM_DEBUG,
              "%s",
              menu));
  return 0;
}

int
Reactor_Timer_Queue_Test_Driver::init (void)
{
  typedef Command<Input_Handler, Input_Handler::ACTION> CMD;

  // initialize <Command>s with their corresponding <Input_Handler>  methods.
  ACE_NEW_RETURN (schedule_cmd_,
                  CMD (thandler_, &Input_Handler::schedule_timer),
                  -1);

  ACE_NEW_RETURN (cancel_cmd_,
                  CMD (thandler_, &Input_Handler::cancel_timer),
                  -1);

  ACE_NEW_RETURN (list_cmd_,
                  CMD (thandler_, &Input_Handler::list_timer),
                  -1);

  ACE_NEW_RETURN (shutdown_cmd_,
                  CMD (thandler_, &Input_Handler::shutdown_timer),
                  -1);

  ACE_Reactor::instance ()->timer_queue (&timer_queue_);

  ACE_Event_Handler::register_stdin_handler (&thandler_,
                                             ACE_Reactor::instance (),
                                             ACE_Thread_Manager::instance ());

  // print the menu of options.
  this->display_menu ();

  return 0;
}

// run test was overrun due to the reactive way of handling input.

int
Reactor_Timer_Queue_Test_Driver::run_test (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "TIMER TEST STARTED\n"));

  this->init ();

  // Run until we say stop.
  while (thandler_.done () == 0)
    ACE_Reactor::instance ()->handle_events ();

  ACE_DEBUG ((LM_DEBUG,
              "TIMER TEST ENDED\n"));
  return 0;
}