2 #include "orbsvcs/CosNamingC.h"
3 #include "ace/Get_Opt.h"
6 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("test.ior");
8 class TestTask
: public ACE_Task_Base
11 TestTask (int argc
, ACE_TCHAR
**argv
);
14 int parse_args (int argc
, ACE_TCHAR
**argv
);
19 CORBA::Boolean shutdown_ns_
;
22 TestTask::TestTask(int argc
, ACE_TCHAR
**argv
)
24 orb_
= CORBA::ORB_init (argc
, argv
, "ServerORB");
26 parse_args (argc
, argv
);
36 TestTask::parse_args (int argc
, ACE_TCHAR
**argv
)
38 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT ("o:s"));
41 while ((c
= get_opts ()) != -1)
48 ior_output_file
= get_opts
.opt_arg ();
51 // Indicates successful parsing of the command line
59 // Get reference to Root POA
60 CORBA::Object_var obj
= orb_
->resolve_initial_references ("RootPOA");
62 PortableServer::POA_var poa
= PortableServer::POA::_narrow (obj
.in ());
64 // Activate POA Manager
65 PortableServer::POAManager_var mgr
= poa
->the_POAManager ();
68 // Find the Naming Service
69 obj
= orb_
->string_to_object (
70 "corbaloc:iiop:1.2@localhost:9932/NameService");
72 CosNaming::NamingContext_var rootB
=
73 CosNaming::NamingContext::_narrow (obj
.in ());
75 if (CORBA::is_nil (rootB
.in ())) {
77 ACE_TEXT ("Error, Nil Naming Context reference\n")));
80 // Bind the example Naming Context, if necessary
81 CosNaming::NamingContext_var example_nc
;
84 name
[0].id
= CORBA::string_dup( "example");
87 obj
= rootB
->resolve (name
);
89 CosNaming::NamingContext::_narrow (obj
.in ());
91 catch (const CosNaming::NamingContext::NotFound
&)
93 example_nc
= rootB
->bind_new_context (name
);
96 // Bind the Test object
98 name
[1].id
= CORBA::string_dup ("Hello");
101 Hello
servant (orb_
.in ());
102 PortableServer::ObjectId_var oid
= poa
->activate_object (&servant
);
103 obj
= poa
->id_to_reference (oid
.in ());
104 rootB
->rebind (name
, obj
.in ());
107 ACE_TEXT ("Hello object bound in Naming Service B\n")));
110 name
[0].id
= CORBA::string_dup ("nsB");
112 obj
= orb_
->string_to_object (
113 "corbaloc:iiop:1.2@localhost:9931/NameService");
115 CosNaming::NamingContext_var rootA
=
116 CosNaming::NamingContext::_narrow (obj
.in ());
118 rootA
->bind_context (name
, rootB
.in ());
121 ACE_TEXT ("Root context of NS B bound in Naming Service A ")
122 ACE_TEXT ("under name 'nsB'\n")));
124 CORBA::String_var ior
=
125 orb_
->object_to_string (obj
.in ());
127 // Output the IOR to the <ior_output_file>
128 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
129 if (output_file
== 0)
130 ACE_ERROR_RETURN ((LM_ERROR
,
131 ACE_TEXT ("Cannot open output file %s for writing ")
132 ACE_TEXT ("IOR: %C\n"),
136 ACE_OS::fprintf (output_file
, ACE_TEXT ("%s"), ior
.in ());
137 ACE_OS::fclose (output_file
);
140 ACE_TEXT ("Wrote IOR file\n")));
142 // Normally we run the orb and the orb is shutdown by
143 // calling TestTask::end().
150 catch (const CORBA::Exception
& ex
)
152 ex
._tao_print_exception (ACE_TEXT ("CORBA exception: "));
158 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
160 // Start the Test task
161 TestTask
test_ (argc
, argv
);
162 if (test_
.activate() == -1)
164 ACE_ERROR_RETURN ((LM_ERROR
,
165 ACE_TEXT ("Unable to start test task.\n")),
169 // Wait the tasks to finish.
170 test_
.thr_mgr ()->wait();