Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2654_Regression / client.cpp
blobb01a90b4eeecdb9f50940e9343943c727a92093b
1 #include "HelloS.h"
3 #include "ace/OS_NS_time.h"
5 #include "tao/Messaging/Messaging.h"
7 #include "tao/Transport_Cache_Manager.h"
8 #include "tao/ORB_Core.h"
9 #include "tao/Thread_Lane_Resources.h"
10 #include "tao/Base_Transport_Property.h"
11 #include "tao/Stub.h"
12 #include "tao/Profile.h"
13 #include "tao/Transport.h"
14 #include "tao/BiDir_GIOP/BiDirGIOP.h"
16 #include "ace/Task.h"
17 #include "ace/Synch.h"
19 #include "ace/Get_Opt.h"
21 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
23 class Callback_i : public POA_Test::CallBack
25 public:
26 void method2(void)
28 ACE_DEBUG ((LM_DEBUG,"(%t) Callback_i::method2 called\n"));
32 class Worker : public ACE_Task_Base
34 public:
35 CORBA::ORB_var orb_;
36 Test::Hello_var hello_;
37 Test::Hello_var asynch_hello_;
38 Test::CallBack_var callback_;
40 private:
41 ACE_Mutex lock_;
42 int orb_threads_;
43 int busy_threads_;
44 CORBA::Short message_counter_;
46 int run_test (void);
47 public:
48 Worker( int ot);
49 ~Worker();
51 int svc (void);
54 Worker::Worker(int ot)
55 :orb_threads_ (ot),
56 busy_threads_ (0),
57 message_counter_ (0)
61 Worker::~Worker()
63 this->orb_->destroy();
66 int
67 Worker::svc()
70 ACE_GUARD_RETURN (ACE_Mutex, ace_mon, this->lock_, 0);
71 if (this->orb_threads_ > 0)
73 --this->orb_threads_;
74 hello_->set_callback(this->callback_.in());
75 ace_mon.release();
76 this->orb_->run();
77 return 0;
79 else
80 ++this->busy_threads_;
82 bool excep = false;
83 ACE_DEBUG ((LM_DEBUG, "(%t) starting loop\n"));
84 for (int i = 0; i < 40; i++)
86 try
88 CORBA::Short n = 0;
90 ACE_GUARD_RETURN (ACE_Mutex, ace_mon, this->lock_, 0);
91 n = ++this->message_counter_;
93 this->asynch_hello_->method (n);
95 ACE_Time_Value udelay(0,400);
96 struct timespec ts = udelay;
97 ACE_OS::nanosleep (&ts);
100 catch (const CORBA::Exception &ex)
102 excep = true;
103 ACE_ERROR ((LM_ERROR,
104 "(%t) Exception caught: %s after %d invocations\n",
105 ex._name(), i));
106 i = 39;
109 if (!excep)
110 ACE_DEBUG ((LM_DEBUG, "(%t) Did all iterations\n"));
113 ACE_GUARD_RETURN (ACE_Mutex, ace_mon, this->lock_, 0);
114 --this->busy_threads_;
115 if (this->busy_threads_)
116 return 0;
121 this->orb_->shutdown();
123 catch (const CORBA::Exception &)
127 return 0;
131 init_callback (Worker &w)
133 CORBA::Object_var obj =
134 w.orb_->resolve_initial_references ("RootPOA");
136 if (CORBA::is_nil (obj.in ()))
137 ACE_ERROR_RETURN ((LM_ERROR,
138 " (%P|%t) Unable to initialize the POA.\n"),
141 PortableServer::POA_var root_poa =
142 PortableServer::POA::_narrow (obj.in ());
144 PortableServer::POAManager_var poa_manager =
145 root_poa->the_POAManager ();
147 // Policies for the childPOA to be created.
148 CORBA::PolicyList policies (1);
149 policies.length (1);
151 CORBA::Any pol;
152 pol <<= BiDirPolicy::BOTH;
153 policies[0] =
154 w.orb_->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
155 pol);
157 // Create POA as child of RootPOA with the above policies. This POA
158 // will receive request in the same connection in which it sent
159 // the request
160 PortableServer::POA_var child_poa =
161 root_poa->create_POA ("childPOA",
162 poa_manager.in (),
163 policies);
166 Callback_i *servant = new Callback_i;
167 PortableServer::ServantBase_var owner = servant;
169 PortableServer::ObjectId_var id = child_poa->activate_object (servant);
170 obj = child_poa->id_to_reference (id.in());
171 w.callback_ = Test::CallBack::_narrow(obj.in());
173 // Creation of childPOA is over. Destroy the Policy objects.
174 for (CORBA::ULong i = 0;
175 i < policies.length ();
176 ++i)
178 policies[i]->destroy ();
181 poa_manager->activate ();
183 return 0;
188 parse_args (int argc, ACE_TCHAR *argv[])
190 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
191 int c;
193 while ((c = get_opts ()) != -1)
194 switch (c)
196 case 'k':
197 ior = get_opts.opt_arg ();
198 break;
200 case '?':
201 default:
202 ACE_ERROR_RETURN ((LM_ERROR,
203 "usage: %s "
204 "-k <ior> "
205 "\n",
206 argv [0]),
207 -1);
209 // Indicates successful parsing of the command line
210 return 0;
214 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
216 static const int orb_threads = 5;
217 static const int total_threads = 15;
219 // It must be ensured that there are more total threads that there are
220 // that are dedicated to running the ORB.
221 ACE_ASSERT (total_threads > orb_threads);
223 Worker worker (orb_threads);
226 worker.orb_ =
227 CORBA::ORB_init (argc, argv, "test");
229 if (parse_args (argc, argv) != 0)
230 return 1;
232 ACE_DEBUG ((LM_DEBUG,"using ior = %s\n",ior));
234 CORBA::Object_var tmp = worker.orb_->string_to_object(ior);
236 if (CORBA::is_nil (tmp.in ()))
238 ACE_ERROR_RETURN ((LM_ERROR, "Invalid IOR.\n")
239 ,1);
241 worker.hello_ = Test::Hello::_narrow(tmp.in ());
243 if (CORBA::is_nil (worker.hello_.in ()))
245 ACE_ERROR_RETURN ((LM_ERROR,
246 "Nil Test::Hello reference <%s>\n",
247 ior),
251 // bool x = worker.hello_->_non_existent();
252 // ACE_DEBUG ((LM_DEBUG, "_non_existent returned %d\n",x));
255 // Set the Synch Scopes
257 CORBA::Any scope_as_any;
259 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Applying SYNC_NONE.\n"));
260 scope_as_any <<= Messaging::SYNC_NONE;
261 CORBA::PolicyList policies (1);
262 policies.length (1);
263 policies[0] =
264 worker.orb_->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
265 scope_as_any);
267 ACE_DEBUG ((LM_DEBUG,
268 "(%P|%t) - Applying Synch Scope at Object level.\n"));
269 tmp = tmp->_set_policy_overrides (policies, CORBA::SET_OVERRIDE);
270 policies[0]->destroy ();
273 worker.asynch_hello_ = Test::Hello::_narrow(tmp.in ());
275 if (CORBA::is_nil (worker.asynch_hello_.in ())) {
276 ACE_ERROR_RETURN ((LM_ERROR,
277 "Nil Test::Hello reference <%s>\n",
278 ior),
282 init_callback(worker);
285 catch (const CORBA::Exception &ex)
287 ACE_ERROR ((LM_ERROR, "Exception caught: %s\"%s\"\n"
288 , ex._name(), ex._rep_id ()));
289 return 1;
292 worker.activate (THR_NEW_LWP | THR_JOINABLE, total_threads);
293 worker.wait();
295 return 0;