Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / performance-tests / POA / Demux / demux_test_server.cpp
blob7f8ee4a8c99f0fe325f168f49279fa890fe612b8
1 //=============================================================================
2 /**
3 * @file demux_test_server.cpp
5 * @author Aniruddha Gokhale
6 */
7 //=============================================================================
10 #include "demux_test_server.h"
11 #include "tao/debug.h"
12 #include "ace/OS_NS_stdio.h"
13 #include "ace/OS_NS_string.h"
15 // Constructor
16 Demux_Test_Server::Demux_Test_Server (void)
17 : argc_ (0),
18 argv_ (0),
19 num_POAs_ (1),
20 num_objs_ (1),
21 poa_fp_ (0),
22 ior_fp_ (0),
23 servant_fp_ (0),
24 use_user_id_ (0),
25 use_transient_poas_ (0)
29 // destructor
30 Demux_Test_Server::~Demux_Test_Server (void)
32 ACE_OS::fclose (this->poa_fp_);
37 // initialize the Demux_Test_Server
40 int
41 Demux_Test_Server::init (int argc, ACE_TCHAR *argv [])
43 ACE_OS::printf ("here\n");
45 this->argc_ = argc;
46 this->argv_ = argv;
48 // Grab the ORB
49 try
51 // get the underlying ORB
52 this->orb_ =
53 CORBA::ORB_init (argc, argv);
55 catch (const CORBA::Exception& ex)
57 ex._tao_print_exception ("ORB_init");
58 throw;
61 // Grab the ROOT POA
62 try
64 CORBA::Object_var temp; // holder for the myriad of times we get
65 // an object which we then have to narrow.
66 // Get the Root POA
68 temp =
69 this->orb_->resolve_initial_references ("RootPOA");
70 if (CORBA::is_nil (temp.in ()))
71 ACE_ERROR_RETURN ((LM_ERROR,
72 "(%P|%t) Unable to get root poa reference.\n"),
73 1);
75 this->root_poa_ =
76 PortableServer::POA::_narrow (temp.in ());
78 catch (const CORBA::Exception& ex)
80 ex._tao_print_exception ("PortableServer::POA::_narrow");
81 throw;
84 // grab the POA Manager
85 try
88 this->poa_mgr_ =
89 this->root_poa_->the_POAManager ();
91 catch (const CORBA::Exception& ex)
93 ex._tao_print_exception ("RootPOA->the_POAManager");
94 throw;
97 // now parse the rest of the arguments to determine the POA depth, the number
98 // of objects with each POA and other info
100 ACE_DEBUG ((LM_DEBUG,
101 "Before Parse Args\n"));
103 if (this->parse_args () == -1)
104 ACE_ERROR_RETURN ((LM_ERROR,
105 "(%N:%l) Demux_Test_Server::init - "
106 "parse_args failed\n"),
107 -1);
109 // init the Policies used by all the POAs
110 CORBA::PolicyList policies (2);
114 // The id_uniqueness_policy by default is UNIQUE_ID. So each of our servants
115 // will have a unique name
117 policies.length (2);
119 // Choose the ID Policy for servants.
121 if (this->use_user_id_)
123 ACE_DEBUG ((LM_DEBUG,
124 "Using the USER_ID policy ...\n"));
126 policies[0] =
127 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
129 else
131 ACE_DEBUG ((LM_DEBUG,
132 "Using the SYSTEM_ID policy ...\n"));
134 policies[0] =
135 this->root_poa_->create_id_assignment_policy (PortableServer::SYSTEM_ID);
139 // Choose the LifeSpan Policy. Default is PERSISTENT.
140 if (this->use_transient_poas_)
142 ACE_DEBUG ((LM_DEBUG,
143 "Using the TRANSIENT Lifespan policy for the POAs\n"));
145 policies[1] =
146 this->root_poa_->create_lifespan_policy (PortableServer::TRANSIENT);
148 else
150 ACE_DEBUG ((LM_DEBUG,
151 "Using the PERSISTENT Lifespan policy for the POAs\n"));
153 policies[1] =
154 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
158 catch (const CORBA::Exception& ex)
160 ex._tao_print_exception ("creating policy");
161 throw;
164 // now create a POA hierarchy of the desired depth and populate each POA with
165 // the specified number of objects. Finally, activate these objects.
167 char poa_file [128];
169 // open the file that has all the POA names in it
170 if ((this->poa_fp_ = ACE_OS::fopen ("poa_names_100.dat", "r")) == 0)
172 ACE_ERROR_RETURN ((LM_ERROR,
173 " (%P|%t) Unable to open POA file %s\n", poa_file),
174 -1);
177 // Open the file that has the servant names in it.
178 if ((this->servant_fp_ = ACE_OS::fopen ("names_file", "r")) == 0)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 " (%P|%t) Unable to open POA file %s\n", poa_file),
182 -1);
185 // loop indices
186 CORBA::ULong i, j;
188 PortableServer::POA *prev_poa = this->root_poa_.in ();
189 for (i = 0; i < this->num_POAs_; i++)
191 char poa_name [128];
193 ACE_OS::memset (poa_name, 0, 128);
194 int n_matched = fscanf (this->poa_fp_, "%s", poa_name);
195 ACE_UNUSED_ARG (n_matched);
199 this->child_poa_[i] = prev_poa->create_POA (poa_name,
200 this->poa_mgr_.in (),
201 policies);
203 catch (const CORBA::Exception& ex)
205 ex._tao_print_exception ("create_POA");
206 throw;
209 for (j = 0; j < this->num_objs_; j++)
211 PortableServer::ObjectId_var id;
213 if (!use_user_id_)
215 // activate the object
218 Demux_Test_i * demux_test_i_ptr = 0;
219 ACE_NEW_RETURN (demux_test_i_ptr,
220 Demux_Test_i,
221 -1);
222 // POA will hold the servant
223 PortableServer::ServantBase_var owner (demux_test_i_ptr);
225 //id = this->child_poa_[i]->activate_object (&this->demux_test_[j],
226 id = this->child_poa_[i]->activate_object (demux_test_i_ptr);
228 catch (const CORBA::Exception& ex)
230 ex._tao_print_exception ("poa->activate_obj");
231 throw;
234 // Get the IOR and output it to the file
237 CORBA::Object_var demux_var = this->child_poa_[i]->id_to_reference (id.in ());
239 CORBA::String_var ior = this->orb_->object_to_string
240 (demux_var.in ());
243 ACE_OS::fprintf (this->ior_fp_, "%s\n", ior.in ());
245 catch (const CORBA::Exception& ex)
247 ex._tao_print_exception ("object_to_string");
248 throw;
251 else
253 // Use the USER_ID policy.
255 char servant_name [128];
259 Demux_Test_i * demux_test_i_ptr;
260 ACE_NEW_RETURN (demux_test_i_ptr,
261 Demux_Test_i (this->child_poa_[i].in ()),
262 -1);
264 ACE_OS::memset (servant_name, 0, 128);
266 n_matched = fscanf (this->servant_fp_, "%s", servant_name);
267 ACE_UNUSED_ARG (n_matched);
269 ACE_DEBUG ((LM_DEBUG,
270 "Activating Servant with Name : %s\n",
271 servant_name));
273 PortableServer::ObjectId_var oid =
274 PortableServer::string_to_ObjectId (servant_name);
276 this->child_poa_[i]->activate_object_with_id (oid.in (),
277 demux_test_i_ptr);
279 // Get Object reference for demux_test_i impl object.
280 CORBA::Object_var demux_var = demux_test_i_ptr->_this ();
283 CORBA::String_var ior = this->orb_->object_to_string
284 (demux_var.in ());
287 ACE_OS::fprintf (this->ior_fp_, "%s\n", ior.in ());
290 catch (const CORBA::Exception& ex)
292 ex._tao_print_exception ("object_to_string");
293 throw;
296 }// end of if (!use_user_id_)
298 } // j loop
300 prev_poa = this->child_poa_[i].in ();
302 } // i loop
304 ACE_OS::fclose (this->ior_fp_);
305 this->ior_fp_ = 0;
306 ACE_OS::fclose (this->servant_fp_);
308 // now activate the POAs
312 this->poa_mgr_->activate ();
315 catch (const CORBA::Exception& ex)
317 ex._tao_print_exception ("poa_mgr->activate");
318 throw;
321 // success
322 return 0;
326 // parse command line arguments (if any).
328 Demux_Test_Server::parse_args (void)
331 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("df:o:p:ut"));
332 int c;
334 while ((c = get_opts ()) != -1)
335 switch (c)
337 case 'd': // debug flag
338 TAO_debug_level++;
339 break;
340 case 'f':
341 this->ior_fp_ = ACE_OS::fopen (get_opts.opt_arg (), "w");
342 if (this->ior_fp_ == 0)
343 ACE_ERROR_RETURN ((LM_ERROR,
344 "Unable to open %s for writing: %p\n",
345 get_opts.opt_arg ()), -1);
346 break;
347 case 'o':
348 this->num_objs_ = ACE_OS::atoi (get_opts.opt_arg ());
349 if (this->num_objs_ > TAO_DEMUX_TEST_MAX_OBJS)
351 ACE_ERROR_RETURN ((LM_ERROR,
352 "%d exceeds the maximum of "
353 "%d objects per POA\n",
354 this->num_objs_,
355 TAO_DEMUX_TEST_MAX_OBJS),
356 -1);
358 break;
359 case 'p':
360 this->num_POAs_ = ACE_OS::atoi (get_opts.opt_arg ());
361 if (this->num_POAs_ > TAO_DEMUX_TEST_MAX_POAS)
363 ACE_ERROR_RETURN ((LM_ERROR,
364 "%d exceeds the maximum of "
365 "%d POAs\n",
366 this->num_objs_,
367 TAO_DEMUX_TEST_MAX_POAS),
368 -1);
370 break;
371 case 'u':
372 this->use_user_id_ = 1;
373 break;
374 case 't':
375 this->use_transient_poas_ = 1;
376 break;
377 case '?':
378 default:
379 ACE_ERROR_RETURN ((LM_ERROR,
380 "usage: %s"
381 " [-d]"
382 " [-o <num objects>]"
383 " [-p <num POAs>]"
384 " [-f <IOR file>]"
385 "\n", this->argv_ [0]),
386 -1);
389 if (!this->ior_fp_)
391 // open default IOR file
392 this->ior_fp_ = ACE_OS::fopen ("ior.dat", "w");
393 if (this->ior_fp_ == 0)
394 ACE_ERROR_RETURN ((LM_ERROR,
395 "Unable to open file ior.dat for writing\n"), -1);
397 return 0;
400 // grab a reference to the naming service so that we can register with it.
402 Demux_Test_Server::init_naming_service (void)
404 // Initialize the naming services
405 if (this->my_name_client_.init (this->orb_.in ()) == -1)
406 ACE_ERROR_RETURN ((LM_ERROR,
407 " (%P|%t) Unable to initialize "
408 "the ACE_Naming_Client.\n"),
409 -1);
411 // success
412 return 0;
415 // The main program for Demux_Test
417 Demux_Test_Server::run (void)
421 this->orb_->run ();
423 catch (const CORBA::Exception& ex)
425 ex._tao_print_exception ("run failed");
426 ACE_ERROR_RETURN ((LM_ERROR,
427 "(%N:%l) Demux_Test_Server::run - "
428 "Error running the server\n"),
429 -1);
432 ACE_TIMEPROBE_PRINT;
434 return 0;