2 //=============================================================================
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>
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"
20 #include "Reactor_Timer_Queue_Test.h"
24 Reactor_Timer_Handler::set_timer_id (long tid
)
30 Reactor_Timer_Handler::handle_timeout (const ACE_Time_Value
&,
33 ACE_Time_Value txv
= ACE_OS::gettimeofday ();
35 "\nTimer #%d fired at %d.%06d (%T)!\n",
44 Input_Handler::Input_Handler (ACE_Timer_Queue
*tq
,
45 Reactor_Timer_Queue_Test_Driver
&timer_queue_driver
)
47 driver_ (timer_queue_driver
)
53 Input_Handler::done ()
59 Input_Handler::schedule_timer (void *argument
)
61 int delay
= *(int *) argument
;
62 Reactor_Timer_Handler
*th
;
65 th
= new Reactor_Timer_Handler
;
68 tid
= this->reactor ()->schedule_timer (th
,
70 ACE_Time_Value (0, delay
));
73 "Unable to schedule timer\n"));
77 "Timer #%d schedule to fire after %d usec from now.\n",
80 th
->set_timer_id (tid
);
84 ACE_ERROR_RETURN ((LM_ERROR
,
85 "not enough memory?\n"),
91 Input_Handler::cancel_timer (void *argument
)
93 int id
= *(int *) argument
;
94 return this->reactor ()->cancel_timer (id
);
98 Input_Handler::shutdown_timer (void *)
101 ACE_DEBUG ((LM_DEBUG
,
102 "Shutting down event loop\n"));
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",
118 tn
->get_timer_value ().sec (),
119 tn
->get_timer_value ().usec ()));
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 ()
146 "1) Schedule timer <usec>\n"
147 "2) Cancel timer <id>\n"
148 "3) List all timers\n"
149 "4) Shutdown program\n"
152 ACE_DEBUG ((LM_DEBUG
,
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
),
168 ACE_NEW_RETURN (cancel_cmd_
,
169 CMD (thandler_
, &Input_Handler::cancel_timer
),
172 ACE_NEW_RETURN (list_cmd_
,
173 CMD (thandler_
, &Input_Handler::list_timer
),
176 ACE_NEW_RETURN (shutdown_cmd_
,
177 CMD (thandler_
, &Input_Handler::shutdown_timer
),
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 ();
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"));
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"));