Cleanup Solaris support
[ACE_TAO.git] / TAO / examples / Load_Balancing_persistent / Identity_Client.cpp
blob76b84686191449c32c61882d6f46cb379d5821ef
1 //=============================================================================
2 /**
3 * @file Identity_Client.cpp
5 * @author Marina Spivak <marina@cs.wustl.edu>
6 */
7 //=============================================================================
10 #include "Identity_Client.h"
11 #include "IdentityC.h"
12 #include "Load_BalancerC.h"
13 #include "tao/debug.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/High_Res_Timer.h"
16 #include "ace/Stats.h"
18 Identity_Client::Identity_Client ()
19 : group_factory_ior_ (0),
20 number_of_invocations_ (5),
21 use_random_ (0),
22 iterations_ (0)
26 int
27 Identity_Client::parse_args (int argc, ACE_TCHAR *argv[])
29 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("di:n:k:r"));
30 int c;
32 while ((c = get_opts ()) != -1)
33 switch (c)
35 case 'd': // debug flag.
36 TAO_debug_level++;
37 break;
38 case 'i': // ior of the <Object_Group_Factory> object.
39 this->group_factory_ior_ = get_opts.opt_arg ();
40 break;
41 case 'n': // number of times to make invocation on an <Identity> object.
42 this->number_of_invocations_ = ACE_OS::atoi (get_opts.opt_arg ());
43 break;
44 case 'k':
45 this->iterations_ = ACE_OS::atoi (get_opts.opt_arg ());
46 break;
47 case 'r': // flag signifying to obtain references to <Identity>
48 // objects from the random <Object_Group> rather than
49 // from the round robin one.
50 this->use_random_ = 1;
51 break;
52 case '?':
53 default:
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "usage: %s"
56 " [-d]"
57 " [-i] <Object_Group_Factory_ior>"
58 " [-n] <number_of_invocations>"
59 " [-r]"
60 "\n",
61 argv [0]),
62 -1);
65 // Indicates successful parsing of command line.
66 return 0;
69 int
70 Identity_Client::init (int argc, ACE_TCHAR* argv[])
72 int result;
74 try
76 result = this->orb_manager_.init (argc, argv);
77 if (result == -1)
78 return result;
80 // Check the non-ORB arguments.
81 result = this->parse_args (argc, argv);
82 if (result < 0)
83 return result;
85 catch (const CORBA::Exception& ex)
87 ex._tao_print_exception ("Identity_Client::init");
88 return -1;
91 return 0;
94 int
95 Identity_Client::run ()
97 // Contact the <Object_Group_Factory> to obtain an <Object_Group>.
98 CORBA::ORB_var orb = orb_manager_.orb ();
99 CORBA::Object_var obj =
100 orb->string_to_object (this->group_factory_ior_);
102 if (obj.in () == 0)
103 ACE_ERROR_RETURN ((LM_ERROR,
104 ACE_TEXT ("(%N|%l) <ERROR> [Identity_Client::run]\n")
105 ACE_TEXT ("factory_resolve\n")),
106 -1);
108 Load_Balancer::Object_Group_Factory_var factory =
109 Load_Balancer::Object_Group_Factory::_narrow (obj.in ());
111 if (CORBA::is_nil (factory.in ()))
112 ACE_ERROR_RETURN ((LM_ERROR,
113 "Identity_Client: problems using the factory ior\n"),
114 -1);
116 const char *group_name;
117 if (this->use_random_)
118 group_name = "Random group";
119 else
120 group_name = "Round Robin group";
122 Load_Balancer::Object_Group_var object_group;
124 // We have this for the measurement that was done.
125 #if defined (DOORS_MEASURE_STATS)
127 // Performance measurements start here
128 ACE_High_Res_Timer::calibrate ();
130 ACE_hrtime_t throughput_base = ACE_OS::gethrtime ();
132 ACE_Throughput_Stats throughput;
133 ACE_High_Res_Timer::global_scale_factor_type gsf =
134 ACE_High_Res_Timer::global_scale_factor ();
136 for (int i = 0; i < this->iterations_; i++)
138 // Record current time.
139 ACE_hrtime_t latency_base = ACE_OS::gethrtime ();
141 #endif /*TAO_MEASURE_STATS*/
142 // Remote call
143 object_group =
144 factory->resolve (group_name);
146 CORBA::String_var iorstring =
147 orb->object_to_string (object_group.in ());
149 ACE_DEBUG ((LM_DEBUG,
150 "The ior string is %s\n", iorstring.in ()));
151 #if defined (DOORS_MEASURE_STATS)
152 // Grab timestamp again.
153 ACE_hrtime_t now = ACE_OS::gethrtime ();
155 // Record statistics.
156 throughput.sample (now - throughput_base,
157 now - latency_base);
160 ACE_OS::printf ("*=*=*=*=Aggregated result *=*=*=*=*=\n");
161 throughput.dump_results (ACE_TEXT("Aggregated"), gsf);
162 #endif /*TAO_MEASURE_STATS */
164 if (CORBA::is_nil (object_group.in ()))
166 ACE_ERROR_RETURN ((LM_ERROR,
167 "(%l|%d)The narrowed object is NUL:"),
168 -1);
171 // List <Object_Group>'s id.
172 CORBA::String_var id = object_group->id ();
173 ACE_DEBUG ((LM_DEBUG, "Object Group's id is: %s\n\n", id.in ()));
175 // List all <Object_Group>s members.
176 Load_Balancer::Member_ID_List_var id_list =
177 object_group->members ();
178 ACE_DEBUG ((LM_DEBUG,
179 "The group contains %d members:\n",
180 id_list->length ()));
181 for (CORBA::ULong i = 0; i < id_list->length (); ++i)
182 ACE_DEBUG ((LM_DEBUG, "%s\n", static_cast<char const*>(id_list[i])));
184 // Perform <number_of_invocations_> method calls on <Identity>
185 // objects, which are members of the <Object_Group>. Before each
186 // invocations, we get an <Identity> reference to use for that
187 // invocation from our <Object_Group>.
188 Identity_var identity_object;
189 CORBA::String_var identity;
190 CORBA::String_var objref;
192 for (size_t ind = 0; ind < this->number_of_invocations_; ++ind)
194 objref = object_group->resolve ();
196 obj = orb->string_to_object (objref.in ());
198 identity_object = Identity::_narrow (obj.in ());
200 if (CORBA::is_nil (identity_object.in ()))
201 ACE_ERROR_RETURN ((LM_ERROR,
202 "Identity_Client: cannot narrow an object received from"
203 "<Object_Group::resolve> to <Identity>\n"),
204 -1);
205 identity_object->get_name (identity.out ());
207 ACE_DEBUG ((LM_DEBUG,
208 "Invocation %s\n",
209 identity.in ()));
212 return 0;
215 Identity_Client::~Identity_Client ()
220 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
222 int result = 0;
223 Identity_Client client;
225 if (client.init (argc, argv) == -1)
226 return 1;
230 result = client.run ();
232 catch (const CORBA::Exception& ex)
234 ex._tao_print_exception ("Identity_Client");
235 return 1;
238 if (result == -1)
239 return 1;
240 else
241 return 0;