Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / performance-tests / POA / Demux / demux_test_server.cpp
blobb05872f9bb68c95527ea74fe33fb1603a15267eb
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 ()
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 ()
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
87 this->poa_mgr_ =
88 this->root_poa_->the_POAManager ();
90 catch (const CORBA::Exception& ex)
92 ex._tao_print_exception ("RootPOA->the_POAManager");
93 throw;
96 // now parse the rest of the arguments to determine the POA depth, the number
97 // of objects with each POA and other info
99 ACE_DEBUG ((LM_DEBUG,
100 "Before Parse Args\n"));
102 if (this->parse_args () == -1)
103 ACE_ERROR_RETURN ((LM_ERROR,
104 "(%N:%l) Demux_Test_Server::init - "
105 "parse_args failed\n"),
106 -1);
108 // init the Policies used by all the POAs
109 CORBA::PolicyList policies (2);
113 // The id_uniqueness_policy by default is UNIQUE_ID. So each of our servants
114 // will have a unique name
116 policies.length (2);
118 // Choose the ID Policy for servants.
120 if (this->use_user_id_)
122 ACE_DEBUG ((LM_DEBUG,
123 "Using the USER_ID policy ...\n"));
125 policies[0] =
126 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
128 else
130 ACE_DEBUG ((LM_DEBUG,
131 "Using the SYSTEM_ID policy ...\n"));
133 policies[0] =
134 this->root_poa_->create_id_assignment_policy (PortableServer::SYSTEM_ID);
138 // Choose the LifeSpan Policy. Default is PERSISTENT.
139 if (this->use_transient_poas_)
141 ACE_DEBUG ((LM_DEBUG,
142 "Using the TRANSIENT Lifespan policy for the POAs\n"));
144 policies[1] =
145 this->root_poa_->create_lifespan_policy (PortableServer::TRANSIENT);
147 else
149 ACE_DEBUG ((LM_DEBUG,
150 "Using the PERSISTENT Lifespan policy for the POAs\n"));
152 policies[1] =
153 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
157 catch (const CORBA::Exception& ex)
159 ex._tao_print_exception ("creating policy");
160 throw;
163 // now create a POA hierarchy of the desired depth and populate each POA with
164 // the specified number of objects. Finally, activate these objects.
166 char poa_file [128];
168 // open the file that has all the POA names in it
169 if ((this->poa_fp_ = ACE_OS::fopen ("poa_names_100.dat", "r")) == 0)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 " (%P|%t) Unable to open POA file %s\n", poa_file),
173 -1);
176 // Open the file that has the servant names in it.
177 if ((this->servant_fp_ = ACE_OS::fopen ("names_file", "r")) == 0)
179 ACE_ERROR_RETURN ((LM_ERROR,
180 " (%P|%t) Unable to open POA file %s\n", poa_file),
181 -1);
184 // loop indices
185 CORBA::ULong i, j;
187 PortableServer::POA *prev_poa = this->root_poa_.in ();
188 for (i = 0; i < this->num_POAs_; i++)
190 char poa_name [128];
192 ACE_OS::memset (poa_name, 0, 128);
193 int n_matched = fscanf (this->poa_fp_, "%s", poa_name);
194 ACE_UNUSED_ARG (n_matched);
198 this->child_poa_[i] = prev_poa->create_POA (poa_name,
199 this->poa_mgr_.in (),
200 policies);
202 catch (const CORBA::Exception& ex)
204 ex._tao_print_exception ("create_POA");
205 throw;
208 for (j = 0; j < this->num_objs_; j++)
210 PortableServer::ObjectId_var id;
212 if (!use_user_id_)
214 // activate the object
217 Demux_Test_i * demux_test_i_ptr = 0;
218 ACE_NEW_RETURN (demux_test_i_ptr,
219 Demux_Test_i,
220 -1);
221 // POA will hold the servant
222 PortableServer::ServantBase_var owner (demux_test_i_ptr);
224 //id = this->child_poa_[i]->activate_object (&this->demux_test_[j],
225 id = this->child_poa_[i]->activate_object (demux_test_i_ptr);
227 catch (const CORBA::Exception& ex)
229 ex._tao_print_exception ("poa->activate_obj");
230 throw;
233 // Get the IOR and output it to the file
236 CORBA::Object_var demux_var = this->child_poa_[i]->id_to_reference (id.in ());
238 CORBA::String_var ior = this->orb_->object_to_string
239 (demux_var.in ());
242 ACE_OS::fprintf (this->ior_fp_, "%s\n", ior.in ());
244 catch (const CORBA::Exception& ex)
246 ex._tao_print_exception ("object_to_string");
247 throw;
250 else
252 // Use the USER_ID policy.
254 char servant_name [128];
258 Demux_Test_i * demux_test_i_ptr;
259 ACE_NEW_RETURN (demux_test_i_ptr,
260 Demux_Test_i (this->child_poa_[i].in ()),
261 -1);
263 ACE_OS::memset (servant_name, 0, 128);
265 n_matched = fscanf (this->servant_fp_, "%s", servant_name);
266 ACE_UNUSED_ARG (n_matched);
268 ACE_DEBUG ((LM_DEBUG,
269 "Activating Servant with Name : %s\n",
270 servant_name));
272 PortableServer::ObjectId_var oid =
273 PortableServer::string_to_ObjectId (servant_name);
275 this->child_poa_[i]->activate_object_with_id (oid.in (),
276 demux_test_i_ptr);
278 // Get Object reference for demux_test_i impl object.
279 CORBA::Object_var demux_var = demux_test_i_ptr->_this ();
282 CORBA::String_var ior = this->orb_->object_to_string
283 (demux_var.in ());
286 ACE_OS::fprintf (this->ior_fp_, "%s\n", ior.in ());
288 catch (const CORBA::Exception& ex)
290 ex._tao_print_exception ("object_to_string");
291 throw;
294 }// end of if (!use_user_id_)
296 } // j loop
298 prev_poa = this->child_poa_[i].in ();
299 } // i loop
301 ACE_OS::fclose (this->ior_fp_);
302 this->ior_fp_ = 0;
303 ACE_OS::fclose (this->servant_fp_);
305 // now activate the POAs
309 this->poa_mgr_->activate ();
311 catch (const CORBA::Exception& ex)
313 ex._tao_print_exception ("poa_mgr->activate");
314 throw;
317 // success
318 return 0;
321 // parse command line arguments (if any).
323 Demux_Test_Server::parse_args ()
325 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("df:o:p:ut"));
326 int c;
328 while ((c = get_opts ()) != -1)
329 switch (c)
331 case 'd': // debug flag
332 TAO_debug_level++;
333 break;
334 case 'f':
335 this->ior_fp_ = ACE_OS::fopen (get_opts.opt_arg (), "w");
336 if (this->ior_fp_ == 0)
337 ACE_ERROR_RETURN ((LM_ERROR,
338 "Unable to open %s for writing: %p\n",
339 get_opts.opt_arg ()), -1);
340 break;
341 case 'o':
342 this->num_objs_ = ACE_OS::atoi (get_opts.opt_arg ());
343 if (this->num_objs_ > TAO_DEMUX_TEST_MAX_OBJS)
345 ACE_ERROR_RETURN ((LM_ERROR,
346 "%d exceeds the maximum of "
347 "%d objects per POA\n",
348 this->num_objs_,
349 TAO_DEMUX_TEST_MAX_OBJS),
350 -1);
352 break;
353 case 'p':
354 this->num_POAs_ = ACE_OS::atoi (get_opts.opt_arg ());
355 if (this->num_POAs_ > TAO_DEMUX_TEST_MAX_POAS)
357 ACE_ERROR_RETURN ((LM_ERROR,
358 "%d exceeds the maximum of "
359 "%d POAs\n",
360 this->num_objs_,
361 TAO_DEMUX_TEST_MAX_POAS),
362 -1);
364 break;
365 case 'u':
366 this->use_user_id_ = 1;
367 break;
368 case 't':
369 this->use_transient_poas_ = 1;
370 break;
371 case '?':
372 default:
373 ACE_ERROR_RETURN ((LM_ERROR,
374 "usage: %s"
375 " [-d]"
376 " [-o <num objects>]"
377 " [-p <num POAs>]"
378 " [-f <IOR file>]"
379 "\n", this->argv_ [0]),
380 -1);
383 if (!this->ior_fp_)
385 // open default IOR file
386 this->ior_fp_ = ACE_OS::fopen ("ior.dat", "w");
387 if (this->ior_fp_ == 0)
388 ACE_ERROR_RETURN ((LM_ERROR,
389 "Unable to open file ior.dat for writing\n"), -1);
391 return 0;
394 // grab a reference to the naming service so that we can register with it.
396 Demux_Test_Server::init_naming_service ()
398 // Initialize the naming services
399 if (this->my_name_client_.init (this->orb_.in ()) == -1)
400 ACE_ERROR_RETURN ((LM_ERROR,
401 " (%P|%t) Unable to initialize "
402 "the ACE_Naming_Client.\n"),
403 -1);
405 // success
406 return 0;
409 // The main program for Demux_Test
411 Demux_Test_Server::run ()
415 this->orb_->run ();
417 catch (const CORBA::Exception& ex)
419 ex._tao_print_exception ("run failed");
420 ACE_ERROR_RETURN ((LM_ERROR,
421 "(%N:%l) Demux_Test_Server::run - "
422 "Error running the server\n"),
423 -1);
426 ACE_TIMEPROBE_PRINT;
428 return 0;