Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Bug_2953_Regression / server.cpp
blob3b5c22692a51625c24c21a31b658200571926726
1 // -*- C++ -*-
2 #include "tao/ORB.h"
3 #include "tao/Object.h"
4 #include "tao/SystemException.h"
5 #include "tao/RTCORBA/RTCORBA.h"
6 #include "tao/RTPortableServer/RTPortableServer.h"
7 #include "tao/Utils/PolicyList_Destroyer.h"
8 #include "tao/Protocols_Hooks.h"
9 #include "tao/ORB_Core.h"
10 #include "ace/Log_Msg.h"
11 #include "ace/Thread.h"
12 #include "ace/Get_Opt.h"
14 #include "TestS.h"
15 #include "Client_Task.h"
17 class Test_i : public virtual POA_Test::Hello
19 public:
20 Test_i(CORBA::ORB_ptr orb, Client_Task* t) : orb_(CORBA::ORB::_duplicate(orb)), task_ (t)
24 virtual ~Test_i()
28 char * get_string()
30 return CORBA::string_dup("hello");
33 //FUZZ: disable check_for_lack_ACE_OS
34 void shutdown()
36 task_->terminate_loop ();
37 orb_ = CORBA::ORB::_nil ();
39 //FUZZ: enable check_for_lack_ACE_OS
41 private:
42 CORBA::ORB_var orb_;
43 Client_Task* task_;
46 const ACE_TCHAR *ior_a_file = ACE_TEXT("iorA.ior");
47 const ACE_TCHAR *ior_b_file = ACE_TEXT("iorB.ior");
49 int
50 parse_args (int argc, ACE_TCHAR *argv[])
52 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:b:"));
53 int c;
55 while ((c = get_opts ()) != -1)
56 switch (c)
58 case 'a':
59 ior_a_file = get_opts.optarg;
60 break;
62 case 'b':
63 ior_b_file = get_opts.optarg;
64 break;
66 case '?':
67 default:
68 ACE_ERROR_RETURN ((LM_ERROR,
69 "usage: %s "
70 "-a <iorAfile>"
71 "-b <iorBfile>"
72 "\n",
73 argv [0]),
74 -1);
76 // Indicates successful parsing of the command line
77 return 0;
80 RTCORBA::ThreadpoolId
81 createThreadpool(CORBA::ORB_ptr orb, RTCORBA::RTORB_ptr rtorb, CORBA::ULong nthreads)
83 CORBA::Object_var obj = orb->resolve_initial_references("RTCurrent");
84 RTCORBA::Current_var rtcurrent = RTCORBA::Current::_narrow(obj.in());
86 CORBA::ULong const stacksize = 0; // default
87 CORBA::ULong const minThreads = 1;
88 CORBA::ULong const dynamicThreads = nthreads - minThreads;
90 TAO_Protocols_Hooks *tph =
91 orb->orb_core ()->get_protocols_hooks ();
93 RTCORBA::Priority dfltThreadPrio;
95 if (!(tph->get_thread_implicit_CORBA_priority (dfltThreadPrio) == 0))
97 throw CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);
100 CORBA::Boolean const doBuffering = false;
101 CORBA::ULong maxBufRequests = 0;
102 CORBA::ULong maxReqBufSize = 0;
104 RTCORBA::ThreadpoolId poolId;
107 poolId = rtorb->create_threadpool(stacksize,
108 minThreads,
109 dynamicThreads,
110 dfltThreadPrio,
111 doBuffering,
112 maxBufRequests,
113 maxReqBufSize);
115 catch (const CORBA::SystemException &x)
117 x._tao_print_exception ("creating thread pool");
118 throw;
120 return poolId;
123 void destroyThreadpool(RTCORBA::RTORB_ptr rtorb, RTCORBA::ThreadpoolId id)
125 rtorb->destroy_threadpool(id);
128 RTCORBA::RTORB_ptr getRTORB(CORBA::ORB_ptr orb, const char *id)
130 CORBA::Object_var obj = orb->resolve_initial_references("RTORB");
131 RTCORBA::RTORB_ptr rtorb = RTCORBA::RTORB::_narrow(obj.in());
132 if (CORBA::is_nil(rtorb))
134 ACE_ERROR ((LM_ERROR,
135 "Failed getting RTORB for orb <%C>\n",
136 id));
138 return rtorb;
141 PortableServer::POA_ptr getRootPoa(CORBA::ORB_ptr orb, const char *id)
143 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
144 PortableServer::POA_ptr poa = PortableServer::POA::_narrow(obj.in());
145 if (CORBA::is_nil(poa))
147 ACE_ERROR ((LM_ERROR,
148 "Failed getting RootPOA for orb <%C>\n",
149 id));
151 return poa;
154 char*
155 addServant(
156 CORBA::ORB_ptr orb,
157 RTCORBA::RTORB_ptr rtorb,
158 PortableServer::POA_ptr poa,
159 PortableServer::ServantBase *servant,
160 RTCORBA::ThreadpoolId& id,
161 short nthreads)
163 id = createThreadpool(orb, rtorb, nthreads);
164 ACE_DEBUG ((LM_DEBUG,
165 "Created threadpool with id <%d>\n",
166 id));
168 TAO::Utils::PolicyList_Destroyer policies (8);
169 policies.length(8);
170 CORBA::ULong i = 0;
172 policies[i++] = poa->create_id_assignment_policy(PortableServer::SYSTEM_ID);
173 policies[i++] = poa->create_id_uniqueness_policy(PortableServer::UNIQUE_ID);
174 policies[i++] = poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
175 policies[i++] = poa->create_lifespan_policy(PortableServer::TRANSIENT);
176 policies[i++] = poa->create_request_processing_policy(PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY);
177 policies[i++] = poa->create_servant_retention_policy(PortableServer::RETAIN);
178 policies[i++] = poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
179 policies[i++] = rtorb->create_threadpool_policy(id);
181 PortableServer::ObjectId_var dummy = poa->activate_object(servant);
182 CORBA::Object_var ref = poa->servant_to_reference(servant);
183 CORBA::String_var str = orb->object_to_string(ref.in ());
185 return str._retn();
188 void
189 removeServant(
190 RTCORBA::RTORB_ptr rtorb,
191 PortableServer::POA_ptr poa,
192 PortableServer::ServantBase *servant,
193 RTCORBA::ThreadpoolId id)
195 PortableServer::ObjectId_var oid = poa->servant_to_id(servant);
196 poa->deactivate_object(oid.in());
197 rtorb->destroy_threadpool(id);
198 ACE_DEBUG ((LM_DEBUG,
199 "Destroyed threadpool with id <%d>\n",
200 id));
203 void shutdownORB(CORBA::ORB_ptr orb, const char * orbid)
205 orb->shutdown(true);
206 ACE_DEBUG ((LM_DEBUG,
207 "ORB <%C> is shutdown\n",
208 orbid));
210 orb->destroy();
211 ACE_DEBUG ((LM_DEBUG,
212 "ORB <%C> is destroyed\n",
213 orbid));
216 int write_iorfile (const ACE_TCHAR* ior_output_file, const char* ior)
218 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
219 if (output_file == 0)
220 ACE_ERROR_RETURN ((LM_ERROR,
221 "Cannot open output file for writing IOR: %s\n",
222 ior_output_file),
224 ACE_OS::fprintf (output_file, "%s", ior);
225 ACE_OS::fclose (output_file);
227 return 0;
232 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
236 const char *orbidA = "orbidA";
237 const char *orbidB = "orbidB";
239 PortableServer::ServantBase *implA = 0;
240 PortableServer::ServantBase *implB = 0;
241 RTCORBA::ThreadpoolId tpidA = 0;
242 RTCORBA::ThreadpoolId tpidB = 0;
244 // server - write our iors to two files
245 CORBA::ORB_var orbA = CORBA::ORB_init(argc, argv, orbidA);
246 CORBA::ORB_var orbB = CORBA::ORB_init(argc, argv, orbidB);
247 RTCORBA::RTORB_var rtorbA = getRTORB(orbA.in (), orbidA);
248 RTCORBA::RTORB_var rtorbB = getRTORB(orbB.in (), orbidB);
250 PortableServer::POA_var rootPoaA = getRootPoa(orbA.in (), orbidA);
251 PortableServer::POA_var rootPoaB = getRootPoa(orbB.in (), orbidB);
253 PortableServer::POAManager_var managerA = rootPoaA->the_POAManager();
254 managerA->activate();
255 PortableServer::POAManager_var managerB = rootPoaB->the_POAManager();
256 managerB->activate();
258 Client_Task client_taskA (orbA.in ());
259 Client_Task client_taskB (orbB.in ());
261 implA = new Test_i(orbA.in (), &client_taskA);
262 PortableServer::ServantBase_var safeA (implA);
263 implB = new Test_i(orbB.in (), &client_taskB);
264 PortableServer::ServantBase_var safeB (implB);
266 CORBA::String_var iorA = addServant(orbA.in (), rtorbA.in (), rootPoaA.in (), implA, tpidA, 3);
267 CORBA::String_var iorB = addServant(orbB.in (), rtorbB.in (), rootPoaB.in (), implB, tpidB, 3);
269 if (parse_args (argc, argv) != 0)
270 return 1;
272 if (write_iorfile(ior_a_file, iorA.in ()) == 1)
273 return 1;
275 if (write_iorfile(ior_b_file, iorB.in ()) == 1)
276 return 1;
278 // colocated calls work fine
279 CORBA::Object_var objA = orbA->string_to_object(iorA.in ());
280 Test::Hello_var helloA(Test::Hello::_narrow(objA.in ()));
281 CORBA::String_var resA = helloA->get_string();
283 CORBA::Object_var objB = orbB->string_to_object(iorB.in ());
284 Test::Hello_var helloB(Test::Hello::_narrow(objB.in ()));
285 CORBA::String_var resB = helloB->get_string();
287 ACE_DEBUG ((LM_DEBUG, "server got resA: %C and resB: %C\n", resA.in (), resB.in ()));
289 if (client_taskA.activate (THR_NEW_LWP | THR_JOINABLE, 1) == -1)
291 ACE_ERROR ((LM_ERROR, "Error activating client task\n"));
293 if (client_taskB.activate (THR_NEW_LWP | THR_JOINABLE, 1) == -1)
295 ACE_ERROR ((LM_ERROR, "Error activating client task\n"));
297 client_taskA.thr_mgr ()->wait ();
298 client_taskB.thr_mgr ()->wait ();
300 if (implA != 0)
302 removeServant(rtorbA.in (), rootPoaA.in (), implA, tpidA);
305 shutdownORB(orbA.in (), orbidA);
307 if (implB != 0)
309 removeServant(rtorbB.in (), rootPoaB.in (), implB, tpidB);
312 shutdownORB(orbB.in (), orbidB);
314 objB = CORBA::Object::_nil ();
315 helloB = Test::Hello::_nil ();
316 orbB = CORBA::ORB::_nil ();
317 rtorbB = RTCORBA::RTORB::_nil ();
318 rootPoaB = PortableServer::POA::_nil ();
319 objA = CORBA::Object::_nil ();
320 helloA = Test::Hello::_nil ();
321 orbA = CORBA::ORB::_nil ();
322 rtorbA = RTCORBA::RTORB::_nil ();
323 rootPoaA = PortableServer::POA::_nil ();
325 catch (const CORBA::Exception& ex)
327 ex._tao_print_exception ("Caught unexpected exception:");
328 ACE_ERROR ((LM_ERROR, "Bug_2953_Regression failed.\n"));
329 return 1;
332 ACE_DEBUG ((LM_DEBUG, "Bug_2953_Regression server ended successfully.\n"));
334 return 0;