1 #include "orbsvcs/CosLoadBalancingC.h"
2 #include "orbsvcs/PortableGroup/PG_Operators.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/OS_NS_unistd.h"
6 const ACE_TCHAR
*location
= ACE_TEXT("MyLocation");
8 const int MAX_RETRIES
= 10;
9 const CosLoadBalancing::LoadId LOAD_ID
= CosLoadBalancing::LoadAverage
;
12 parse_args (int argc
, ACE_TCHAR
*argv
[])
14 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("l:"));
18 while ((c
= get_opts ()) != -1)
22 location
= get_opts
.opt_arg ();
26 ACE_ERROR_RETURN ((LM_ERROR
,
27 "Usage: %s -l <ior>\n",
36 check_loads (const CosLoadBalancing::LoadList
& loads
)
38 if (loads
.length () != 1)
41 "ERROR: Load list length is not equal to one.\n"));
43 throw CORBA::INTERNAL ();
46 if (loads
[0].id
!= LOAD_ID
47 || loads
[0].value
< 0)
50 "ERROR: Returned load is not a CPU load.\n"));
52 throw CORBA::INTERNAL ();
57 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
62 CORBA::ORB_init (argc
, argv
);
64 // Obtain a reference to the LoadManager.
65 CORBA::Object_var obj
=
66 orb
->resolve_initial_references ("LoadManager");
68 CosLoadBalancing::LoadManager_var lm
=
69 CosLoadBalancing::LoadManager::_narrow (obj
.in ());
71 CosLoadBalancing::Location
the_location (1);
72 the_location
.length (1);
74 the_location
[0].id
= CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(location
));
76 // Attempt to get loads from the LoadManager.
79 "Retrieving loads from LoadManager ..."));
81 CosLoadBalancing::LoadList_var loads
;
83 CORBA::Boolean retrieved_load
= false;
85 // Try a few times until a load is capable of being retrieved.
86 for (int i
= 0; i
< MAX_RETRIES
&& retrieved_load
== 0; ++i
)
90 loads
= lm
->get_loads (the_location
);
94 catch (const CORBA::Exception
&)
96 ACE_DEBUG ((LM_DEBUG
, ".")); // Show some progress.
98 // Give some time for loads to be reported to the
104 if (retrieved_load
== 0)
105 ACE_ERROR_RETURN ((LM_ERROR
,
106 "\nERROR: Unable to retrieve loads "
107 "from LoadManager.\n"),
113 ::check_loads (loads
.in ());
115 // Attempt to get loads directly from the LoadMonitor.
116 CosLoadBalancing::LoadMonitor_var monitor
=
117 lm
->get_load_monitor (the_location
);
119 CosLoadBalancing::Location_var cpu_mon_location
=
120 monitor
->the_location ();
122 if (cpu_mon_location
.in () != the_location
)
124 ACE_ERROR ((LM_ERROR
,
125 "ERROR: Mismatched CPU load monitor location\n"));
127 throw CORBA::INTERNAL ();
131 "Retrieving loads directly from LoadMonitor ..."));
133 loads
= monitor
->loads ();
138 ::check_loads (loads
.in ());
140 catch (const CORBA::Exception
& ex
)
142 ex
._tao_print_exception ("CPU Load Monitor test:");
146 ACE_DEBUG ((LM_INFO
, "CPU Load Monitor test passed.\n\n"));