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");
19 parse_args (int argc
, ACE_TCHAR
*argv
[])
21 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:n:s:r:c:d:"));
24 while ((c
= get_opts ()) != -1)
28 ior_output_file
= get_opts
.opt_arg ();
32 strategy
= get_opts
.opt_arg ();
36 number
= ACE_OS::atoi (get_opts
.opt_arg ());
41 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
46 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
51 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
56 ACE_ERROR_RETURN ((LM_ERROR
,
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"
67 // Indicates successful parsing of the command line
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);
85 name
[0].id
= "StockFactoryObjectGroup";
86 name
[0].kind
= "Object Group";
88 CORBA::Object_var group
;
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);
102 PortableGroup::Property
& property
= criteria
[0];
103 property
.nam
.length (1);
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
,
123 PortableGroup::Properties
props (1);
125 props
[0].nam
.length (1);
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
=
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);
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);
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);
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 (),
209 return group
._retn ();
213 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
217 ORBInitializer
*initializer
= 0;
218 ACE_NEW_RETURN (initializer
,
220 -1); // No exceptions yet!
221 PortableInterceptor::ORBInitializer_var orb_initializer
=
224 PortableInterceptor::register_orb_initializer (orb_initializer
.in ());
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)
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 (),
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",
289 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
290 ACE_OS::fclose (output_file
);
292 load_manager
->register_load_monitor (location
.in (),
295 load_manager
->register_load_alert (location
.in (),
300 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) server - event loop finished\n"));
302 root_poa
->destroy (true, true);
306 catch (const CORBA::Exception
& ex
)
308 ex
._tao_print_exception ("lb_server exception");