Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / ForwardOnceUponException / client.cpp
blob2a0223db73b556a0e3e6d4df9c8dc910a66ab0c7
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 (void);
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:
87 // The ORB reference
88 CORBA::ORB_var orb_;
89 // The exceptions caught.
90 int received_ex_kind_;
91 // The number of received exceptions.
92 int num_received_ex_;
93 // Flag for test completion. The result is
94 // collected before done.
95 bool done_;
98 int
99 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
103 CORBA::ORB_var orb =
104 CORBA::ORB_init (argc, argv);
106 if (parse_args (argc, argv) != 0)
107 return 1;
109 Worker worker (orb.in ());
111 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
112 nthreads) != 0)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "(%P|%t) Cannot activate worker threads\n"),
117 int timeout = 30;
118 int now = 0;
119 while (now < timeout
120 && ((expect_ex_kind != TAO::FOE_NON
121 && worker.received_ex_kind () != expect_ex_kind
122 && worker.num_received_ex () != expect_num_ex)
123 || expect_ex_kind == TAO::FOE_NON))
125 std::cout << ".";
126 now += 1;
127 ACE_Time_Value tv (1, 0);
128 orb->run (tv);
130 std::cout << std::endl;
132 worker.done ();
134 if (do_shutdown)
136 CORBA::Object_var object =
137 orb->string_to_object (ior);
139 Simple_Server_var server =
140 Simple_Server::_narrow (object.in ());
142 server->shutdown ();
145 ACE_OS::sleep (1);
147 orb->destroy ();
149 worker.thr_mgr ()->wait ();
151 if (worker.received_ex_kind () != expect_ex_kind
152 || worker.num_received_ex () != expect_num_ex)
154 ACE_ERROR_RETURN ((LM_ERROR,
155 ("(%P|%t)client: test failed - expected is different from received. "
156 "expected %d/%d received %d/%d.\n"),
157 expect_ex_kind, expect_num_ex, worker.received_ex_kind (), worker.num_received_ex()),
161 ACE_DEBUG ((LM_DEBUG, "(%P|%t)client: test passed.\n"));
163 catch (const CORBA::Exception& ex)
165 ex._tao_print_exception ("Exception caught in main:");
166 return 1;
169 return 0;
172 // ****************************************************************
174 Worker::Worker (CORBA::ORB_ptr orb)
175 : orb_ (CORBA::ORB::_duplicate (orb)),
176 received_ex_kind_ (TAO::FOE_NON),
177 num_received_ex_ (0),
178 done_ (false)
183 Worker::svc (void)
187 CORBA::Object_var object =
188 this->orb_->string_to_object (ior);
190 Simple_Server_var server =
191 Simple_Server::_narrow (object.in ());
193 if (CORBA::is_nil (server.in ()))
195 ACE_ERROR ((LM_ERROR,
196 "Object reference <%s> is nil.\n",
197 ior));
198 return 0;
201 for (int i = 0; i < num_requests; ++i)
203 try {
204 CORBA::Boolean r =
205 server->test_is_a ("IDL:Foo:1.0");
207 if (r != 0)
208 ACE_DEBUG ((LM_DEBUG,
209 "(%P|%t) unexpected result = %d\n",
210 r));
213 catch (const CORBA::OBJECT_NOT_EXIST &)
215 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received OBJECT_NOT_EXIST \n"));
216 if (!this->done_)
218 ++ this->num_received_ex_;
219 received_ex_kind_ |= TAO::FOE_OBJECT_NOT_EXIST;
222 catch (const CORBA::COMM_FAILURE &)
224 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received COMM_FAILURE \n"));
225 if (!this->done_)
227 ++ this->num_received_ex_;
228 received_ex_kind_ |= TAO::FOE_COMM_FAILURE;
231 catch (const CORBA::TRANSIENT &)
233 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received TRANSIENT \n"));
234 if (!this->done_)
236 ++ this->num_received_ex_;
237 received_ex_kind_ |= TAO::FOE_TRANSIENT;
240 catch (const CORBA::INV_OBJREF &)
242 //ACE_DEBUG ((LM_DEBUG, "(%P|%t)received INV_OBJREF \n"));
243 if (!this->done_)
245 ++ this->num_received_ex_;
246 received_ex_kind_ |= TAO::FOE_INV_OBJREF;
251 catch (const CORBA::Exception& ex)
253 ex._tao_print_exception (
254 "Unexpected exception caught");
257 return 0;
262 Worker::received_ex_kind () const
264 return received_ex_kind_;
268 Worker::num_received_ex () const
270 return num_received_ex_;
273 void
274 Worker::done ()
276 done_ = true;