Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Multiple_Retry_Tests / Retry_On_Reply_Failure / client.cpp
blob9865948dd48c71c22c6ccc6fd9bc7ba18988cdde
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 static const ACE_TCHAR corbaloc_prefix[] = ACE_TEXT("corbaloc:");
11 int expect_ex_kind = TAO::FOE_NON;
12 int num_requests = 1;
14 int
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:e:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'k':
24 ior = get_opts.opt_arg ();
25 break;
27 case 'e':
28 expect_ex_kind = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-k <ior>"
36 "-e <expected exception kind> "
37 "\n",
38 argv [0]),
39 -1);
42 if (ACE_OS::strncmp (ior,
43 corbaloc_prefix,
44 ACE_OS::strlen(corbaloc_prefix)) != 0)
45 return 1;
47 // Indicates successful parsing of the command line
48 return 0;
51 class Worker : public ACE_Task_Base
53 public:
54 Worker (CORBA::ORB_ptr orb);
55 // Constructor
57 // = The Task_Base methods
58 virtual int svc ();
60 // Caught any exception ?
61 int received_ex_kind () const;
63 // Return number of received exceptions.
64 int num_received_ex () const;
66 // Indicate if the invocation completed.
67 bool invocation_completed () const;
69 // Is test done ?
70 void done ();
72 private:
73 // The ORB reference
74 CORBA::ORB_var orb_;
75 // The exceptions caught.
76 int received_ex_kind_;
77 // The number of received exceptions.
78 int num_received_ex_;
79 // Flag indicating that the invocation was completed.
80 bool invocation_completed_;
81 // Flag for test completion. The result is
82 // collected before done.
83 bool done_;
86 int
87 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
89 try
91 CORBA::ORB_var orb =
92 CORBA::ORB_init (argc, argv);
94 if (parse_args (argc, argv) != 0)
95 return 1;
97 Worker worker (orb.in ());
99 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
100 nthreads) != 0)
101 ACE_ERROR_RETURN ((LM_ERROR,
102 "(%P|%t) Cannot activate worker threads\n"),
105 int timeout = 30;
106 int now = 0;
107 while (now < timeout &&
108 ((expect_ex_kind == 0 && !worker.invocation_completed ()) ||
109 (expect_ex_kind != 0 && expect_ex_kind != worker.received_ex_kind ()))
112 std::cout << "." << std::flush;
113 now += 1;
114 ACE_Time_Value tv (1, 0);
115 orb->run (tv);
117 ACE_ASSERT (now != 0);
120 std::cout << std::endl;
122 worker.done ();
124 CORBA::Object_var object =
125 orb->string_to_object (ior);
127 Simple_Server_var server =
128 Simple_Server::_narrow (object.in ());
130 server->shutdown ();
132 ACE_OS::sleep (1);
134 orb->destroy ();
136 worker.thr_mgr ()->wait ();
139 bool expect_no_ex =
140 expect_ex_kind == TAO::FOE_NON && worker.num_received_ex () == 0 && worker.invocation_completed ();
141 bool expect_ex_received =
142 expect_ex_kind == worker.received_ex_kind () && worker.num_received_ex () > 0 && !worker.invocation_completed ();
143 if (expect_no_ex || expect_ex_received)
145 ACE_DEBUG ((LM_DEBUG, "(%P|%t)client: test passed.\n"));
146 return 0;
148 else
150 ACE_DEBUG ((LM_ERROR, "(%P|%t)client: test failed.\n"));
151 return 1;
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Exception caught in main:");
158 return 1;
161 return 0;
164 // ****************************************************************
166 Worker::Worker (CORBA::ORB_ptr orb)
167 : orb_ (CORBA::ORB::_duplicate (orb)),
168 received_ex_kind_ (TAO::FOE_NON),
169 num_received_ex_ (0),
170 invocation_completed_ (false),
171 done_ (false)
176 Worker::svc ()
180 CORBA::Object_var object =
181 this->orb_->string_to_object (ior);
183 Simple_Server_var server =
184 Simple_Server::_narrow (object.in ());
186 if (CORBA::is_nil (server.in ()))
188 ACE_ERROR ((LM_ERROR,
189 "Object reference <%s> is nil.\n",
190 ior));
191 return 0;
194 try {
195 CORBA::Boolean r =
196 server->test_is_a ("IDL:Foo:1.0");
198 this->invocation_completed_ = true;
200 if (r != 0)
201 ACE_DEBUG ((LM_DEBUG,
202 "(%P|%t) unexpected result = %d\n",
203 r));
205 catch (const CORBA::OBJECT_NOT_EXIST &)
207 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received OBJECT_NOT_EXIST \n"));
208 if (!this->done_)
210 ++ this->num_received_ex_;
211 received_ex_kind_ |= TAO::FOE_OBJECT_NOT_EXIST;
214 catch (const CORBA::COMM_FAILURE &)
216 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received COMM_FAILURE \n"));
217 if (!this->done_)
219 ++ this->num_received_ex_;
220 received_ex_kind_ |= TAO::FOE_COMM_FAILURE;
223 catch (const CORBA::TRANSIENT &)
225 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received TRANSIENT \n"));
226 if (!this->done_)
228 ++ this->num_received_ex_;
229 received_ex_kind_ |= TAO::FOE_TRANSIENT;
232 catch (const CORBA::INV_OBJREF &)
234 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received INV_OBJREF \n"));
235 if (!this->done_)
237 ++ this->num_received_ex_;
238 received_ex_kind_ |= TAO::FOE_INV_OBJREF;
242 catch (const CORBA::Exception& ex)
244 ex._tao_print_exception ("Unexpected exception caught");
247 return 0;
252 Worker::received_ex_kind () const
254 return received_ex_kind_;
258 Worker::num_received_ex () const
260 return num_received_ex_;
263 bool
264 Worker::invocation_completed () const
266 return invocation_completed_;
269 void
270 Worker::done ()
272 done_ = true;