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");
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 ("corbaloc:iiop:1.2@localhost:9932/NameService");
69 CosNaming::NamingContext_var root
=
70 CosNaming::NamingContext::_narrow(obj
.in());
72 if (CORBA::is_nil(root
.in())) {
73 ACE_ERROR ((LM_ERROR
, "Error, Nil Naming Context reference\n"));
76 // Bind the example Naming Context, if necessary
77 CosNaming::NamingContext_var example_nc
;
80 name
[0].id
= CORBA::string_dup("example");
83 obj
= root
->resolve(name
);
85 CosNaming::NamingContext::_narrow(obj
.in());
87 catch (const CosNaming::NamingContext::NotFound
&)
89 example_nc
= root
->bind_new_context(name
);
92 // Bind the Test object
94 name
[1].id
= CORBA::string_dup("Hello");
97 Hello
servant(orb_
.in ());
98 PortableServer::ObjectId_var oid
= poa
->activate_object(&servant
);
99 obj
= poa
->id_to_reference(oid
.in());
100 root
->rebind(name
, obj
.in());
102 ACE_DEBUG ((LM_INFO
, "Hello object bound in Naming Service B\n"));
105 obj
= orb_
->string_to_object ("corbaloc:iiop:1.2@localhost:9931/NameService");
106 root
= CosNaming::NamingContext::_narrow(obj
.in());
107 root
->bind_context (name
, example_nc
.in ());
109 ACE_DEBUG ((LM_INFO
, "'example' context of NS B bound in Naming Service A\n"));
111 CORBA::String_var ior
=
112 orb_
->object_to_string (obj
.in ());
114 // Output the IOR to the <ior_output_file>
115 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
116 if (output_file
== 0)
117 ACE_ERROR_RETURN ((LM_ERROR
,
118 "Cannot open output file %s for writing IOR: %C\n",
122 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
123 ACE_OS::fclose (output_file
);
125 ACE_DEBUG ((LM_INFO
, "Wrote IOR file\n"));
127 // Normally we run the orb and the orb is shutdown by
128 // calling TestTask::end().
135 catch (const CORBA::Exception
& ex
)
137 ex
._tao_print_exception ("CORBA exception: ");
143 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
145 // Start the Test task
146 TestTask
test_ (argc
, argv
);
147 if (test_
.activate() == -1)
149 ACE_ERROR_RETURN ((LM_ERROR
, "Unable to start test task.\n"), -1);
152 // Wait the tasks to finish.
153 test_
.thr_mgr ()->wait();