ACE+TAO-6_5_17
[ACE_TAO.git] / TAO / tests / Hang_Shutdown / client.cpp
blobd5f0e3a45c2bd4be2b047f13e401cb6fab5a50ed
1 #include "TestC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "ace/Profile_Timer.h"
5 #include "ace/OS_NS_unistd.h"
7 namespace Test
9 const ACE_TCHAR *ior = ACE_TEXT("file://server.ior");
11 ACE_Profile_Timer profile_timer;
12 bool blocked = false;
14 bool
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("b:k:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'b':
25 int tmp =
26 ACE_OS::atoi (get_opts.opt_arg ());
28 if (tmp)
29 blocked = true;
30 else
31 blocked = false;
33 break;
34 case 'k':
36 ior = get_opts.opt_arg ();
38 break;
39 case '?':
40 default:
41 ACE_ERROR_RETURN ((LM_ERROR,
42 "usage: %s "
43 "-k <ior> "
44 "\n",
45 argv [0]),
46 false);
49 // Indicates successful parsing of the command line
50 return true;
53 class Client_Task : public ACE_Task_Base
55 public:
56 Client_Task (Hang_ptr h)
57 : h_ (Hang::_duplicate (h))
60 virtual int svc (void)
63 try
65 this->h_->send_stuff ("Testing",
66 false);
68 this->h_->send_stuff ("Testing",
69 false);
71 this->h_->send_stuff ("Testing",
72 true);
74 catch (const CORBA::COMM_FAILURE& f)
76 ACE_UNUSED_ARG (f);
77 ACE_DEBUG ((LM_DEBUG,
78 "(%P|%t) Caught COMM_FAILURE Exception\n"));
80 ACE_DEBUG ((LM_DEBUG,
81 "(%P|%t) This is expected\n"));
83 return 0;
85 catch (const CORBA::Exception& ex)
87 ex._tao_print_exception ("Caught CORBA Exception\n");
89 ACE_ERROR ((LM_ERROR,
90 "(%P|%t) Error in test\n"));
92 return -1;
94 catch (...)
96 ACE_DEBUG ((LM_DEBUG,
97 "(%P|%t) Caught a C++ exception\n"));
98 ACE_ERROR ((LM_ERROR,
99 "(%P|%t) Error in test\n"));
101 return -1;
104 return 0;
107 private:
108 Hang_var h_;
111 class Shutdown_Task : public ACE_Task_Base
113 public:
114 Shutdown_Task (CORBA::ORB_ptr o)
115 : o_ (CORBA::ORB::_duplicate (o))
118 virtual int svc (void)
123 ACE_DEBUG ((LM_DEBUG,
124 "(%P|%t) Calling shutdown\n"));
126 // Just wait for the main thread to start sending out
127 // messages
128 ACE_OS::sleep (4);
130 // Start the timer
131 profile_timer.start ();
133 this->o_->shutdown (blocked);
135 // Stop the timer
136 profile_timer.stop ();
138 // Get the elampsed time
139 ACE_Profile_Timer::ACE_Elapsed_Time el;
140 profile_timer.elapsed_time (el);
142 // The elapsed time is in secs
143 if (el.real_time > 1)
145 ACE_ERROR ((LM_ERROR,
146 "(%P|%t) ERROR: Too long to shutdown\n"));
148 return 0;
151 catch (...)
153 ACE_DEBUG ((LM_DEBUG,
154 "(%P|%t) Caught exception during shutdown\n"));
156 ACE_ERROR ((LM_ERROR,
157 "(%P|%t) Error in test\n"));
158 return -1;
161 ACE_DEBUG ((LM_DEBUG,
162 "(%P|%t) Returning from shutdown\n"));
163 return 0;
165 private:
166 CORBA::ORB_var o_;
169 static int
170 try_main (int argc, ACE_TCHAR *argv[])
175 CORBA::ORB_var orb =
176 CORBA::ORB_init (argc, argv);
178 if (parse_args (argc, argv) == false)
179 return -1;
181 CORBA::Object_var tmp =
182 orb->string_to_object (ior);
184 Hang_var test =
185 Hang::_narrow (tmp.in ());
187 if (CORBA::is_nil (test.in ()))
189 ACE_ERROR_RETURN ((LM_DEBUG,
190 "Nil test reference <%s>\n",
191 ior),
195 Client_Task ct (test.in ());
197 if (ct.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
198 ACE_ERROR_RETURN ((LM_ERROR,
199 "Cannot activate client threads\n"),
202 ACE_DEBUG ((LM_DEBUG,
203 "(%P|%t) Activating shutdown thread\n"));
205 Shutdown_Task st (orb.in ());
207 if (st.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 "Cannot activate shutdown threads\n"),
212 ACE_Thread_Manager::instance ()->wait ();
214 orb->destroy ();
217 catch (const CORBA::Exception& ex)
219 ex._tao_print_exception ("CORBA Exception caught\n");
220 ACE_ERROR ((LM_ERROR,
221 "(%P|%t) Eror in test\n"));
222 return -1;
224 catch (...)
226 ACE_DEBUG ((LM_DEBUG,
227 "(%P|%t) Error in test\n"));
228 return -1;
231 return 0;
236 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
238 return Test::try_main (argc, argv);