Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Reactor / Timer_Cancel.cpp
blob8928b046f019e18db0bc4298fdf1ac239b696bbb
1 /**
2 * Changing the interval and canceling
3 */
5 #include "ace/OS_NS_time.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/Reactor.h"
8 #include "ace/Event_Handler.h"
10 class MyTimerHandler : public ACE_Event_Handler
12 public:
13 int handle_timeout (const ACE_Time_Value &current_time,
14 const void * = 0)
16 time_t epoch = ((timespec_t)current_time).tv_sec;
17 ACE_DEBUG ((LM_INFO,
18 ACE_TEXT ("handle_timeout: %s"),
19 ACE_OS::ctime(&epoch)));
20 return 0;
24 #if !defined (ACE_LACKS_UNIX_SIGNALS)
26 // Listing 1 code/ch07
27 class SignalHandler : public ACE_Event_Handler
29 public:
30 SignalHandler (long timerId, int currentInterval)
31 : ACE_Event_Handler(),
32 timerId_(timerId),
33 currentInterval_(currentInterval)
37 int handle_signal (int sig, siginfo_t * = 0, ucontext_t * = 0)
39 if (sig == SIGINT)
41 ACE_DEBUG ((LM_INFO,
42 ACE_TEXT ("Resetting interval of timer ")
43 ACE_TEXT ("%d to %d\n"),
44 this->timerId_,
45 ++this->currentInterval_));
46 ACE_Time_Value newInterval (this->currentInterval_);
47 ACE_Reactor::instance ()->
48 reset_timer_interval (this->timerId_, newInterval);
50 else if (sig == SIGTSTP)
52 ACE_DEBUG ((LM_INFO,
53 ACE_TEXT ("Canceling timer %d\n"),
54 this->timerId_));
55 ACE_Reactor::instance ()->cancel_timer (this->timerId_);
58 return 0;
61 private:
62 long timerId_;
63 int currentInterval_;
65 // Listing 1
67 #endif /* ACE_LACKS_UNIX_SIGNALS */
70 int ACE_TMAIN (int, ACE_TCHAR *[])
72 ACE_Time_Value initialDelay (3);
73 ACE_Time_Value interval (5);
75 // Listing 2 code/ch07
76 MyTimerHandler *handler = new MyTimerHandler ();
77 long timerId =
78 ACE_Reactor::instance ()->schedule_timer (handler,
80 initialDelay,
81 interval);
82 // Listing 2
84 #if !defined (ACE_LACKS_UNIX_SIGNALS)
86 // Listing 3 code/ch07
87 SignalHandler *mutateTimer =
88 new SignalHandler (timerId, 5);
89 ACE_Reactor::instance ()->register_handler (SIGINT,
90 mutateTimer);
91 ACE_Reactor::instance ()->register_handler (SIGTSTP,
92 mutateTimer);
93 // Listing 3
95 #else
96 ACE_UNUSED_ARG (timerId);
97 #endif /* ACE_LACKS_UNIX_SIGNALS */
99 ACE_Reactor::instance ()->run_reactor_event_loop ();
100 return 0;