1 #include "NamingTask.h"
3 #include "NsShutdown.h"
4 #include "orbsvcs/CosNamingC.h"
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR
*ior_output_file
= ACE_TEXT("ns.ior");
8 const ACE_TCHAR
*ior_shutdown_file
= ACE_TEXT("shutdown.ior");
10 class TestTask
: public ACE_Task_Base
13 TestTask (int argc
, ACE_TCHAR
**argv
);
16 int parse_args (int argc
, ACE_TCHAR
**argv
);
20 NamingTask namingServiceA_
;
21 NamingTask namingServiceB_
;
23 CORBA::Boolean shutdown_ns_
;
26 TestTask::TestTask (int argc
, ACE_TCHAR
**argv
)
27 : namingServiceA_("NamingORBA", argc
, argv
, 9931),
28 namingServiceB_("NamingORBB", argc
, argv
, 9932)
30 orb_
= CORBA::ORB_init (argc
, argv
, "ServerORB");
32 parse_args (argc
, argv
);
42 TestTask::parse_args (int argc
, ACE_TCHAR
**argv
)
44 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:s:"));
47 while ((c
= get_opts ()) != -1)
51 ior_output_file
= get_opts
.opt_arg ();
54 ior_shutdown_file
= get_opts
.opt_arg ();
58 // Indicates successful parsing of the command line
66 // Start the Naming Service tasks
67 namingServiceA_
.activate();
68 // Wait for the Naming Service initialized.
69 namingServiceA_
.waitInit();
71 namingServiceB_
.activate();
72 // Wait for the Naming Service initialized.
73 namingServiceB_
.waitInit();
75 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
77 ACE_ERROR_RETURN ((LM_ERROR
,
78 "Cannot open output file for writing IOR: ns.ior\n"),
80 ACE_OS::fprintf (output_file
, "%s", namingServiceA_
.ior ());
81 ACE_OS::fclose (output_file
);
83 // Get reference to Root POA
84 CORBA::Object_var obj
= orb_
->resolve_initial_references("RootPOA");
85 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
87 // Activate POA Manager
88 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
91 // Find the Naming Service
92 obj
= orb_
->string_to_object (namingServiceB_
.ior ());
93 CosNaming::NamingContext_var root
=
94 CosNaming::NamingContext::_narrow(obj
.in());
96 if (CORBA::is_nil(root
.in())) {
97 ACE_ERROR ((LM_ERROR
, "Error, Nil Naming Context reference\n"));
100 // Bind the example Naming Context, if necessary
101 CosNaming::NamingContext_var example_nc
;
102 CosNaming::Name name
;
104 name
[0].id
= CORBA::string_dup("example");
107 obj
= root
->resolve(name
);
108 example_nc
= CosNaming::NamingContext::_narrow(obj
.in());
110 catch (const CosNaming::NamingContext::NotFound
&)
112 example_nc
= root
->bind_new_context(name
);
115 // Bind the Test object
117 name
[1].id
= CORBA::string_dup("Hello");
120 Hello
servant(orb_
.in ());
121 PortableServer::ObjectId_var oid
= poa
->activate_object(&servant
);
122 obj
= poa
->id_to_reference(oid
.in());
123 root
->rebind(name
, obj
.in());
125 ACE_DEBUG ((LM_INFO
, "Hello object bound in Naming Service B\n"));
128 obj
= orb_
->string_to_object (namingServiceA_
.ior ());
129 root
= CosNaming::NamingContext::_narrow(obj
.in());
130 root
->bind_context (name
, example_nc
.in ());
132 ACE_DEBUG ((LM_INFO
, "'example' context of NS B bound in Naming Service A\n"));
136 namingServiceB_
.end();
138 ACE_DEBUG ((LM_INFO
, "Naming Service B shut down\n"));
141 // Create shutdown server
142 NsShutdown
shutdown_servant(orb_
.in ());
143 PortableServer::ObjectId_var shutdown_oid
= poa
->activate_object(&shutdown_servant
);
144 obj
= poa
->id_to_reference(shutdown_oid
.in());
145 CORBA::String_var ior
= orb_
->object_to_string (obj
.in ());
147 output_file
= ACE_OS::fopen (ior_shutdown_file
, "w");
148 if (output_file
== 0)
149 ACE_ERROR_RETURN ((LM_ERROR
,
150 "Cannot open output file %s for writing IOR: %C\n",
154 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
155 ACE_OS::fclose (output_file
);
157 // Normally we run the orb and the orb is shutdown by
158 // calling TestTask::end().
163 // Shutdown the Naming Services.
164 namingServiceA_
.end();
167 namingServiceB_
.end();
172 catch (const CORBA::Exception
& ex
)
174 ex
._tao_print_exception ("CORBA exception: ");
180 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
182 // Start the Test task
183 TestTask
test_ (argc
, argv
);
184 if (test_
.activate() == -1)
186 ACE_ERROR_RETURN ((LM_ERROR
, "Unable to start test task.\n"), -1);
189 // Wait tasks finish.
190 test_
.thr_mgr ()->wait();