1 //=============================================================================
3 * @file Reactor_Exceptions_Test.cpp
5 * This is a test that makes sure the <ACE_Reactor> works
6 * correctly in the face of C++ exceptions and threads.
8 * @author Luca Priorelli <lucapri@mbox.vol.it> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
10 //=============================================================================
12 #include "test_config.h"
13 #include "ace/Reactor.h"
14 #include "ace/SOCK_Dgram.h"
15 #include "ace/INET_Addr.h"
16 #include "ace/Thread_Manager.h"
17 #include "ace/Select_Reactor.h"
19 // Just need a simple exception class.
26 ACE_TEXT ("(%P|%t) throw exception\n")));
30 class My_Handler
: public ACE_Event_Handler
, public ACE_SOCK_Dgram
33 My_Handler (const ACE_INET_Addr
&local_addr
);
35 ACE_HANDLE
get_handle () const override
;
36 int handle_input (ACE_HANDLE handle
) override
;
39 My_Handler::My_Handler (const ACE_INET_Addr
&local_addr
)
40 : ACE_SOCK_Dgram (local_addr
)
45 My_Handler::get_handle () const
47 return ACE_SOCK_Dgram::get_handle ();
51 My_Handler::handle_input (ACE_HANDLE
)
53 ACE_TCHAR buf
[BUFSIZ
];
54 ACE_INET_Addr from_addr
;
57 ACE_TEXT ("Activity occurred on handle %d!\n"),
58 ACE_SOCK_Dgram::get_handle ()));
60 ssize_t n
= ACE_SOCK_Dgram::recv (buf
,
66 ACE_TEXT ("handle_input")));
69 ACE_TEXT ("got buf = %s\n"),
76 class My_Reactor
: public ACE_Select_Reactor
79 int handle_events (ACE_Time_Value
*max_wait_time
) override
85 ret
= ACE_Select_Reactor::handle_events (max_wait_time
);
90 ACE_TEXT (" (%t) catch exception\n")));
92 // do your thing, etc.
97 int handle_events (ACE_Time_Value
&max_wait_time
) override
99 return this->handle_events (&max_wait_time
);
103 #if defined (ACE_HAS_THREADS)
107 ACE_Reactor::instance ()->owner (ACE_OS::thr_self ());
110 if (ACE_Reactor::instance ()->handle_events () == -1)
112 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" (%t) exception return\n")));
118 #endif /* ACE_HAS_THREADS */
121 run_main (int argc
, ACE_TCHAR
*argv
[])
123 ACE_START_TEST (ACE_TEXT ("Reactor_Exceptions_Test"));
125 ACE_DEBUG ((LM_DEBUG
,
126 ACE_TEXT ("Starting tracing\n")));
128 u_short port
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : ACE_DEFAULT_SERVER_PORT
;
130 ACE_INET_Addr
local_addr (port
);
131 ACE_INET_Addr
remote_addr (port
, ACE_LOCALHOST
, PF_INET
);
132 ACE_Reactor
reactor (new My_Reactor
, true);
134 ACE_Reactor::instance (&reactor
);
135 ACE_Thread_Manager
*thr_mgr
= ACE_Thread_Manager::instance ();
138 // Make sure handler gets cleaned up before reactor by putting it in its
140 My_Handler
handler (local_addr
);
142 if (ACE_Reactor::instance ()->register_handler
144 ACE_Event_Handler::READ_MASK
) == -1)
145 ACE_ERROR_RETURN ((LM_ERROR
,
147 ACE_TEXT ("register_handler")),
150 #if defined (ACE_HAS_THREADS)
151 thr_mgr
->spawn (ACE_THR_FUNC (worker
));
153 // Need to figure out how to implement this test.
155 ACE_TEXT ("threads not supported on this platform\n")));
156 #endif /* ACE_HAS_THREADS */
158 ACE_SOCK_Dgram
dgram (ACE_sap_any_cast (ACE_INET_Addr
&), PF_INET
);
160 for (size_t i
= 0; i
< ACE_MAX_ITERATIONS
; i
++)
161 dgram
.send (ACE_TEXT ("Hello"),
162 sizeof (ACE_TEXT ("Hello")),
164 // Barrier to wait for the other thread to return.
171 ACE_DEBUG ((LM_DEBUG
,
172 ACE_TEXT (" (%t) exiting main\n")));