Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_Naming / FaultTolerant / LB_server.cpp
blobcc9e81f24348f37e65e12fb21dff758214597493
1 #include "LB_server.h"
2 #include "Basic.h"
4 #include "TestC.h"
5 #include "ace/OS_NS_stdio.h"
7 LB_server::LB_server (int argc, ACE_TCHAR **argv)
8 : argc_ (argc)
9 , argv_ (argv)
10 , ior_output_file_ (ACE_TEXT ("obj.ior"))
14 int
15 LB_server::destroy ()
17 try
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"));
29 return -1;
31 return 0;
34 CORBA::ORB_ptr
35 LB_server::orb ()
37 return this->orb_.in ();
40 CORBA::Object_ptr
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 ();
52 int
53 LB_server::write_ior_to_file (const ACE_TCHAR *file_name,
54 const char *ior)
56 FILE *output_file =
57 ACE_OS::fopen (file_name, "w");
59 if (output_file == 0)
61 ACE_ERROR ((LM_ERROR,
62 ACE_TEXT ("Cannot open output file for writing IOR:")));
63 return -1;
66 ACE_OS::fprintf (output_file, "%s", ior);
67 ACE_OS::fclose (output_file);
68 return 0;
71 int
72 LB_server::parse_args (int argc, ACE_TCHAR *argv[])
74 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
75 int c;
77 while ((c = get_opts ()) != -1)
78 switch (c)
80 case 'o':
81 this->ior_output_file_ = get_opts.opt_arg ();
82 break;
83 case '?':
84 default:
85 ACE_ERROR_RETURN ((LM_ERROR,
86 ACE_TEXT ("usage: %s ")
87 ACE_TEXT ("-o <iorfile>")
88 ACE_TEXT ("\n"),
89 argv [0]),
90 -1);
92 // Indicates successful parsing of the command line
93 return 0;
96 int
97 LB_server::start_orb_and_poa ()
99 try
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")),
111 -1);
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")),
125 -1);
127 CORBA::Object_var obj = this->orb_->resolve_initial_references (
128 "NamingManager");
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")),
137 -1);
139 catch (const CORBA::Exception& ex)
141 ex._tao_print_exception (
142 ACE_TEXT ("Exception raised initialising ORB or POA"));
143 return -1;
146 return 0;
150 LB_server::create_object_group (const char *group_name)
154 if (this->parse_args (argc_, argv_) != 0)
155 return -1;
157 PortableGroup::Criteria criteria (2);
158 criteria.length (1);
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 (
175 group_name,
176 FT_Naming::ROUND_ROBIN,
177 criteria);
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"));
190 return 1;
192 catch (const CORBA::Exception& ex)
194 ex._tao_print_exception (
195 ACE_TEXT ("Exception raised while creating object group"));
196 return -1;
199 return 0;
203 LB_server::register_servant (Basic *servant, const char *loc)
207 Test::Basic_var basic =
208 servant->_this ();
210 ACE_DEBUG ((LM_DEBUG,
211 ACE_TEXT ("Writing ior to file: %C\n"),
212 loc));
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);
219 location.length (1);
221 location[0].id = CORBA::string_dup (loc);
223 this->object_group_ =
224 this->naming_manager_->add_member (this->object_group_.in (),
225 location,
226 basic.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"));
237 return -1;
240 return 0;
244 LB_server::remove_servant (const char *loc)
246 PortableGroup::Location location (1);
247 location.length (1);
249 location[0].id = CORBA::string_dup (loc);
251 try {
252 this->object_group_ =
253 this->naming_manager_->remove_member (this->object_group_.in (),
254 location);
256 catch (const CORBA::Exception& ex)
258 ex._tao_print_exception (
259 ACE_TEXT ("Exception raised while removing servant"));
260 return -1;
263 return 0;
266 TAO_Naming_Client&
267 LB_server::name_svc ()
269 return name_svc_;