1 //=============================================================================
3 * @file demux_test_server.cpp
5 * @author Aniruddha Gokhale
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"
16 Demux_Test_Server::Demux_Test_Server (void)
25 use_transient_poas_ (0)
30 Demux_Test_Server::~Demux_Test_Server (void)
32 ACE_OS::fclose (this->poa_fp_
);
37 // initialize the Demux_Test_Server
41 Demux_Test_Server::init (int argc
, ACE_TCHAR
*argv
[])
43 ACE_OS::printf ("here\n");
51 // get the underlying ORB
53 CORBA::ORB_init (argc
, argv
);
55 catch (const CORBA::Exception
& ex
)
57 ex
._tao_print_exception ("ORB_init");
64 CORBA::Object_var temp
; // holder for the myriad of times we get
65 // an object which we then have to narrow.
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"),
76 PortableServer::POA::_narrow (temp
.in ());
78 catch (const CORBA::Exception
& ex
)
80 ex
._tao_print_exception ("PortableServer::POA::_narrow");
84 // grab the POA Manager
89 this->root_poa_
->the_POAManager ();
91 catch (const CORBA::Exception
& ex
)
93 ex
._tao_print_exception ("RootPOA->the_POAManager");
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"),
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
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"));
127 this->root_poa_
->create_id_assignment_policy (PortableServer::USER_ID
);
131 ACE_DEBUG ((LM_DEBUG
,
132 "Using the SYSTEM_ID policy ...\n"));
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"));
146 this->root_poa_
->create_lifespan_policy (PortableServer::TRANSIENT
);
150 ACE_DEBUG ((LM_DEBUG
,
151 "Using the PERSISTENT Lifespan policy for the POAs\n"));
154 this->root_poa_
->create_lifespan_policy (PortableServer::PERSISTENT
);
158 catch (const CORBA::Exception
& ex
)
160 ex
._tao_print_exception ("creating policy");
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.
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
),
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
),
188 PortableServer::POA
*prev_poa
= this->root_poa_
.in ();
189 for (i
= 0; i
< this->num_POAs_
; i
++)
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 (),
203 catch (const CORBA::Exception
& ex
)
205 ex
._tao_print_exception ("create_POA");
209 for (j
= 0; j
< this->num_objs_
; j
++)
211 PortableServer::ObjectId_var id
;
215 // activate the object
218 Demux_Test_i
* demux_test_i_ptr
= 0;
219 ACE_NEW_RETURN (demux_test_i_ptr
,
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");
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
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");
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 ()),
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",
273 PortableServer::ObjectId_var oid
=
274 PortableServer::string_to_ObjectId (servant_name
);
276 this->child_poa_
[i
]->activate_object_with_id (oid
.in (),
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
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");
296 }// end of if (!use_user_id_)
300 prev_poa
= this->child_poa_
[i
].in ();
304 ACE_OS::fclose (this->ior_fp_
);
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");
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"));
334 while ((c
= get_opts ()) != -1)
337 case 'd': // debug flag
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);
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",
355 TAO_DEMUX_TEST_MAX_OBJS
),
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 "
367 TAO_DEMUX_TEST_MAX_POAS
),
372 this->use_user_id_
= 1;
375 this->use_transient_poas_
= 1;
379 ACE_ERROR_RETURN ((LM_ERROR
,
382 " [-o <num objects>]"
385 "\n", this->argv_
[0]),
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);
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"),
415 // The main program for Demux_Test
417 Demux_Test_Server::run (void)
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"),