Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_Naming / Load_Balancing / LB_server.cpp
blobed8a69d0724b0c8dd9cbee2ca5b66500b4270ebb
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 ("Basic Group");
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 char *ior)
55 FILE *output_file =
56 ACE_OS::fopen (this->ior_output_file_, "w");
58 if (output_file == 0)
60 ACE_ERROR ((LM_ERROR,
61 ACE_TEXT ("Cannot open output file for writing IOR:")));
62 return -1;
65 ACE_OS::fprintf (output_file, "%s", ior);
66 ACE_OS::fclose (output_file);
67 return 0;
70 int
71 LB_server::parse_args (int argc, ACE_TCHAR *argv[])
73 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
74 int c;
76 while ((c = get_opts ()) != -1)
77 switch (c)
79 case 'o':
80 this->ior_output_file_ = get_opts.opt_arg ();
81 break;
82 case '?':
83 default:
84 ACE_ERROR_RETURN ((LM_ERROR,
85 ACE_TEXT ("usage: %s ")
86 ACE_TEXT ("-o <iorfile>")
87 ACE_TEXT ("\n"),
88 argv [0]),
89 -1);
91 // Indicates successful parsing of the command line
92 return 0;
95 int
96 LB_server::start_orb_and_poa ()
98 try
100 // Initialise the ORB.
101 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
103 CORBA::Object_var poa_object =
104 this->orb_->resolve_initial_references("RootPOA");
106 if (CORBA::is_nil (poa_object.in ()))
107 ACE_ERROR_RETURN ((LM_ERROR,
108 ACE_TEXT (" (%P|%t) Unable to initialize the POA.\n")),
109 -1);
111 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
113 PortableServer::POAManager_var poa_manager =
114 this->root_poa_->the_POAManager ();
116 poa_manager->activate ();
118 ACE_Time_Value timeout (10); // Wait up to 10 seconds for the naming service
119 if (name_svc_.init (this->orb_.in (), &timeout) != 0)
120 ACE_ERROR_RETURN ((LM_DEBUG,
121 ACE_TEXT ("LB_server: Could not connect to naming ")
122 ACE_TEXT ("service.\n")),
123 -1);
125 CORBA::Object_var obj =
126 this->orb_->resolve_initial_references ("NamingManager");
128 this->naming_manager_ =
129 FT_Naming::NamingManager::_narrow (obj.in ());
131 if (CORBA::is_nil (this->naming_manager_.in ()))
132 ACE_ERROR_RETURN ((LM_ERROR,
133 ACE_TEXT (" (%P|%t) Unable to get Naming ")
134 ACE_TEXT ("Manager Reference\n")),
135 -1);
137 catch (const CORBA::Exception& ex)
139 ex._tao_print_exception (
140 ACE_TEXT ("Exception raised initialising ORB or POA"));
141 return -1;
144 return 0;
148 LB_server::create_object_group ()
152 if (this->parse_args (argc_, argv_) != 0)
153 return -1;
155 PortableGroup::Criteria criteria (1);
156 criteria.length (1);
158 PortableGroup::Property &mem_style = criteria[0];
159 mem_style.nam.length (1);
161 // Set the membership style property
162 mem_style.nam[0].id =
163 CORBA::string_dup ("org.omg.PortableGroup.MembershipStyle");
164 PortableGroup::MembershipStyleValue msv =
165 PortableGroup::MEMB_APP_CTRL;
166 mem_style.val <<= msv;
168 this->object_group_ = this->naming_manager_->create_object_group (
169 "Basic Group",
170 FT_Naming::ROUND_ROBIN,
171 criteria);
173 CORBA::String_var ior =
174 this->orb_->object_to_string (this->object_group_.in ());
176 this->write_ior_to_file (ior.in ());
178 catch (const CORBA::Exception& ex)
180 ex._tao_print_exception (
181 ACE_TEXT ("Exception raised while creating object group"));
182 return -1;
185 return 0;
189 LB_server::register_servant (Basic *servant, const char *loc)
193 Test::Basic_var basic =
194 servant->_this ();
196 PortableGroup::Location location (1);
197 location.length (1);
199 location[0].id = CORBA::string_dup (loc);
201 this->object_group_ =
202 this->naming_manager_->add_member (this->object_group_.in (),
203 location,
204 basic.in ());
206 catch (const CORBA::Exception& ex)
208 ex._tao_print_exception (
209 ACE_TEXT ("Exception raised while registering servant"));
210 return -1;
213 return 0;
216 TAO_Naming_Client&
217 LB_server::name_svc ()
219 return name_svc_;