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");
60 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
62 // Activate POA Manager
63 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
66 // Find the Naming Service
67 obj
= orb_
->string_to_object ("corbaloc:iiop:1.2@localhost:9932/NameService");
68 CosNaming::NamingContext_var root
=
69 CosNaming::NamingContext::_narrow(obj
.in());
71 if (CORBA::is_nil(root
.in())) {
72 ACE_ERROR ((LM_ERROR
, "Error, Nil Naming Context reference\n"));
75 // Bind the example Naming Context, if necessary
76 CosNaming::NamingContext_var example_nc
;
79 name
[0].id
= CORBA::string_dup("example");
82 obj
= root
->resolve(name
);
84 CosNaming::NamingContext::_narrow(obj
.in());
86 catch (const CosNaming::NamingContext::NotFound
&)
88 example_nc
= root
->bind_new_context(name
);
91 // Bind the Test object
93 name
[1].id
= CORBA::string_dup("Hello");
96 Hello
servant(orb_
.in ());
97 PortableServer::ObjectId_var oid
= poa
->activate_object(&servant
);
98 obj
= poa
->id_to_reference(oid
.in());
99 root
->rebind(name
, obj
.in());
101 ACE_DEBUG ((LM_INFO
, "Hello object bound in Naming Service B\n"));
104 obj
= orb_
->string_to_object ("corbaloc:iiop:1.2@localhost:9931/NameService");
105 root
= CosNaming::NamingContext::_narrow(obj
.in());
106 root
->bind_context (name
, example_nc
.in ());
108 ACE_DEBUG ((LM_INFO
, "'example' context of NS B bound in Naming Service A\n"));
110 CORBA::String_var ior
=
111 orb_
->object_to_string (obj
.in ());
113 // Output the IOR to the <ior_output_file>
114 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
115 if (output_file
== 0)
116 ACE_ERROR_RETURN ((LM_ERROR
,
117 "Cannot open output file %s for writing IOR: %C\n",
121 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
122 ACE_OS::fclose (output_file
);
124 ACE_DEBUG ((LM_INFO
, "Wrote IOR file\n"));
126 // Normally we run the orb and the orb is shutdown by
127 // calling TestTask::end().
134 catch (const CORBA::Exception
& ex
)
136 ex
._tao_print_exception ("CORBA exception: ");
142 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
144 // Start the Test task
145 TestTask
test_ (argc
, argv
);
146 if (test_
.activate() == -1)
148 ACE_ERROR_RETURN ((LM_ERROR
, "Unable to start test task.\n"), -1);
151 // Wait the tasks to finish.
152 test_
.thr_mgr ()->wait();