Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_2654_Regression / client.cpp
blobf4cefb3167439741e5de96b6247874ccceead103
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()
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 ();
47 public:
48 Worker( int ot);
49 ~Worker();
51 int svc ();
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);
99 catch (const CORBA::Exception &ex)
101 excep = true;
102 ACE_ERROR ((LM_ERROR,
103 "(%t) Exception caught: %s after %d invocations\n",
104 ex._name(), i));
105 i = 39;
108 if (!excep)
109 ACE_DEBUG ((LM_DEBUG, "(%t) Did all iterations\n"));
112 ACE_GUARD_RETURN (ACE_Mutex, ace_mon, this->lock_, 0);
113 --this->busy_threads_;
114 if (this->busy_threads_)
115 return 0;
120 this->orb_->shutdown();
122 catch (const CORBA::Exception &)
126 return 0;
130 init_callback (Worker &w)
132 CORBA::Object_var obj =
133 w.orb_->resolve_initial_references ("RootPOA");
135 if (CORBA::is_nil (obj.in ()))
136 ACE_ERROR_RETURN ((LM_ERROR,
137 " (%P|%t) Unable to initialize the POA.\n"),
140 PortableServer::POA_var root_poa =
141 PortableServer::POA::_narrow (obj.in ());
143 PortableServer::POAManager_var poa_manager =
144 root_poa->the_POAManager ();
146 // Policies for the childPOA to be created.
147 CORBA::PolicyList policies (1);
148 policies.length (1);
150 CORBA::Any pol;
151 pol <<= BiDirPolicy::BOTH;
152 policies[0] =
153 w.orb_->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
154 pol);
156 // Create POA as child of RootPOA with the above policies. This POA
157 // will receive request in the same connection in which it sent
158 // the request
159 PortableServer::POA_var child_poa =
160 root_poa->create_POA ("childPOA",
161 poa_manager.in (),
162 policies);
165 Callback_i *servant = new Callback_i;
166 PortableServer::ServantBase_var owner = servant;
168 PortableServer::ObjectId_var id = child_poa->activate_object (servant);
169 obj = child_poa->id_to_reference (id.in());
170 w.callback_ = Test::CallBack::_narrow(obj.in());
172 // Creation of childPOA is over. Destroy the Policy objects.
173 for (CORBA::ULong i = 0;
174 i < policies.length ();
175 ++i)
177 policies[i]->destroy ();
180 poa_manager->activate ();
182 return 0;
187 parse_args (int argc, ACE_TCHAR *argv[])
189 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
190 int c;
192 while ((c = get_opts ()) != -1)
193 switch (c)
195 case 'k':
196 ior = get_opts.opt_arg ();
197 break;
199 case '?':
200 default:
201 ACE_ERROR_RETURN ((LM_ERROR,
202 "usage: %s "
203 "-k <ior> "
204 "\n",
205 argv [0]),
206 -1);
208 // Indicates successful parsing of the command line
209 return 0;
213 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
215 static const int orb_threads = 5;
216 static const int total_threads = 15;
218 // It must be ensured that there are more total threads that there are
219 // that are dedicated to running the ORB.
220 ACE_ASSERT (total_threads > orb_threads);
222 Worker worker (orb_threads);
225 worker.orb_ =
226 CORBA::ORB_init (argc, argv, "test");
228 if (parse_args (argc, argv) != 0)
229 return 1;
231 ACE_DEBUG ((LM_DEBUG,"using ior = %s\n",ior));
233 CORBA::Object_var tmp = worker.orb_->string_to_object(ior);
235 if (CORBA::is_nil (tmp.in ()))
237 ACE_ERROR_RETURN ((LM_ERROR, "Invalid IOR.\n")
238 ,1);
240 worker.hello_ = Test::Hello::_narrow(tmp.in ());
242 if (CORBA::is_nil (worker.hello_.in ()))
244 ACE_ERROR_RETURN ((LM_ERROR,
245 "Nil Test::Hello reference <%s>\n",
246 ior),
250 // bool x = worker.hello_->_non_existent();
251 // ACE_DEBUG ((LM_DEBUG, "_non_existent returned %d\n",x));
254 // Set the Synch Scopes
256 CORBA::Any scope_as_any;
258 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Applying SYNC_NONE.\n"));
259 scope_as_any <<= Messaging::SYNC_NONE;
260 CORBA::PolicyList policies (1);
261 policies.length (1);
262 policies[0] =
263 worker.orb_->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
264 scope_as_any);
266 ACE_DEBUG ((LM_DEBUG,
267 "(%P|%t) - Applying Synch Scope at Object level.\n"));
268 tmp = tmp->_set_policy_overrides (policies, CORBA::SET_OVERRIDE);
269 policies[0]->destroy ();
272 worker.asynch_hello_ = Test::Hello::_narrow(tmp.in ());
274 if (CORBA::is_nil (worker.asynch_hello_.in ())) {
275 ACE_ERROR_RETURN ((LM_ERROR,
276 "Nil Test::Hello reference <%s>\n",
277 ior),
281 init_callback(worker);
283 catch (const CORBA::Exception &ex)
285 ACE_ERROR ((LM_ERROR, "Exception caught: %s\"%s\"\n"
286 , ex._name(), ex._rep_id ()));
287 return 1;
290 worker.activate (THR_NEW_LWP | THR_JOINABLE, total_threads);
291 worker.wait();
293 return 0;