Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Multiple_Retry_Tests / Retry_On_Reply_Failure / client.cpp
blobbc53d39ce431a7532bacb58810af42b26fd7946d
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 (void);
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:
74 // The ORB reference
75 CORBA::ORB_var orb_;
76 // The exceptions caught.
77 int received_ex_kind_;
78 // The number of received exceptions.
79 int num_received_ex_;
80 // Flag indicating that the invocation was completed.
81 bool invocation_completed_;
82 // Flag for test completion. The result is
83 // collected before done.
84 bool done_;
87 int
88 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
90 try
92 CORBA::ORB_var orb =
93 CORBA::ORB_init (argc, argv);
95 if (parse_args (argc, argv) != 0)
96 return 1;
98 Worker worker (orb.in ());
100 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
101 nthreads) != 0)
102 ACE_ERROR_RETURN ((LM_ERROR,
103 "(%P|%t) Cannot activate worker threads\n"),
106 int timeout = 30;
107 int now = 0;
108 while (now < timeout &&
109 ((expect_ex_kind == 0 && !worker.invocation_completed ()) ||
110 (expect_ex_kind != 0 && expect_ex_kind != worker.received_ex_kind ()))
113 std::cout << "." << std::flush;
114 now += 1;
115 ACE_Time_Value tv (1, 0);
116 orb->run (tv);
118 ACE_ASSERT (now != 0);
121 std::cout << std::endl;
123 worker.done ();
125 CORBA::Object_var object =
126 orb->string_to_object (ior);
128 Simple_Server_var server =
129 Simple_Server::_narrow (object.in ());
131 server->shutdown ();
133 ACE_OS::sleep (1);
135 orb->destroy ();
137 worker.thr_mgr ()->wait ();
140 bool expect_no_ex =
141 expect_ex_kind == TAO::FOE_NON && worker.num_received_ex () == 0 && worker.invocation_completed ();
142 bool expect_ex_received =
143 expect_ex_kind == worker.received_ex_kind () && worker.num_received_ex () > 0 && !worker.invocation_completed ();
144 if (expect_no_ex || expect_ex_received)
146 ACE_DEBUG ((LM_DEBUG, "(%P|%t)client: test passed.\n"));
147 return 0;
149 else
151 ACE_DEBUG ((LM_ERROR, "(%P|%t)client: test failed.\n"));
152 return 1;
156 catch (const CORBA::Exception& ex)
158 ex._tao_print_exception ("Exception caught in main:");
159 return 1;
162 return 0;
165 // ****************************************************************
167 Worker::Worker (CORBA::ORB_ptr orb)
168 : orb_ (CORBA::ORB::_duplicate (orb)),
169 received_ex_kind_ (TAO::FOE_NON),
170 num_received_ex_ (0),
171 invocation_completed_ (false),
172 done_ (false)
177 Worker::svc (void)
181 CORBA::Object_var object =
182 this->orb_->string_to_object (ior);
184 Simple_Server_var server =
185 Simple_Server::_narrow (object.in ());
187 if (CORBA::is_nil (server.in ()))
189 ACE_ERROR ((LM_ERROR,
190 "Object reference <%s> is nil.\n",
191 ior));
192 return 0;
195 try {
196 CORBA::Boolean r =
197 server->test_is_a ("IDL:Foo:1.0");
199 this->invocation_completed_ = true;
201 if (r != 0)
202 ACE_DEBUG ((LM_DEBUG,
203 "(%P|%t) unexpected result = %d\n",
204 r));
207 catch (const CORBA::OBJECT_NOT_EXIST &)
209 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received OBJECT_NOT_EXIST \n"));
210 if (!this->done_)
212 ++ this->num_received_ex_;
213 received_ex_kind_ |= TAO::FOE_OBJECT_NOT_EXIST;
216 catch (const CORBA::COMM_FAILURE &)
218 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received COMM_FAILURE \n"));
219 if (!this->done_)
221 ++ this->num_received_ex_;
222 received_ex_kind_ |= TAO::FOE_COMM_FAILURE;
225 catch (const CORBA::TRANSIENT &)
227 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received TRANSIENT \n"));
228 if (!this->done_)
230 ++ this->num_received_ex_;
231 received_ex_kind_ |= TAO::FOE_TRANSIENT;
234 catch (const CORBA::INV_OBJREF &)
236 ACE_DEBUG ((LM_DEBUG, "(%P|%t)received INV_OBJREF \n"));
237 if (!this->done_)
239 ++ this->num_received_ex_;
240 received_ex_kind_ |= TAO::FOE_INV_OBJREF;
244 catch (const CORBA::Exception& ex)
246 ex._tao_print_exception ("Unexpected exception caught");
249 return 0;
254 Worker::received_ex_kind () const
256 return received_ex_kind_;
260 Worker::num_received_ex () const
262 return num_received_ex_;
265 bool
266 Worker::invocation_completed () const
268 return invocation_completed_;
271 void
272 Worker::done ()
274 done_ = true;