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 ()
25 use_transient_poas_ (0)
30 Demux_Test_Server::~Demux_Test_Server ()
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
88 this->root_poa_
->the_POAManager ();
90 catch (const CORBA::Exception
& ex
)
92 ex
._tao_print_exception ("RootPOA->the_POAManager");
96 // now parse the rest of the arguments to determine the POA depth, the number
97 // of objects with each POA and other info
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"),
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
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"));
126 this->root_poa_
->create_id_assignment_policy (PortableServer::USER_ID
);
130 ACE_DEBUG ((LM_DEBUG
,
131 "Using the SYSTEM_ID policy ...\n"));
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"));
145 this->root_poa_
->create_lifespan_policy (PortableServer::TRANSIENT
);
149 ACE_DEBUG ((LM_DEBUG
,
150 "Using the PERSISTENT Lifespan policy for the POAs\n"));
153 this->root_poa_
->create_lifespan_policy (PortableServer::PERSISTENT
);
157 catch (const CORBA::Exception
& ex
)
159 ex
._tao_print_exception ("creating policy");
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.
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
),
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
),
187 PortableServer::POA
*prev_poa
= this->root_poa_
.in ();
188 for (i
= 0; i
< this->num_POAs_
; i
++)
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 (),
202 catch (const CORBA::Exception
& ex
)
204 ex
._tao_print_exception ("create_POA");
208 for (j
= 0; j
< this->num_objs_
; j
++)
210 PortableServer::ObjectId_var id
;
214 // activate the object
217 Demux_Test_i
* demux_test_i_ptr
= 0;
218 ACE_NEW_RETURN (demux_test_i_ptr
,
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");
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
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");
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 ()),
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",
272 PortableServer::ObjectId_var oid
=
273 PortableServer::string_to_ObjectId (servant_name
);
275 this->child_poa_
[i
]->activate_object_with_id (oid
.in (),
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
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");
294 }// end of if (!use_user_id_)
298 prev_poa
= this->child_poa_
[i
].in ();
301 ACE_OS::fclose (this->ior_fp_
);
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");
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"));
328 while ((c
= get_opts ()) != -1)
331 case 'd': // debug flag
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);
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",
349 TAO_DEMUX_TEST_MAX_OBJS
),
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 "
361 TAO_DEMUX_TEST_MAX_POAS
),
366 this->use_user_id_
= 1;
369 this->use_transient_poas_
= 1;
373 ACE_ERROR_RETURN ((LM_ERROR
,
376 " [-o <num objects>]"
379 "\n", this->argv_
[0]),
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);
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"),
409 // The main program for Demux_Test
411 Demux_Test_Server::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"),