1 //=============================================================================
3 * @file Identity_Server.cpp
5 * @author Marina Spivak <marina@cs.wustl.edu>
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),
24 Identity_Server::parse_args (int argc
, ACE_TCHAR
*argv
[])
26 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("di:a:o:"));
29 while ((c
= get_opts ()) != -1)
32 case 'd': // debug flag.
35 case 'i': // ior of the <Object_Group_Factory> object.
36 this->group_factory_ior_
= get_opts
.opt_arg ();
38 case 'a': // number of objects to create/register with the random group.
39 random_objects_
= ACE_OS::atoi (get_opts
.opt_arg ());
41 case 'o': // number of objects to create/register with round
43 rr_objects_
= ACE_OS::atoi (get_opts
.opt_arg ());
47 ACE_ERROR_RETURN ((LM_ERROR
,
50 " [-i] <Object_Group_Factory_ior>"
51 " [-a] <number_of_objects_for_random_group>"
52 " [-o] <number_of_objects_for_rr_group>"
58 // Indicates successful parsing of command line.
63 Identity_Server::init (int argc
,
70 result
= this->orb_manager_
.init (argc
,
75 // Check the non-ORB arguments.
76 result
= this->parse_args (argc
, argv
);
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"),
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
112 ACE_DEBUG ((LM_DEBUG
,
113 "Identity_Server: Registering %d object(s) "
114 "with <Identity, Random> Object Group\n",
116 create_objects (random_objects_
,
119 ACE_DEBUG ((LM_DEBUG
,
120 "Identity_Server: Registering %d object(s) "
121 "with <Identity, Round Robin> Object Group\n",
123 create_objects (rr_objects_
,
126 catch (const CORBA::Exception
& ex
)
128 ex
._tao_print_exception ("Identity_Server::init");
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
];
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"));
174 result
= this->orb_manager_
.run ();
179 Identity_Server::~Identity_Server (void)
184 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
187 Identity_Server server
;
189 if (server
.init (argc
, argv
) == -1)
194 result
= server
.run ();
196 catch (const CORBA::Exception
& ex
)
198 ex
._tao_print_exception ("Identity_Server");