1 #include "Identity_Server.h"
2 #include "Identity_i.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_stdio.h"
7 Identity_Server::Identity_Server (void)
8 : group_factory_ior_ (0),
15 Identity_Server::parse_args (int argc
, ACE_TCHAR
*argv
[])
17 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("di:a:o:"));
20 while ((c
= get_opts ()) != -1)
23 case 'd': // debug flag.
26 case 'i': // ior of the <Object_Group_Factory> object.
27 this->group_factory_ior_
= get_opts
.opt_arg ();
29 case 'a': // number of objects to create/register with the random group.
30 random_objects_
= ACE_OS::atoi (get_opts
.opt_arg ());
32 case 'o': // number of objects to create/register with round
34 rr_objects_
= ACE_OS::atoi (get_opts
.opt_arg ());
38 ACE_ERROR_RETURN ((LM_ERROR
,
41 " [-i] <Object_Group_Factory_ior>"
42 " [-a] <number_of_objects_for_random_group>"
43 " [-o] <number_of_objects_for_rr_group>"
49 // Indicates successful parsing of command line.
54 Identity_Server::init (int argc
,
61 result
= this->orb_manager_
.init (argc
,
66 CORBA::PolicyList
policies (2);
71 this->orb_manager_
.root_poa()->create_lifespan_policy (PortableServer::PERSISTENT
);
74 this->orb_manager_
.root_poa()->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION
);
76 this->persistent_POA_
=
77 this->orb_manager_
.root_poa()->create_POA ("persistent_server",
78 this->orb_manager_
.poa_manager (),
82 // Destroy policy objects
83 for (CORBA::ULong i
= 0;
84 i
< policies
.length ();
87 policies
[i
]->destroy ();
92 catch (const CORBA::Exception
& ex
)
94 ex
._tao_print_exception ("Identity_Server::init");
102 Identity_Server::register_groups (void)
107 // Contact the <Object_Group_Factory> to create 2
108 // <Object_Group>s, one random and one rr.
109 CORBA::ORB_var orb
= orb_manager_
.orb ();
110 CORBA::Object_var obj
=
111 orb
->string_to_object (this->group_factory_ior_
);
113 Load_Balancer::Object_Group_Factory_var factory
=
114 Load_Balancer::Object_Group_Factory::_narrow (obj
.in ());
116 if (CORBA::is_nil (factory
.in ()))
117 ACE_ERROR_RETURN ((LM_ERROR
,
118 "Identity_Server::init: "
119 "problems using the factory ior\n"),
123 // Unbind the previously registered random group.
126 factory
->unbind_random ("Random group");
128 catch (const CORBA::Exception
&)
130 ACE_DEBUG ((LM_DEBUG
,
131 "(%N | %l) <Unbind> harmless here\n"));
134 // Unbind the previously registered round robin group
137 factory
->unbind_round_robin ("Round Robin group");
139 catch (const CORBA::Exception
&)
141 ACE_DEBUG ((LM_DEBUG
,
142 "(%N | %l) <Unbind> harmless here\n"));
146 // We want to make two groups Random & Round Robin.
147 Load_Balancer::Object_Group_var random_group
=
148 factory
->make_random ("Random group");
150 if (CORBA::is_nil (random_group
.in ()))
152 ACE_ERROR ((LM_ERROR
, "Got nil random group from load balancer\n"));
156 Load_Balancer::Object_Group_var rr_group
=
157 factory
->make_round_robin ("Round Robin group");
159 if (CORBA::is_nil (rr_group
.in ()))
161 ACE_ERROR ((LM_ERROR
, "Got nil round robin group from load balancer\n"));
165 // Create the requested number of <Identity> objects, and
166 // register them with the random and round robin
168 this->create_objects (random_objects_
,
172 this->create_objects (rr_objects_
,
179 Identity_Server::create_objects (size_t number_of_objects
,
180 Load_Balancer::Object_Group_ptr group
)
182 // Create the specified number of servants, and register each one
183 // with the provided <Object_Group>.
184 for (size_t i
= 0; i
< number_of_objects
; ++i
)
186 // Create an id for this servant.
187 ACE_TCHAR id
[BUFSIZ
];
189 ACE_TEXT("Identity object ") ACE_SIZE_T_FORMAT_SPECIFIER
,
192 // Create and activate a servant.
193 Identity_i
* identity_servant
;
194 ACE_NEW_THROW_EX (identity_servant
,
195 Identity_i (ACE_TEXT_ALWAYS_CHAR(id
), this->persistent_POA_
.in ()),
196 CORBA::NO_MEMORY ());
198 PortableServer::ServantBase_var s
= identity_servant
;
199 this->orb_manager_
.activate_poa_manager ();
201 CORBA::Object_var obj
= identity_servant
->_this ();
203 Load_Balancer::Member member
;
204 member
.id
= CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(id
));
205 member
.obj
= this->orb_manager_
.orb ()->object_to_string (obj
.in ());
207 // Do an unbind and then bind
210 group
->unbind (ACE_TEXT_ALWAYS_CHAR(id
));
212 catch (const CORBA::Exception
&)
214 ACE_DEBUG ((LM_DEBUG
,
215 "(%N | %l) Harmless here\n"));
218 // Bind the servant in the random <Object_Group>.
219 group
->bind (member
);
225 Identity_Server::run (void)
229 result
= this->orb_manager_
.run ();
234 Identity_Server::~Identity_Server (void)
239 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
242 Identity_Server server
;
244 if (server
.init (argc
, argv
) == -1)
247 // Check the non-ORB arguments.
248 if (server
.parse_args (argc
, argv
) == -1)
253 result
= server
.register_groups ();
258 result
= server
.run ();
260 catch (const CORBA::Exception
& ex
)
262 ex
._tao_print_exception ("Identity_Server");