Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Hang_Shutdown / client.cpp
blobe23098b21c0a8ce590ee71b9ed275183047b0f36
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 ()
62 try
64 this->h_->send_stuff ("Testing",
65 false);
67 this->h_->send_stuff ("Testing",
68 false);
70 this->h_->send_stuff ("Testing",
71 true);
73 catch (const CORBA::COMM_FAILURE& f)
75 ACE_UNUSED_ARG (f);
76 ACE_DEBUG ((LM_DEBUG,
77 "(%P|%t) Caught COMM_FAILURE Exception\n"));
79 ACE_DEBUG ((LM_DEBUG,
80 "(%P|%t) This is expected\n"));
82 return 0;
84 catch (const CORBA::Exception& ex)
86 ex._tao_print_exception ("Caught CORBA Exception\n");
88 ACE_ERROR ((LM_ERROR,
89 "(%P|%t) Error in test\n"));
91 return -1;
93 catch (...)
95 ACE_DEBUG ((LM_DEBUG,
96 "(%P|%t) Caught a C++ exception\n"));
97 ACE_ERROR ((LM_ERROR,
98 "(%P|%t) Error in test\n"));
100 return -1;
103 return 0;
106 private:
107 Hang_var h_;
110 class Shutdown_Task : public ACE_Task_Base
112 public:
113 Shutdown_Task (CORBA::ORB_ptr o)
114 : o_ (CORBA::ORB::_duplicate (o))
117 virtual int svc ()
121 ACE_DEBUG ((LM_DEBUG,
122 "(%P|%t) Calling shutdown\n"));
124 // Just wait for the main thread to start sending out
125 // messages
126 ACE_OS::sleep (4);
128 // Start the timer
129 profile_timer.start ();
131 this->o_->shutdown (blocked);
133 // Stop the timer
134 profile_timer.stop ();
136 // Get the elampsed time
137 ACE_Profile_Timer::ACE_Elapsed_Time el;
138 profile_timer.elapsed_time (el);
140 // The elapsed time is in secs
141 if (el.real_time > 1)
143 ACE_ERROR ((LM_ERROR,
144 "(%P|%t) ERROR: Too long to shutdown\n"));
146 return 0;
149 catch (...)
151 ACE_DEBUG ((LM_DEBUG,
152 "(%P|%t) Caught exception during shutdown\n"));
154 ACE_ERROR ((LM_ERROR,
155 "(%P|%t) Error in test\n"));
156 return -1;
159 ACE_DEBUG ((LM_DEBUG,
160 "(%P|%t) Returning from shutdown\n"));
161 return 0;
163 private:
164 CORBA::ORB_var o_;
167 static int
168 try_main (int argc, ACE_TCHAR *argv[])
172 CORBA::ORB_var orb =
173 CORBA::ORB_init (argc, argv);
175 if (parse_args (argc, argv) == false)
176 return -1;
178 CORBA::Object_var tmp =
179 orb->string_to_object (ior);
181 Hang_var test =
182 Hang::_narrow (tmp.in ());
184 if (CORBA::is_nil (test.in ()))
186 ACE_ERROR_RETURN ((LM_DEBUG,
187 "Nil test reference <%s>\n",
188 ior),
192 Client_Task ct (test.in ());
194 if (ct.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
195 ACE_ERROR_RETURN ((LM_ERROR,
196 "Cannot activate client threads\n"),
199 ACE_DEBUG ((LM_DEBUG,
200 "(%P|%t) Activating shutdown thread\n"));
202 Shutdown_Task st (orb.in ());
204 if (st.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
205 ACE_ERROR_RETURN ((LM_ERROR,
206 "Cannot activate shutdown threads\n"),
209 ACE_Thread_Manager::instance ()->wait ();
211 orb->destroy ();
213 catch (const CORBA::Exception& ex)
215 ex._tao_print_exception ("CORBA Exception caught\n");
216 ACE_ERROR ((LM_ERROR,
217 "(%P|%t) Eror in test\n"));
218 return -1;
220 catch (...)
222 ACE_DEBUG ((LM_DEBUG,
223 "(%P|%t) Error in test\n"));
224 return -1;
227 return 0;
232 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
234 return Test::try_main (argc, argv);