Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / ForwardOnceUponException / client.cpp
blob619fedad3b02a52c1665a60e7956b4c34b92454c
1 #include "testC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "ace/streams.h"
5 #include "tao/Invocation_Utils.h"
6 #include "ace/OS_NS_unistd.h"
8 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
9 int nthreads = 1;
10 int do_shutdown = 0;
11 static const ACE_TCHAR corbaloc_prefix[] = ACE_TEXT("corbaloc:");
12 int expect_ex_kind = TAO::FOE_NON;
13 int num_requests = 1;
14 int expect_num_ex = 1;
16 int
17 parse_args (int argc, ACE_TCHAR *argv[])
19 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:e:i:n:"));
20 int c;
22 while ((c = get_opts ()) != -1)
23 switch (c)
25 case 'x':
26 do_shutdown = 1;
27 break;
29 case 'k':
30 ior = get_opts.opt_arg ();
31 break;
33 case 'e':
34 expect_ex_kind = ACE_OS::atoi (get_opts.opt_arg ());
35 break;
37 case 'i':
38 num_requests = ACE_OS::atoi (get_opts.opt_arg ());
39 break;
41 case 'n':
42 expect_num_ex = ACE_OS::atoi (get_opts.opt_arg ());
43 break;
45 case '?':
46 default:
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "usage: %s "
49 "-k <ior> -x "
50 "-e <expected exception kind> "
51 "-i <num_requests> "
52 "-n <expected number of exceptions> "
53 "\n",
54 argv [0]),
55 -1);
58 if (ACE_OS::strncmp (ior,
59 corbaloc_prefix,
60 ACE_OS::strlen(corbaloc_prefix)) != 0)
61 return 1;
63 // Indicates successful parsing of the command line
64 return 0;
67 class Worker : public ACE_Task_Base
69 public:
70 Worker (CORBA::ORB_ptr orb);
71 // Constructor
73 // = The Task_Base methods
74 virtual int svc ();
76 // Caught any exception ?
77 int received_ex_kind () const;
79 // Return number of received exceptions.
80 int num_received_ex () const;
82 // Is test done ?
83 void done ();
85 private:
86 // The ORB reference
87 CORBA::ORB_var orb_;
88 // The exceptions caught.
89 int received_ex_kind_;
90 // The number of received exceptions.
91 int num_received_ex_;
92 // Flag for test completion. The result is
93 // collected before done.
94 bool done_;
97 int
98 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
102 CORBA::ORB_var orb =
103 CORBA::ORB_init (argc, argv);
105 if (parse_args (argc, argv) != 0)
106 return 1;
108 Worker worker (orb.in ());
110 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
111 nthreads) != 0)
112 ACE_ERROR_RETURN ((LM_ERROR,
113 "(%P|%t) Cannot activate worker threads\n"),
116 int timeout = 30;
117 int now = 0;
118 while (now < timeout
119 && ((expect_ex_kind != TAO::FOE_NON
120 && worker.received_ex_kind () != expect_ex_kind
121 && worker.num_received_ex () != expect_num_ex)
122 || expect_ex_kind == TAO::FOE_NON))
124 std::cout << ".";
125 now += 1;
126 ACE_Time_Value tv (1, 0);
127 orb->run (tv);
129 std::cout << std::endl;
131 worker.done ();
133 if (do_shutdown)
135 CORBA::Object_var object =
136 orb->string_to_object (ior);
138 Simple_Server_var server =
139 Simple_Server::_narrow (object.in ());
141 server->shutdown ();
144 ACE_OS::sleep (1);
146 orb->destroy ();
148 worker.thr_mgr ()->wait ();
150 if (worker.received_ex_kind () != expect_ex_kind
151 || worker.num_received_ex () != expect_num_ex)
153 ACE_ERROR_RETURN ((LM_ERROR,
154 ("(%P|%t)client: test failed - expected is different from received. "
155 "expected %d/%d received %d/%d.\n"),
156 expect_ex_kind, expect_num_ex, worker.received_ex_kind (), worker.num_received_ex()),
160 ACE_DEBUG ((LM_DEBUG, "(%P|%t)client: test passed.\n"));
162 catch (const CORBA::Exception& ex)
164 ex._tao_print_exception ("Exception caught in main:");
165 return 1;
168 return 0;
171 // ****************************************************************
173 Worker::Worker (CORBA::ORB_ptr orb)
174 : orb_ (CORBA::ORB::_duplicate (orb)),
175 received_ex_kind_ (TAO::FOE_NON),
176 num_received_ex_ (0),
177 done_ (false)
182 Worker::svc ()
186 CORBA::Object_var object =
187 this->orb_->string_to_object (ior);
189 Simple_Server_var server =
190 Simple_Server::_narrow (object.in ());
192 if (CORBA::is_nil (server.in ()))
194 ACE_ERROR ((LM_ERROR,
195 "Object reference <%s> is nil.\n",
196 ior));
197 return 0;
200 for (int i = 0; i < num_requests; ++i)
202 try {
203 CORBA::Boolean r =
204 server->test_is_a ("IDL:Foo:1.0");
206 if (r != 0)
207 ACE_DEBUG ((LM_DEBUG,
208 "(%P|%t) unexpected result = %d\n",
209 r));
211 catch (const CORBA::OBJECT_NOT_EXIST &)
213 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received OBJECT_NOT_EXIST \n"));
214 if (!this->done_)
216 ++ this->num_received_ex_;
217 received_ex_kind_ |= TAO::FOE_OBJECT_NOT_EXIST;
220 catch (const CORBA::COMM_FAILURE &)
222 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received COMM_FAILURE \n"));
223 if (!this->done_)
225 ++ this->num_received_ex_;
226 received_ex_kind_ |= TAO::FOE_COMM_FAILURE;
229 catch (const CORBA::TRANSIENT &)
231 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received TRANSIENT \n"));
232 if (!this->done_)
234 ++ this->num_received_ex_;
235 received_ex_kind_ |= TAO::FOE_TRANSIENT;
238 catch (const CORBA::INV_OBJREF &)
240 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received INV_OBJREF \n"));
241 if (!this->done_)
243 ++ this->num_received_ex_;
244 received_ex_kind_ |= TAO::FOE_INV_OBJREF;
249 catch (const CORBA::Exception& ex)
251 ex._tao_print_exception (
252 "Unexpected exception caught");
255 return 0;
260 Worker::received_ex_kind () const
262 return received_ex_kind_;
266 Worker::num_received_ex () const
268 return num_received_ex_;
271 void
272 Worker::done ()
274 done_ = true;