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
65 // Start the Naming Service tasks
66 namingServiceA_
.activate();
67 // Wait for the Naming Service initialized.
68 namingServiceA_
.waitInit();
70 namingServiceB_
.activate();
71 // Wait for the Naming Service initialized.
72 namingServiceB_
.waitInit();
74 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
76 ACE_ERROR_RETURN ((LM_ERROR
,
77 "Cannot open output file for writing IOR: ns.ior\n"),
79 ACE_OS::fprintf (output_file
, "%s", namingServiceA_
.ior ());
80 ACE_OS::fclose (output_file
);
82 // Get reference to Root POA
83 CORBA::Object_var obj
= orb_
->resolve_initial_references("RootPOA");
84 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
86 // Activate POA Manager
87 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
90 // Find the Naming Service
91 obj
= orb_
->string_to_object (namingServiceB_
.ior ());
92 CosNaming::NamingContext_var root
=
93 CosNaming::NamingContext::_narrow(obj
.in());
95 if (CORBA::is_nil(root
.in())) {
96 ACE_ERROR ((LM_ERROR
, "Error, Nil Naming Context reference\n"));
99 // Bind the example Naming Context, if necessary
100 CosNaming::NamingContext_var example_nc
;
101 CosNaming::Name name
;
103 name
[0].id
= CORBA::string_dup("example");
106 obj
= root
->resolve(name
);
107 example_nc
= CosNaming::NamingContext::_narrow(obj
.in());
109 catch (const CosNaming::NamingContext::NotFound
&)
111 example_nc
= root
->bind_new_context(name
);
114 // Bind the Test object
116 name
[1].id
= CORBA::string_dup("Hello");
119 Hello
servant(orb_
.in ());
120 PortableServer::ObjectId_var oid
= poa
->activate_object(&servant
);
121 obj
= poa
->id_to_reference(oid
.in());
122 root
->rebind(name
, obj
.in());
124 ACE_DEBUG ((LM_INFO
, "Hello object bound in Naming Service B\n"));
127 obj
= orb_
->string_to_object (namingServiceA_
.ior ());
128 root
= CosNaming::NamingContext::_narrow(obj
.in());
129 root
->bind_context (name
, example_nc
.in ());
131 ACE_DEBUG ((LM_INFO
, "'example' context of NS B bound in Naming Service A\n"));
135 namingServiceB_
.end();
137 ACE_DEBUG ((LM_INFO
, "Naming Service B shut down\n"));
140 // Create shutdown server
141 NsShutdown
shutdown_servant(orb_
.in ());
142 PortableServer::ObjectId_var shutdown_oid
= poa
->activate_object(&shutdown_servant
);
143 obj
= poa
->id_to_reference(shutdown_oid
.in());
144 CORBA::String_var ior
= orb_
->object_to_string (obj
.in ());
146 output_file
= ACE_OS::fopen (ior_shutdown_file
, "w");
147 if (output_file
== 0)
148 ACE_ERROR_RETURN ((LM_ERROR
,
149 "Cannot open output file %s for writing IOR: %C\n",
153 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
154 ACE_OS::fclose (output_file
);
156 // Normally we run the orb and the orb is shutdown by
157 // calling TestTask::end().
162 // Shutdown the Naming Services.
163 namingServiceA_
.end();
166 namingServiceB_
.end();
171 catch (const CORBA::Exception
& ex
)
173 ex
._tao_print_exception ("CORBA exception: ");
179 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
181 // Start the Test task
182 TestTask
test_ (argc
, argv
);
183 if (test_
.activate() == -1)
185 ACE_ERROR_RETURN ((LM_ERROR
, "Unable to start test task.\n"), -1);
188 // Wait tasks finish.
189 test_
.thr_mgr ()->wait();