Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / test_timeout_st.cpp
blobaf43be19501b5fa66bf7a0a884b0f73d12b577ad
2 //=============================================================================
3 /**
4 * @file test_timeout_st.cpp
6 * This example application shows how to write event loops that
7 * handle events for some fixed amount of time. This is the single
8 * threaded version of the test_timeout.cpp application.
10 * @author Irfan Pyarali and Alexander Babu Arulanthu
12 //=============================================================================
15 #include "ace/Proactor.h"
16 #include "ace/OS_main.h"
19 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
20 // This only works on Win32 platforms and on Unix platforms supporting
21 // POSIX aio calls.
23 /**
24 * @class Timeout_Handler
26 * @brief Generic timeout handler.
28 class Timeout_Handler : public ACE_Handler
30 public:
31 Timeout_Handler ()
32 : count_ (0),
33 start_time_ (ACE_OS::gettimeofday ())
37 virtual void handle_time_out (const ACE_Time_Value &tv,
38 const void *arg)
40 // Print out when timeouts occur.
41 ACE_DEBUG ((LM_DEBUG, "(%t) %d timeout occurred for %s @ %d.\n",
42 ++count_,
43 (char *) arg,
44 (tv - this->start_time_).sec ()));
47 private:
48 /// Sequence number for the timeouts.
49 int count_;
51 /// Starting time of the test.
52 ACE_Time_Value start_time_;
56 int
57 ACE_TMAIN (int, ACE_TCHAR *[])
59 Timeout_Handler handler;
61 // Register a 2 second timer.
62 ACE_Time_Value foo_tv (2);
63 if (ACE_Proactor::instance ()->schedule_timer (handler,
64 (void *) "Foo",
65 ACE_Time_Value::zero,
66 foo_tv) == -1)
67 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
69 // Register a 3 second timer.
70 ACE_Time_Value bar_tv (3);
71 if (ACE_Proactor::instance ()->schedule_timer (handler,
72 (void *) "Bar",
73 ACE_Time_Value::zero,
74 bar_tv) == -1)
75 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
77 // Handle events for 13 seconds.
78 ACE_Time_Value run_time (13);
80 ACE_DEBUG ((LM_DEBUG, "Starting event loop\n"));
82 // Run the event loop.
83 if (ACE_Proactor::run_event_loop(run_time) == -1)
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "(%t):%p.\n", "Worker::svc"),
86 1);
88 ACE_DEBUG ((LM_DEBUG, "Ending event loop\n"));
90 return 0;
93 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */