Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / examples / Load_Balancing / Identity_Client.cpp
blob0b75abf340419944f5f5195b46024438b6c87239
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"
14 #include "tao/debug.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/OS_NS_string.h"
19 Identity_Client::Identity_Client ()
20 : group_factory_ior_ (0),
21 number_of_invocations_ (5),
22 use_random_ (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: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 'r': // flag signifying to obtain references to <Identity>
45 // objects from the random <Object_Group> rather than
46 // from the round robin one.
47 this->use_random_ = 1;
48 break;
49 case '?':
50 default:
51 ACE_ERROR_RETURN ((LM_ERROR,
52 "usage: %s"
53 " [-d]"
54 " [-i] <Object_Group_Factory_ior>"
55 " [-n] <number_of_invocations>"
56 " [-r]"
57 "\n",
58 argv [0]),
59 -1);
62 // Indicates successful parsing of command line.
63 return 0;
66 int
67 Identity_Client::init (int argc,
68 ACE_TCHAR* argv[])
70 int result;
72 try
74 result = this->orb_manager_.init (argc, argv);
75 if (result == -1)
76 return result;
78 // Check the non-ORB arguments.
79 result = this->parse_args (argc, argv);
80 if (result < 0)
81 return result;
83 catch (const CORBA::Exception& ex)
85 ex._tao_print_exception ("Identity_Client::init");
86 return -1;
89 return 0;
92 int
93 Identity_Client::run ()
95 ACE_DEBUG ((LM_DEBUG, "Identity_Client: Initialized\n"));
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_);
101 Load_Balancer::Object_Group_Factory_var factory =
102 Load_Balancer::Object_Group_Factory::_narrow (obj.in ());
104 if (CORBA::is_nil (factory.in ()))
105 ACE_ERROR_RETURN ((LM_ERROR,
106 "Identity_Client: problems using the factory ior\n"),
107 -1);
109 const char *group_name = 0;
110 if (this->use_random_)
111 group_name = "Identity, Random";
112 else
113 group_name = "Identity, Round Robin";
115 ACE_DEBUG ((LM_DEBUG,
116 "Identity_Client: Requesting Object Group "
117 "with id <%C>\n", group_name));
118 Load_Balancer::Object_Group_var object_group =
119 factory->resolve (group_name);
121 // List <Object_Group>'s id.
122 CORBA::String_var id = object_group->id ();
124 if (ACE_OS::strcmp (id.in (), group_name) != 0)
125 ACE_ERROR_RETURN ((LM_ERROR,
126 "Identity_Client: incorrect object group"
127 "returned from factory->resolve\n"),
128 -1);
130 // List all <Object_Group>s members.
131 ACE_DEBUG ((LM_DEBUG,
132 "Identity_Client: Requesting member list of <%C> Object Group\n",
133 group_name));
135 Load_Balancer::Member_ID_List_var id_list =
136 object_group->members ();
138 ACE_DEBUG ((LM_DEBUG,
139 "Identity_Client: The Group contains %d members:\n",
140 id_list->length ()));
141 for (CORBA::ULong i = 0; i < id_list->length (); ++i)
142 ACE_DEBUG ((LM_DEBUG, " <%C>\n",
143 static_cast<char const*>((id_list[i]))));
145 // Perform <number_of_invocations_> method calls on <Identity>
146 // objects, which are members of the <Object_Group>. Before each
147 // invocations, we get an <Identity> reference to use for that
148 // invocation from our <Object_Group>.
149 Identity_var identity_object;
150 CORBA::String_var identity;
152 ACE_DEBUG ((LM_DEBUG,
153 "Identity_Client: Performing %d invocation(s), "
154 "consulting the <%C> Group\n"
155 " for Identity object "
156 "to use before each invocation\n",
157 this->number_of_invocations_,
158 group_name));
160 for (size_t ind = 0; ind < this->number_of_invocations_; ++ind)
162 obj = object_group->resolve ();
164 identity_object = Identity::_narrow (obj.in ());
165 if (CORBA::is_nil (identity_object.in ()))
166 ACE_ERROR_RETURN ((LM_ERROR,
167 "Identity_Client: cannot narrow an object received from"
168 "<Object_Group::resolve> to <Identity>\n"),
169 -1);
170 identity_object->get_name (identity.out ());
173 ACE_DEBUG ((LM_DEBUG,
174 "Identity_Client: Done\n"));
176 return 0;
179 Identity_Client::~Identity_Client ()
184 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
186 int result = 0;
187 Identity_Client client;
189 if (client.init (argc, argv) == -1)
190 return 1;
194 result = client.run ();
196 catch (const CORBA::Exception& ex)
198 ex._tao_print_exception ("Identity_Client");
199 return 1;
202 if (result == -1)
203 return 1;
204 else
205 return 0;