Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Load_Balancing / Identity_Server.cpp
blobe6def9c52953264b16fce533ed59169db914144a
1 //=============================================================================
2 /**
3 * @file Identity_Server.cpp
5 * @author Marina Spivak <marina@cs.wustl.edu>
6 */
7 //=============================================================================
10 #include "Identity_Server.h"
11 #include "Identity_i.h"
12 #include "tao/debug.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/OS_NS_stdio.h"
16 Identity_Server::Identity_Server (void)
17 : group_factory_ior_ (0),
18 random_objects_ (5),
19 rr_objects_ (5)
23 int
24 Identity_Server::parse_args (int argc, ACE_TCHAR *argv[])
26 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("di:a:o:"));
27 int c;
29 while ((c = get_opts ()) != -1)
30 switch (c)
32 case 'd': // debug flag.
33 TAO_debug_level++;
34 break;
35 case 'i': // ior of the <Object_Group_Factory> object.
36 this->group_factory_ior_ = get_opts.opt_arg ();
37 break;
38 case 'a': // number of objects to create/register with the random group.
39 random_objects_ = ACE_OS::atoi (get_opts.opt_arg ());
40 break;
41 case 'o': // number of objects to create/register with round
42 //robin group.
43 rr_objects_ = ACE_OS::atoi (get_opts.opt_arg ());
44 break;
45 case '?':
46 default:
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "usage: %s"
49 " [-d]"
50 " [-i] <Object_Group_Factory_ior>"
51 " [-a] <number_of_objects_for_random_group>"
52 " [-o] <number_of_objects_for_rr_group>"
53 "\n",
54 argv [0]),
55 -1);
58 // Indicates successful parsing of command line.
59 return 0;
62 int
63 Identity_Server::init (int argc,
64 ACE_TCHAR* argv[])
66 int result;
68 try
70 result = this->orb_manager_.init (argc,
71 argv);
72 if (result == -1)
73 return result;
75 // Check the non-ORB arguments.
76 result = this->parse_args (argc, argv);
77 if (result < 0)
78 return result;
80 // Contact the <Object_Group_Factory> to create 2
81 // <Object_Group>s, one random and one rr.
82 CORBA::ORB_var orb = orb_manager_.orb ();
83 CORBA::Object_var obj =
84 orb->string_to_object (this->group_factory_ior_);
85 Load_Balancer::Object_Group_Factory_var factory =
86 Load_Balancer::Object_Group_Factory::_narrow (obj.in ());
88 if (CORBA::is_nil (factory.in ()))
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "Identity_Server::init: "
91 "problems using the factory ior\n"),
92 -1);
94 ACE_DEBUG ((LM_DEBUG,
95 "Identity_Server: Requesting new Random Object "
96 "Group with id <Identity, Random>\n"));
98 Load_Balancer::Object_Group_var random_group =
99 factory->make_random ("Identity, Random");
101 ACE_DEBUG ((LM_DEBUG,
102 "Identity_Server: Requesting new Round Robin "
103 "Object Group with id <Identity, Round Robin>\n"));
105 Load_Balancer::Object_Group_var rr_group =
106 factory->make_round_robin ("Identity, Round Robin");
108 // Create the requested number of <Identity> objects, and
109 // register them with the random and round robin
110 // <Object_Group>s.
112 ACE_DEBUG ((LM_DEBUG,
113 "Identity_Server: Registering %d object(s) "
114 "with <Identity, Random> Object Group\n",
115 random_objects_));
116 create_objects (random_objects_,
117 random_group.in ());
119 ACE_DEBUG ((LM_DEBUG,
120 "Identity_Server: Registering %d object(s) "
121 "with <Identity, Round Robin> Object Group\n",
122 random_objects_));
123 create_objects (rr_objects_,
124 rr_group.in ());
126 catch (const CORBA::Exception& ex)
128 ex._tao_print_exception ("Identity_Server::init");
129 return -1;
132 return 0;
135 void
136 Identity_Server::create_objects (size_t number_of_objects,
137 Load_Balancer::Object_Group_ptr group)
139 // Create the specified number of servants, and register each one
140 // with the provided <Object_Group>.
141 for (size_t i = 0; i < number_of_objects; ++i)
143 // Create an id for this servant.
144 ACE_TCHAR id[BUFSIZ];
145 ACE_OS::sprintf (id,
146 ACE_TEXT("Identity object ") ACE_SIZE_T_FORMAT_SPECIFIER,
149 // Create and activate a servant.
150 Identity_i * identity_servant = 0;
151 ACE_NEW_THROW_EX (identity_servant,
152 Identity_i (ACE_TEXT_ALWAYS_CHAR(id)),
153 CORBA::NO_MEMORY ());
154 PortableServer::ServantBase_var s = identity_servant;
155 orb_manager_.activate (identity_servant);
157 Load_Balancer::Member member;
158 member.id = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(id));
159 member.obj = identity_servant->_this ();
161 // Bind the servant in the <Object_Group>.
162 group->bind (member);
167 Identity_Server::run (void)
169 ACE_DEBUG ((LM_DEBUG,
170 "Identity_Server: Initialized\n"));
172 int result;
174 result = this->orb_manager_.run ();
176 return result;
179 Identity_Server::~Identity_Server (void)
184 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
186 int result = 0;
187 Identity_Server server;
189 if (server.init (argc, argv) == -1)
190 return 1;
194 result = server.run ();
196 catch (const CORBA::Exception& ex)
198 ex._tao_print_exception ("Identity_Server");
199 return 1;
202 if (result == -1)
203 return 1;
204 else
205 return 0;