Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / Load_Balancing_persistent / Identity_Server.cpp
blobace0fe75317c54d468e73325c68b48281160cb33
1 #include "Identity_Server.h"
2 #include "Identity_i.h"
3 #include "tao/debug.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_stdio.h"
7 Identity_Server::Identity_Server ()
8 : group_factory_ior_ (0),
9 random_objects_ (5),
10 rr_objects_ (5)
14 int
15 Identity_Server::parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("di:a:o:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'd': // debug flag.
24 TAO_debug_level++;
25 break;
26 case 'i': // ior of the <Object_Group_Factory> object.
27 this->group_factory_ior_ = get_opts.opt_arg ();
28 break;
29 case 'a': // number of objects to create/register with the random group.
30 random_objects_ = ACE_OS::atoi (get_opts.opt_arg ());
31 break;
32 case 'o': // number of objects to create/register with round
33 //robin group.
34 rr_objects_ = ACE_OS::atoi (get_opts.opt_arg ());
35 break;
36 case '?':
37 default:
38 ACE_ERROR_RETURN ((LM_ERROR,
39 "usage: %s"
40 " [-d]"
41 " [-i] <Object_Group_Factory_ior>"
42 " [-a] <number_of_objects_for_random_group>"
43 " [-o] <number_of_objects_for_rr_group>"
44 "\n",
45 argv [0]),
46 -1);
49 // Indicates successful parsing of command line.
50 return 0;
53 int
54 Identity_Server::init (int argc,
55 ACE_TCHAR* argv[])
57 int result;
59 try
61 result = this->orb_manager_.init (argc,
62 argv);
63 if (result == -1)
64 return result;
66 CORBA::PolicyList policies (2);
67 policies.length (2);
69 // Lifespan policy
70 policies[0] =
71 this->orb_manager_.root_poa()->create_lifespan_policy (PortableServer::PERSISTENT);
73 policies[1] =
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 (),
79 policies);
82 // Destroy policy objects
83 for (CORBA::ULong i = 0;
84 i < policies.length ();
85 ++i)
87 policies[i]->destroy ();
92 catch (const CORBA::Exception& ex)
94 ex._tao_print_exception ("Identity_Server::init");
95 return -1;
98 return 0;
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"),
117 -1);
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"));
150 return -1;
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"));
159 return -1;
162 // Create the requested number of <Identity> objects, and
163 // register them with the random and round robin
164 // <Object_Group>s.
165 this->create_objects (random_objects_,
166 random_group.in ());
169 this->create_objects (rr_objects_,
170 rr_group.in ());
172 return 0;
175 void
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];
185 ACE_OS::sprintf (id,
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 ()
223 int result;
225 result = this->orb_manager_.run ();
227 return result;
230 Identity_Server::~Identity_Server ()
235 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
237 int result = 0;
238 Identity_Server server;
240 if (server.init (argc, argv) == -1)
241 return 1;
243 // Check the non-ORB arguments.
244 if (server.parse_args (argc, argv) == -1)
245 return -1;
249 result = server.register_groups ();
251 if (result != 0)
252 return 1;
254 result = server.run ();
256 catch (const CORBA::Exception& ex)
258 ex._tao_print_exception ("Identity_Server");
259 return 1;
262 if (result == -1)
263 return 1;
264 else
265 return 0;