Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_3673_Regression / nsmain.cpp
blob6a3ba02a1d6a6e2bb4467214d093a056c3cf00d7
1 #include "NamingTask.h"
2 #include "Hello.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
12 public:
13 TestTask (int argc, ACE_TCHAR **argv);
14 virtual int svc ();
16 int parse_args (int argc, ACE_TCHAR **argv);
18 void end();
19 private:
20 NamingTask namingServiceA_;
21 NamingTask namingServiceB_;
22 CORBA::ORB_var orb_;
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");
31 shutdown_ns_ = false;
32 parse_args (argc, argv);
35 void TestTask::end()
37 orb_->shutdown(0);
38 this->wait();
41 int
42 TestTask::parse_args (int argc, ACE_TCHAR **argv)
44 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:"));
45 int c;
47 while ((c = get_opts ()) != -1)
48 switch (c)
50 case 'o':
51 ior_output_file = get_opts.opt_arg ();
52 break;
53 case 's':
54 ior_shutdown_file = get_opts.opt_arg ();
55 shutdown_ns_ = true;
56 break;
58 // Indicates successful parsing of the command line
59 return 0;
62 int TestTask::svc()
64 try {
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");
75 if (output_file == 0)
76 ACE_ERROR_RETURN ((LM_ERROR,
77 "Cannot open output file for writing IOR: ns.ior\n"),
78 1);
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();
88 mgr->activate();
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"));
97 return 1;
99 // Bind the example Naming Context, if necessary
100 CosNaming::NamingContext_var example_nc;
101 CosNaming::Name name;
102 name.length(1);
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
115 name.length(2);
116 name[1].id = CORBA::string_dup("Hello");
118 // Create an object
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"));
126 name.length(1);
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"));
133 if (shutdown_ns_)
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",
150 ior_shutdown_file,
151 ior.in ()),
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().
158 // Accept requests
159 orb_->run();
160 orb_->destroy();
162 // Shutdown the Naming Services.
163 namingServiceA_.end();
164 if (!shutdown_ns_)
166 namingServiceB_.end();
169 return 0;
171 catch (const CORBA::Exception& ex)
173 ex._tao_print_exception ("CORBA exception: ");
176 return -1;
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();
191 return 0;