Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / performance-tests / LoadBalancing / LBPerf / RPS / server.cpp
blob213e824a89b4073e8dbb54c87114c8affb774062
1 #include "Roundtrip.h"
2 #include "ORBInitializer.h"
3 #include "RPS_Monitor.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/Sched_Params.h"
7 #include "ace/OS_NS_strings.h"
8 #include "ace/OS_NS_errno.h"
10 #include "tao/ORBInitializer_Registry.h"
11 #include "tao/Strategies/advanced_resource.h"
13 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
15 CORBA::Float reject_threshold = 0;
16 CORBA::Float critical_threshold = 0;
17 CORBA::Float dampening = 0;
18 const ACE_TCHAR *strategy = ACE_TEXT("Random");
20 int
21 parse_args (int argc, ACE_TCHAR *argv[])
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:r:c:d:"));
24 int c;
26 while ((c = get_opts ()) != -1)
27 switch (c)
29 case 'o':
30 ior_output_file = get_opts.opt_arg ();
31 break;
33 case 's':
34 strategy = get_opts.opt_arg ();
35 break;
37 case 'r':
38 reject_threshold =
39 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
40 break;
42 case 'c':
43 critical_threshold =
44 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
45 break;
47 case 'd':
48 dampening =
49 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
50 break;
52 case '?':
53 default:
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "usage: %s\n"
56 " -o <iorfile>\n"
57 " -s <RoundRobin | Random | LeastLoaded>\n"
58 " -r <reject threshold> (LeastLoaded only)\n"
59 " -c <critical threshold> (LeastLoaded only)\n"
60 " -d <damping value> (LeastLoaded only)\n"
61 "\n",
62 argv [0]),
63 -1);
65 // Indicates successful parsing of the command line
66 return 0;
69 CORBA::Object_ptr
70 join_object_group (CORBA::ORB_ptr orb,
71 CosLoadBalancing::LoadManager_ptr lm,
72 const PortableGroup::Location & location)
74 CORBA::Object_var ns_object =
75 orb->resolve_initial_references ("NameService");
77 CosNaming::NamingContext_var nc =
78 CosNaming::NamingContext::_narrow (ns_object.in ());
80 CosNaming::Name name (1);
81 name.length (1);
83 name[0].id = "RoundtripObjectGroup";
84 name[0].kind = "Object Group";
86 CORBA::Object_var group;
88 try
90 group = nc->resolve (name);
92 catch (const CosNaming::NamingContext::NotFound& )
94 // Object group not created. Create one.
95 const char repository_id[] = "IDL:Test/Roundtrip:1.0";
97 PortableGroup::Criteria criteria (1);
98 criteria.length (1);
100 PortableGroup::Property & property = criteria[0];
101 property.nam.length (1);
103 property.nam[0].id =
104 CORBA::string_dup ("org.omg.PortableGroup.MembershipStyle");
106 PortableGroup::MembershipStyleValue msv =
107 PortableGroup::MEMB_APP_CTRL;
108 property.val <<= msv;
110 PortableGroup::GenericFactory::FactoryCreationId_var fcid;
112 group = lm->create_object (repository_id,
113 criteria,
114 fcid.out ());
118 nc->bind (name,
119 group.in ());
121 PortableGroup::Properties props (1);
122 props.length (1);
123 props[0].nam.length (1);
124 props[0].nam[0].id =
125 CORBA::string_dup ("org.omg.CosLoadBalancing.StrategyInfo");
127 CosLoadBalancing::StrategyInfo strategy_info;
129 strategy_info.name = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(strategy));
131 if (ACE_OS::strcasecmp (strategy, ACE_TEXT("LeastLoaded")) == 0
132 && (!ACE::is_equal (reject_threshold, (CORBA::Float)0.0)
133 || !ACE::is_equal (critical_threshold, (CORBA::Float)0.0)
134 || !ACE::is_equal (dampening, (CORBA::Float)0.0)))
136 CORBA::ULong len = 1;
138 PortableGroup::Properties & props =
139 strategy_info.props;
141 if (!ACE::is_equal (reject_threshold, (CORBA::Float)0.0))
143 const CORBA::ULong i = len - 1;
145 props.length (len++);
147 props[i].nam.length (1);
148 props[i].nam[0].id =
149 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.RejectThreshold");
150 props[i].val <<= reject_threshold;
153 if (!ACE::is_equal (critical_threshold, (CORBA::Float)0.0))
155 const CORBA::ULong i = len - 1;
157 props.length (len++);
159 props[i].nam.length (1);
160 props[i].nam[0].id =
161 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.CriticalThreshold");
162 props[i].val <<= critical_threshold;
165 if (!ACE::is_equal (dampening, (CORBA::Float)0.0))
167 const CORBA::ULong i = len - 1;
169 props.length (len++);
171 props[i].nam.length (1);
172 props[i].nam[0].id =
173 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.Dampening");
174 props[i].val <<= dampening;
179 props[0].val <<= strategy_info;
181 lm->set_default_properties (props);
183 catch (const CosNaming::NamingContext::AlreadyBound& )
185 // Somebody beat us to creating the object group. Clean up
186 // the one we created.
187 lm->delete_object (fcid.in ());
189 group = nc->resolve (name);
193 Roundtrip * roundtrip_impl;
194 ACE_NEW_THROW_EX (roundtrip_impl,
195 Roundtrip (orb),
196 CORBA::NO_MEMORY ());
198 PortableServer::ServantBase_var owner_transfer (roundtrip_impl);
200 Test::Roundtrip_var roundtrip =
201 roundtrip_impl->_this ();
203 group = lm->add_member (group.in (),
204 location,
205 roundtrip.in ());
207 return group._retn ();
211 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
213 int priority =
214 (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
215 + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
216 priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
217 priority);
218 // Enable FIFO scheduling
220 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
221 priority,
222 ACE_SCOPE_PROCESS)) != 0)
224 if (ACE_OS::last_error () == EPERM)
226 ACE_DEBUG ((LM_DEBUG,
227 "server (%P|%t): user is not superuser, "
228 "test runs in time-shared class\n"));
230 else
231 ACE_ERROR ((LM_ERROR,
232 "server (%P|%t): sched_params failed\n"));
237 ORBInitializer *initializer = 0;
238 ACE_NEW_RETURN (initializer,
239 ORBInitializer,
240 -1); // No exceptions yet!
241 PortableInterceptor::ORBInitializer_var orb_initializer =
242 initializer;
244 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
246 CORBA::ORB_var orb =
247 CORBA::ORB_init (argc, argv);
250 CORBA::Object_var poa_object =
251 orb->resolve_initial_references ("RootPOA");
253 if (CORBA::is_nil (poa_object.in ()))
254 ACE_ERROR_RETURN ((LM_ERROR,
255 " (%P|%t) Unable to initialize the POA.\n"),
258 PortableServer::POA_var root_poa =
259 PortableServer::POA::_narrow (poa_object.in ());
261 PortableServer::POAManager_var poa_manager =
262 root_poa->the_POAManager ();
264 if (parse_args (argc, argv) != 0)
265 return 1;
267 poa_manager->activate ();
269 CORBA::Object_var lm_object =
270 orb->resolve_initial_references ("LoadManager");
272 CosLoadBalancing::LoadManager_var load_manager =
273 CosLoadBalancing::LoadManager::_narrow (lm_object.in ());
275 RPS_Monitor * monitor_servant;
276 ACE_NEW_THROW_EX (monitor_servant,
277 RPS_Monitor (initializer->interceptor ()),
278 CORBA::NO_MEMORY ());
280 PortableServer::ServantBase_var safe_monitor_servant (monitor_servant);
282 CosLoadBalancing::LoadMonitor_var load_monitor =
283 monitor_servant->_this ();
285 PortableGroup::Location_var location =
286 load_monitor->the_location ();
288 CORBA::Object_var roundtrip =
289 ::join_object_group (orb.in (),
290 load_manager.in (),
291 location.in ());
293 TAO_LB_LoadAlert & alert_servant = initializer->load_alert ();
295 CosLoadBalancing::LoadAlert_var load_alert =
296 alert_servant._this ();
299 CORBA::String_var ior =
300 orb->object_to_string (roundtrip.in ());
302 // If the ior_output_file exists, output the ior to it
303 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
304 if (output_file == 0)
305 ACE_ERROR_RETURN ((LM_ERROR,
306 "Cannot open output file for writing IOR: %s",
307 ior_output_file),
309 ACE_OS::fprintf (output_file, "%s", ior.in ());
310 ACE_OS::fclose (output_file);
312 load_manager->register_load_monitor (location.in (),
313 load_monitor.in ());
315 load_manager->register_load_alert (location.in (),
316 load_alert.in ());
318 orb->run ();
320 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
322 root_poa->destroy (true, true);
324 orb->destroy ();
326 catch (const CORBA::Exception& ex)
328 ex._tao_print_exception ("Exception caught:");
329 return 1;
332 return 0;