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");
21 parse_args (int argc
, ACE_TCHAR
*argv
[])
23 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:s:r:c:d:"));
26 while ((c
= get_opts ()) != -1)
30 ior_output_file
= get_opts
.opt_arg ();
34 strategy
= get_opts
.opt_arg ();
39 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
44 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
49 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
54 ACE_ERROR_RETURN ((LM_ERROR
,
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"
65 // Indicates successful parsing of the command line
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);
83 name
[0].id
= "RoundtripObjectGroup";
84 name
[0].kind
= "Object Group";
86 CORBA::Object_var group
;
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);
100 PortableGroup::Property
& property
= criteria
[0];
101 property
.nam
.length (1);
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
,
121 PortableGroup::Properties
props (1);
123 props
[0].nam
.length (1);
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
=
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);
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);
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);
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
,
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 (),
207 return group
._retn ();
211 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
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
,
218 // Enable FIFO scheduling
220 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO
,
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"));
231 ACE_ERROR ((LM_ERROR
,
232 "server (%P|%t): sched_params failed\n"));
237 ORBInitializer
*initializer
= 0;
238 ACE_NEW_RETURN (initializer
,
240 -1); // No exceptions yet!
241 PortableInterceptor::ORBInitializer_var orb_initializer
=
244 PortableInterceptor::register_orb_initializer (orb_initializer
.in ());
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)
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 (),
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",
309 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
310 ACE_OS::fclose (output_file
);
312 load_manager
->register_load_monitor (location
.in (),
315 load_manager
->register_load_alert (location
.in (),
320 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) server - event loop finished\n"));
322 root_poa
->destroy (true, true);
326 catch (const CORBA::Exception
& ex
)
328 ex
._tao_print_exception ("Exception caught:");