5 #include "ace/OS_NS_stdio.h"
7 LB_server::LB_server (int argc
, ACE_TCHAR
**argv
)
10 , ior_output_file_ (ACE_TEXT ("obj.ior"))
19 this->naming_manager_
->delete_object_group ("BasicGroup");
21 this->root_poa_
->destroy (1, 1);
23 this->orb_
->destroy ();
25 catch (const CORBA::Exception
& ex
)
27 ex
._tao_print_exception (
28 ACE_TEXT ("Exception caught while destroying LB_server\n"));
37 return this->orb_
.in ();
41 LB_server::object_group ()
43 return this->object_group_
.in ();
46 FT_Naming::NamingManager_ptr
47 LB_server::naming_manager ()
49 return this->naming_manager_
.in ();
53 LB_server::write_ior_to_file (const ACE_TCHAR
*file_name
,
57 ACE_OS::fopen (file_name
, "w");
62 ACE_TEXT ("Cannot open output file for writing IOR:")));
66 ACE_OS::fprintf (output_file
, "%s", ior
);
67 ACE_OS::fclose (output_file
);
72 LB_server::parse_args (int argc
, ACE_TCHAR
*argv
[])
74 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:"));
77 while ((c
= get_opts ()) != -1)
81 this->ior_output_file_
= get_opts
.opt_arg ();
85 ACE_ERROR_RETURN ((LM_ERROR
,
86 ACE_TEXT ("usage: %s ")
87 ACE_TEXT ("-o <iorfile>")
92 // Indicates successful parsing of the command line
97 LB_server::start_orb_and_poa ()
101 // Initialise the ORB.
102 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
104 CORBA::Object_var poa_object
=
105 this->orb_
->resolve_initial_references ("RootPOA");
107 if (CORBA::is_nil (poa_object
.in ()))
108 ACE_ERROR_RETURN ((LM_ERROR
,
109 ACE_TEXT (" (%P|%t) Unable to initialize ")
110 ACE_TEXT ("the POA.\n")),
113 this->root_poa_
= PortableServer::POA::_narrow (poa_object
.in ());
115 PortableServer::POAManager_var poa_manager
=
116 this->root_poa_
->the_POAManager ();
118 poa_manager
->activate ();
120 ACE_Time_Value
timeout (10); // Wait up to 10 seconds for the naming service
121 if (name_svc_
.init (this->orb_
.in (), &timeout
) != 0)
122 ACE_ERROR_RETURN ((LM_DEBUG
,
123 ACE_TEXT (" (%P|%t) LB_server: Could not connect ")
124 ACE_TEXT ("to naming service.\n")),
127 CORBA::Object_var obj
= this->orb_
->resolve_initial_references (
130 this->naming_manager_
=
131 FT_Naming::NamingManager::_narrow (obj
.in ());
133 if (CORBA::is_nil (this->naming_manager_
.in ()))
134 ACE_ERROR_RETURN ((LM_ERROR
,
135 ACE_TEXT (" (%P|%t) Unable to get Naming Manager ")
136 ACE_TEXT ("Reference.\n")),
139 catch (const CORBA::Exception
& ex
)
141 ex
._tao_print_exception (
142 ACE_TEXT ("Exception raised initialising ORB or POA"));
150 LB_server::create_object_group (const char *group_name
)
154 if (this->parse_args (argc_
, argv_
) != 0)
157 PortableGroup::Criteria
criteria (2);
160 PortableGroup::Property
&mem_style
= criteria
[0];
161 mem_style
.nam
.length (1);
163 // Set the membership style property
164 mem_style
.nam
[0].id
= CORBA::string_dup (
165 "org.omg.PortableGroup.MembershipStyle");
167 PortableGroup::MembershipStyleValue msv
=
168 PortableGroup::MEMB_APP_CTRL
;
169 mem_style
.val
<<= msv
;
171 ACE_DEBUG ((LM_DEBUG
,
172 ACE_TEXT ("(%P|%t) LB_server - creating the object group\n")));
174 this->object_group_
= this->naming_manager_
->create_object_group (
176 FT_Naming::ROUND_ROBIN
,
179 CORBA::String_var ior
=
180 this->orb_
->object_to_string (this->object_group_
.in ());
182 this->write_ior_to_file (this->ior_output_file_
, ior
.in ());
184 catch (const PortableGroup::ObjectNotCreated
&)
186 this->object_group_
=
187 this->naming_manager_
->get_object_group_ref_from_name (group_name
);
188 ACE_DEBUG ((LM_DEBUG
,
189 "(%P|%t) LB_server - object group already exists\n"));
192 catch (const CORBA::Exception
& ex
)
194 ex
._tao_print_exception (
195 ACE_TEXT ("Exception raised while creating object group"));
203 LB_server::register_servant (Basic
*servant
, const char *loc
)
207 Test::Basic_var basic
=
210 ACE_DEBUG ((LM_DEBUG
,
211 ACE_TEXT ("Writing ior to file: %C\n"),
213 CORBA::String_var ior
=
214 this->orb_
->object_to_string (basic
.in ());
216 this->write_ior_to_file (ACE_TEXT_CHAR_TO_TCHAR (loc
), ior
);
218 PortableGroup::Location
location (1);
221 location
[0].id
= CORBA::string_dup (loc
);
223 this->object_group_
=
224 this->naming_manager_
->add_member (this->object_group_
.in (),
228 catch (const PortableGroup::ObjectNotAdded
& )
230 ACE_DEBUG ((LM_DEBUG
,
231 "(%P|%t) Member was already added previously.\n"));
233 catch (const CORBA::Exception
& ex
)
235 ex
._tao_print_exception (
236 ACE_TEXT ("Exception raised while registering servant"));
244 LB_server::remove_servant (const char *loc
)
246 PortableGroup::Location
location (1);
249 location
[0].id
= CORBA::string_dup (loc
);
252 this->object_group_
=
253 this->naming_manager_
->remove_member (this->object_group_
.in (),
256 catch (const CORBA::Exception
& ex
)
258 ex
._tao_print_exception (
259 ACE_TEXT ("Exception raised while removing servant"));
267 LB_server::name_svc ()