Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Load_Balancing_persistent / Identity_Server.cpp
blobe6a4069813d42c9fd78bb4dc2edbe8e04727b86e
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 (void)
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 (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"),
120 -1);
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"));
153 return -1;
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"));
162 return -1;
165 // Create the requested number of <Identity> objects, and
166 // register them with the random and round robin
167 // <Object_Group>s.
168 this->create_objects (random_objects_,
169 random_group.in ());
172 this->create_objects (rr_objects_,
173 rr_group.in ());
175 return 0;
178 void
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];
188 ACE_OS::sprintf (id,
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)
227 int result;
229 result = this->orb_manager_.run ();
231 return result;
234 Identity_Server::~Identity_Server (void)
239 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
241 int result = 0;
242 Identity_Server server;
244 if (server.init (argc, argv) == -1)
245 return 1;
247 // Check the non-ORB arguments.
248 if (server.parse_args (argc, argv) == -1)
249 return -1;
253 result = server.register_groups ();
255 if (result != 0)
256 return 1;
258 result = server.run ();
260 catch (const CORBA::Exception& ex)
262 ex._tao_print_exception ("Identity_Server");
263 return 1;
266 if (result == -1)
267 return 1;
268 else
269 return 0;