1 //=============================================================================
3 * @file Identity_Client.cpp
5 * @author Marina Spivak <marina@cs.wustl.edu>
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 (void)
20 : group_factory_ior_ (0),
21 number_of_invocations_ (5),
27 Identity_Client::parse_args (int argc
, ACE_TCHAR
*argv
[])
29 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("di:n:r"));
32 while ((c
= get_opts ()) != -1)
35 case 'd': // debug flag.
38 case 'i': // ior of the <Object_Group_Factory> object.
39 this->group_factory_ior_
= get_opts
.opt_arg ();
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 ());
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;
51 ACE_ERROR_RETURN ((LM_ERROR
,
54 " [-i] <Object_Group_Factory_ior>"
55 " [-n] <number_of_invocations>"
62 // Indicates successful parsing of command line.
67 Identity_Client::init (int argc
,
74 result
= this->orb_manager_
.init (argc
, argv
);
78 // Check the non-ORB arguments.
79 result
= this->parse_args (argc
, argv
);
83 catch (const CORBA::Exception
& ex
)
85 ex
._tao_print_exception ("Identity_Client::init");
93 Identity_Client::run (void)
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"),
109 const char *group_name
= 0;
110 if (this->use_random_
)
111 group_name
= "Identity, Random";
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"),
130 // List all <Object_Group>s members.
131 ACE_DEBUG ((LM_DEBUG
,
132 "Identity_Client: Requesting member list of <%C> Object Group\n",
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_
,
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"),
170 identity_object
->get_name (identity
.out ());
173 ACE_DEBUG ((LM_DEBUG
,
174 "Identity_Client: Done\n"));
179 Identity_Client::~Identity_Client (void)
184 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
187 Identity_Client client
;
189 if (client
.init (argc
, argv
) == -1)
194 result
= client
.run ();
196 catch (const CORBA::Exception
& ex
)
198 ex
._tao_print_exception ("Identity_Client");