Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Timer_Queue / Reactor_Timer_Queue_Test.cpp
blob6d668f9edc840867c5e1472cbeb8c35711c6a125
2 //=============================================================================
3 /**
4 * @file Reactor_Timer_Queue_Test.cpp
6 * This example tests the timer queue mechanism of ACE_Reactor.
8 * @author Nanbor Wang <nw1@cs.wustl.edu> and Sergio Flores-Gaitan <sergio@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "ace/OS_NS_sys_time.h"
14 #include "ace/Thread_Manager.h"
15 #include "ace/Select_Reactor.h"
16 #include "ace/Reactor.h"
17 #include "ace/Timer_Heap.h"
19 #include "Driver.h"
20 #include "Reactor_Timer_Queue_Test.h"
23 void
24 Reactor_Timer_Handler::set_timer_id (long tid)
26 this->tid_ = tid;
29 int
30 Reactor_Timer_Handler::handle_timeout (const ACE_Time_Value &,
31 const void *)
33 ACE_Time_Value txv = ACE_OS::gettimeofday ();
34 ACE_DEBUG ((LM_DEBUG,
35 "\nTimer #%d fired at %d.%06d (%T)!\n",
36 this->tid_,
37 txv.sec (),
38 txv.usec ()));
39 delete this;
41 return 0;
44 Input_Handler::Input_Handler (ACE_Timer_Queue *tq,
45 Reactor_Timer_Queue_Test_Driver &timer_queue_driver)
46 : done_ (0),
47 driver_ (timer_queue_driver)
49 this->tq_ = tq;
52 int
53 Input_Handler::done ()
55 return this->done_;
58 int
59 Input_Handler::schedule_timer (void *argument)
61 int delay = *(int *) argument;
62 Reactor_Timer_Handler *th;
63 long tid;
65 th = new Reactor_Timer_Handler;
66 if (th != 0)
68 tid = this->reactor ()->schedule_timer (th,
70 ACE_Time_Value (0, delay));
71 if (tid == -1)
72 ACE_DEBUG ((LM_DEBUG,
73 "Unable to schedule timer\n"));
74 else
76 ACE_DEBUG ((LM_DEBUG,
77 "Timer #%d schedule to fire after %d usec from now.\n",
78 tid,
79 delay));
80 th->set_timer_id (tid);
83 else
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "not enough memory?\n"),
86 -1);
87 return tid;
90 int
91 Input_Handler::cancel_timer (void *argument)
93 int id = *(int *) argument;
94 return this->reactor ()->cancel_timer (id);
97 int
98 Input_Handler::shutdown_timer (void *)
100 this->done_ = 1;
101 ACE_DEBUG ((LM_DEBUG,
102 "Shutting down event loop\n"));
103 return -1;
107 Input_Handler::list_timer (void *)
109 ACE_Timer_Queue_Iterator &iter = this->tq_->iter ();
110 ACE_DEBUG ((LM_DEBUG,
111 "\n\nTimers in queue:\n"));
113 for (; !iter.isdone (); iter.next ())
115 ACE_Timer_Node *tn = iter.item ();
116 ACE_DEBUG ((LM_DEBUG, "Timer #%d: %d.%06d\n",
117 tn->get_timer_id (),
118 tn->get_timer_value ().sec (),
119 tn->get_timer_value ().usec ()));
121 return 0;
125 Input_Handler::handle_input (ACE_HANDLE)
127 return driver_.get_next_request ();
130 Reactor_Timer_Queue_Test_Driver::Reactor_Timer_Queue_Test_Driver ()
131 : thandler_ (&timer_queue_, *this)
135 Reactor_Timer_Queue_Test_Driver::~Reactor_Timer_Queue_Test_Driver ()
137 // unhook our timer queue
138 ACE_Reactor::instance ()->timer_queue (0);
142 Reactor_Timer_Queue_Test_Driver::display_menu ()
144 static char menu[] =
145 "\n*****\n"
146 "1) Schedule timer <usec>\n"
147 "2) Cancel timer <id>\n"
148 "3) List all timers\n"
149 "4) Shutdown program\n"
150 "Enter selection:";
152 ACE_DEBUG ((LM_DEBUG,
153 "%s",
154 menu));
155 return 0;
159 Reactor_Timer_Queue_Test_Driver::init ()
161 typedef Command<Input_Handler, Input_Handler::ACTION> CMD;
163 // initialize <Command>s with their corresponding <Input_Handler> methods.
164 ACE_NEW_RETURN (schedule_cmd_,
165 CMD (thandler_, &Input_Handler::schedule_timer),
166 -1);
168 ACE_NEW_RETURN (cancel_cmd_,
169 CMD (thandler_, &Input_Handler::cancel_timer),
170 -1);
172 ACE_NEW_RETURN (list_cmd_,
173 CMD (thandler_, &Input_Handler::list_timer),
174 -1);
176 ACE_NEW_RETURN (shutdown_cmd_,
177 CMD (thandler_, &Input_Handler::shutdown_timer),
178 -1);
180 ACE_Reactor::instance ()->timer_queue (&timer_queue_);
182 ACE_Event_Handler::register_stdin_handler (&thandler_,
183 ACE_Reactor::instance (),
184 ACE_Thread_Manager::instance ());
186 // print the menu of options.
187 this->display_menu ();
189 return 0;
192 // run test was overrun due to the reactive way of handling input.
195 Reactor_Timer_Queue_Test_Driver::run_test ()
197 ACE_DEBUG ((LM_DEBUG,
198 "TIMER TEST STARTED\n"));
200 this->init ();
202 // Run until we say stop.
203 while (thandler_.done () == 0)
204 ACE_Reactor::instance ()->handle_events ();
206 ACE_DEBUG ((LM_DEBUG,
207 "TIMER TEST ENDED\n"));
208 return 0;