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 ()
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 ()
104 // Contact the <Object_Group_Factory> to create 2
105 // <Object_Group>s, one random and one rr.
106 CORBA::ORB_var orb
= orb_manager_
.orb ();
107 CORBA::Object_var obj
=
108 orb
->string_to_object (this->group_factory_ior_
);
110 Load_Balancer::Object_Group_Factory_var factory
=
111 Load_Balancer::Object_Group_Factory::_narrow (obj
.in ());
113 if (CORBA::is_nil (factory
.in ()))
114 ACE_ERROR_RETURN ((LM_ERROR
,
115 "Identity_Server::init: "
116 "problems using the factory ior\n"),
120 // Unbind the previously registered random group.
123 factory
->unbind_random ("Random group");
125 catch (const CORBA::Exception
&)
127 ACE_DEBUG ((LM_DEBUG
,
128 "(%N | %l) <Unbind> harmless here\n"));
131 // Unbind the previously registered round robin group
134 factory
->unbind_round_robin ("Round Robin group");
136 catch (const CORBA::Exception
&)
138 ACE_DEBUG ((LM_DEBUG
,
139 "(%N | %l) <Unbind> harmless here\n"));
143 // We want to make two groups Random & Round Robin.
144 Load_Balancer::Object_Group_var random_group
=
145 factory
->make_random ("Random group");
147 if (CORBA::is_nil (random_group
.in ()))
149 ACE_ERROR ((LM_ERROR
, "Got nil random group from load balancer\n"));
153 Load_Balancer::Object_Group_var rr_group
=
154 factory
->make_round_robin ("Round Robin group");
156 if (CORBA::is_nil (rr_group
.in ()))
158 ACE_ERROR ((LM_ERROR
, "Got nil round robin group from load balancer\n"));
162 // Create the requested number of <Identity> objects, and
163 // register them with the random and round robin
165 this->create_objects (random_objects_
,
169 this->create_objects (rr_objects_
,
176 Identity_Server::create_objects (size_t number_of_objects
,
177 Load_Balancer::Object_Group_ptr group
)
179 // Create the specified number of servants, and register each one
180 // with the provided <Object_Group>.
181 for (size_t i
= 0; i
< number_of_objects
; ++i
)
183 // Create an id for this servant.
184 ACE_TCHAR id
[BUFSIZ
];
186 ACE_TEXT("Identity object ") ACE_SIZE_T_FORMAT_SPECIFIER
,
189 // Create and activate a servant.
190 Identity_i
* identity_servant
;
191 ACE_NEW_THROW_EX (identity_servant
,
192 Identity_i (ACE_TEXT_ALWAYS_CHAR(id
), this->persistent_POA_
.in ()),
193 CORBA::NO_MEMORY ());
195 PortableServer::ServantBase_var s
= identity_servant
;
196 this->orb_manager_
.activate_poa_manager ();
198 CORBA::Object_var obj
= identity_servant
->_this ();
200 Load_Balancer::Member member
;
201 member
.id
= CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(id
));
202 member
.obj
= this->orb_manager_
.orb ()->object_to_string (obj
.in ());
204 // Do an unbind and then bind
207 group
->unbind (ACE_TEXT_ALWAYS_CHAR(id
));
209 catch (const CORBA::Exception
&)
211 ACE_DEBUG ((LM_DEBUG
,
212 "(%N | %l) Harmless here\n"));
215 // Bind the servant in the random <Object_Group>.
216 group
->bind (member
);
221 Identity_Server::run ()
225 result
= this->orb_manager_
.run ();
230 Identity_Server::~Identity_Server ()
235 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
238 Identity_Server server
;
240 if (server
.init (argc
, argv
) == -1)
243 // Check the non-ORB arguments.
244 if (server
.parse_args (argc
, argv
) == -1)
249 result
= server
.register_groups ();
254 result
= server
.run ();
256 catch (const CORBA::Exception
& ex
)
258 ex
._tao_print_exception ("Identity_Server");