Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / examples / LoadBalancing / server.cpp
blobc3f0c42fcd8bbe10be497e71c31076ebad49bd58
1 #include "tao/ORBInitializer_Registry.h"
2 #include "StockFactory.h"
3 #include "ORBInitializer.h"
4 #include "RPS_Monitor.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_NS_strings.h"
8 #include "ace/Get_Opt.h"
10 const ACE_TCHAR *ior_output_file = ACE_TEXT("obj.ior");
12 CORBA::Float reject_threshold = 0;
13 CORBA::Float critical_threshold = 0;
14 CORBA::Float dampening = 0;
15 const ACE_TCHAR *strategy = ACE_TEXT("LeastLoaded");
16 int number;
18 int
19 parse_args (int argc, ACE_TCHAR *argv[])
21 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:n:s:r:c:d:"));
22 int c;
24 while ((c = get_opts ()) != -1)
25 switch (c)
27 case 'o':
28 ior_output_file = get_opts.opt_arg ();
29 break;
31 case 's':
32 strategy = get_opts.opt_arg ();
33 break;
35 case 'n':
36 number = ACE_OS::atoi (get_opts.opt_arg ());
37 break;
39 case 'r':
40 reject_threshold =
41 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
42 break;
44 case 'c':
45 critical_threshold =
46 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
47 break;
49 case 'd':
50 dampening =
51 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
52 break;
54 case '?':
55 default:
56 ACE_ERROR_RETURN ((LM_ERROR,
57 "usage: %s\n"
58 " -o <iorfile>\n"
59 " -s <RoundRobin | Random | LeastLoaded>\n"
60 " -r <reject threshold> (LeastLoaded only)\n"
61 " -c <critical threshold> (LeastLoaded only)\n"
62 " -d <damping value> (LeastLoaded only)\n"
63 "\n",
64 argv [0]),
65 -1);
67 // Indicates successful parsing of the command line
68 return 0;
71 CORBA::Object_ptr
72 join_object_group (CORBA::ORB_ptr orb,
73 CosLoadBalancing::LoadManager_ptr lm,
74 const PortableGroup::Location & location)
76 CORBA::Object_var ns_object =
77 orb->resolve_initial_references ("NameService");
79 CosNaming::NamingContext_var nc =
80 CosNaming::NamingContext::_narrow (ns_object.in ());
82 CosNaming::Name name (1);
83 name.length (1);
85 name[0].id = "StockFactoryObjectGroup";
86 name[0].kind = "Object Group";
88 CORBA::Object_var group;
90 try
92 group = nc->resolve (name);
94 catch (const CosNaming::NamingContext::NotFound& )
96 // Object group not created. Create one.
97 const char repository_id[] = "IDL:Test/StockFactory:1.0";
99 PortableGroup::Criteria criteria (1);
100 criteria.length (1);
102 PortableGroup::Property & property = criteria[0];
103 property.nam.length (1);
105 property.nam[0].id =
106 CORBA::string_dup ("omg.org.PortableGroup.MembershipStyle");
108 PortableGroup::MembershipStyleValue msv =
109 PortableGroup::MEMB_APP_CTRL;
110 property.val <<= msv;
112 PortableGroup::GenericFactory::FactoryCreationId_var fcid;
114 group = lm->create_object (repository_id,
115 criteria,
116 fcid.out ());
120 nc->bind (name,
121 group.in ());
123 PortableGroup::Properties props (1);
124 props.length (1);
125 props[0].nam.length (1);
126 props[0].nam[0].id =
127 CORBA::string_dup ("org.omg.CosLoadBalancing.StrategyInfo");
129 CosLoadBalancing::StrategyInfo strategy_info;
131 strategy_info.name = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(strategy));
133 if (ACE_OS::strcasecmp (strategy, ACE_TEXT("LeastLoaded")) == 0
134 && (!ACE::is_equal (reject_threshold, 0.0f)
135 || !ACE::is_equal (critical_threshold, 0.0f)
136 || !ACE::is_equal (dampening, 0.0f)))
138 CORBA::ULong len = 1;
140 PortableGroup::Properties & props =
141 strategy_info.props;
143 if (!ACE::is_equal (reject_threshold, 0.0f))
145 const CORBA::ULong i = len - 1;
147 props.length (len++);
149 props[i].nam.length (1);
150 props[i].nam[0].id =
151 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.RejectThreshold");
152 props[i].val <<= reject_threshold;
155 if (!ACE::is_equal (critical_threshold, 0.0f))
157 const CORBA::ULong i = len - 1;
159 props.length (len++);
161 props[i].nam.length (1);
162 props[i].nam[0].id =
163 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.CriticalThreshold");
164 props[i].val <<= critical_threshold;
167 if (!ACE::is_equal (dampening, 0.0f))
169 const CORBA::ULong i = len - 1;
171 props.length (len++);
173 props[i].nam.length (1);
174 props[i].nam[0].id =
175 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.Dampening");
176 props[i].val <<= dampening;
181 props[0].val <<= strategy_info;
183 lm->set_default_properties (props);
185 catch (const CosNaming::NamingContext::AlreadyBound& )
187 // Somebody beat us to creating the object group. Clean up
188 // the one we created.
189 lm->delete_object (fcid.in ());
191 group = nc->resolve (name);
195 StockFactory * stockfactory_impl;
196 ACE_NEW_THROW_EX (stockfactory_impl,
197 StockFactory (orb, number),
198 CORBA::NO_MEMORY ());
200 PortableServer::ServantBase_var owner_transfer (stockfactory_impl);
202 Test::StockFactory_var stockfactory =
203 stockfactory_impl->_this ();
205 group = lm->add_member (group.in (),
206 location,
207 stockfactory.in ());
209 return group._retn ();
213 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
217 ORBInitializer *initializer = 0;
218 ACE_NEW_RETURN (initializer,
219 ORBInitializer,
220 -1); // No exceptions yet!
221 PortableInterceptor::ORBInitializer_var orb_initializer =
222 initializer;
224 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
226 CORBA::ORB_var orb =
227 CORBA::ORB_init (argc, argv);
230 CORBA::Object_var poa_object =
231 orb->resolve_initial_references ("RootPOA");
233 if (CORBA::is_nil (poa_object.in ()))
234 ACE_ERROR_RETURN ((LM_ERROR,
235 " (%P|%t) Unable to initialize the POA.\n"),
238 PortableServer::POA_var root_poa =
239 PortableServer::POA::_narrow (poa_object.in ());
241 PortableServer::POAManager_var poa_manager =
242 root_poa->the_POAManager ();
244 if (parse_args (argc, argv) != 0)
245 return 1;
247 poa_manager->activate ();
249 CORBA::Object_var lm_object =
250 orb->resolve_initial_references ("LoadManager");
252 CosLoadBalancing::LoadManager_var load_manager =
253 CosLoadBalancing::LoadManager::_narrow (lm_object.in ());
255 RPS_Monitor * monitor_servant;
256 ACE_NEW_THROW_EX (monitor_servant,
257 RPS_Monitor (initializer->interceptor ()),
258 CORBA::NO_MEMORY ());
260 PortableServer::ServantBase_var safe_monitor_servant (monitor_servant);
262 CosLoadBalancing::LoadMonitor_var load_monitor =
263 monitor_servant->_this ();
265 PortableGroup::Location_var location =
266 load_monitor->the_location ();
268 CORBA::Object_var stockfactory =
269 ::join_object_group (orb.in (),
270 load_manager.in (),
271 location.in ());
273 TAO_LB_LoadAlert & alert_servant = initializer->load_alert ();
275 CosLoadBalancing::LoadAlert_var load_alert =
276 alert_servant._this ();
279 CORBA::String_var ior =
280 orb->object_to_string (stockfactory.in ());
282 // If the ior_output_file exists, output the ior to it
283 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
284 if (output_file == 0)
285 ACE_ERROR_RETURN ((LM_ERROR,
286 "Cannot open output file for writing IOR: %s",
287 ior_output_file),
289 ACE_OS::fprintf (output_file, "%s", ior.in ());
290 ACE_OS::fclose (output_file);
292 load_manager->register_load_monitor (location.in (),
293 load_monitor.in ());
295 load_manager->register_load_alert (location.in (),
296 load_alert.in ());
298 orb->run ();
300 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
302 root_poa->destroy (true, true);
304 orb->destroy ();
306 catch (const CORBA::Exception& ex)
308 ex._tao_print_exception ("lb_server exception");
309 return 1;
312 return 0;