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
58 // Get reference to Root POA
59 CORBA::Object_var obj
= orb_
->resolve_initial_references ("RootPOA");
61 PortableServer::POA_var poa
= PortableServer::POA::_narrow (obj
.in ());
63 // Activate POA Manager
64 PortableServer::POAManager_var mgr
= poa
->the_POAManager ();
67 // Find the Naming Service
68 obj
= orb_
->string_to_object (
69 "corbaloc:iiop:1.2@localhost:9932/NameService");
71 CosNaming::NamingContext_var rootB
=
72 CosNaming::NamingContext::_narrow (obj
.in ());
74 if (CORBA::is_nil (rootB
.in ())) {
76 ACE_TEXT ("Error, Nil Naming Context reference\n")));
79 // Bind the example Naming Context, if necessary
80 CosNaming::NamingContext_var example_nc
;
83 name
[0].id
= CORBA::string_dup( "example");
86 obj
= rootB
->resolve (name
);
88 CosNaming::NamingContext::_narrow (obj
.in ());
90 catch (const CosNaming::NamingContext::NotFound
&)
92 example_nc
= rootB
->bind_new_context (name
);
95 // Bind the Test object
97 name
[1].id
= CORBA::string_dup ("Hello");
100 Hello
servant (orb_
.in ());
101 PortableServer::ObjectId_var oid
= poa
->activate_object (&servant
);
102 obj
= poa
->id_to_reference (oid
.in ());
103 rootB
->rebind (name
, obj
.in ());
106 ACE_TEXT ("Hello object bound in Naming Service B\n")));
109 name
[0].id
= CORBA::string_dup ("nsB");
111 obj
= orb_
->string_to_object (
112 "corbaloc:iiop:1.2@localhost:9931/NameService");
114 CosNaming::NamingContext_var rootA
=
115 CosNaming::NamingContext::_narrow (obj
.in ());
117 rootA
->bind_context (name
, rootB
.in ());
120 ACE_TEXT ("Root context of NS B bound in Naming Service A ")
121 ACE_TEXT ("under name 'nsB'\n")));
123 CORBA::String_var ior
=
124 orb_
->object_to_string (obj
.in ());
126 // Output the IOR to the <ior_output_file>
127 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
128 if (output_file
== 0)
129 ACE_ERROR_RETURN ((LM_ERROR
,
130 ACE_TEXT ("Cannot open output file %s for writing ")
131 ACE_TEXT ("IOR: %C\n"),
135 ACE_OS::fprintf (output_file
, ACE_TEXT ("%s"), ior
.in ());
136 ACE_OS::fclose (output_file
);
139 ACE_TEXT ("Wrote IOR file\n")));
141 // Normally we run the orb and the orb is shutdown by
142 // calling TestTask::end().
149 catch (const CORBA::Exception
& ex
)
151 ex
._tao_print_exception (ACE_TEXT ("CORBA exception: "));
157 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
159 // Start the Test task
160 TestTask
test_ (argc
, argv
);
161 if (test_
.activate() == -1)
163 ACE_ERROR_RETURN ((LM_ERROR
,
164 ACE_TEXT ("Unable to start test task.\n")),
168 // Wait the tasks to finish.
169 test_
.thr_mgr ()->wait();