Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_2800_Regression / nsmain.cpp
blob444ace92c128339a40aea2a43a001295a0fa713f
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()
65 try {
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");
76 if (output_file == 0)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 "Cannot open output file for writing IOR: ns.ior\n"),
79 1);
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();
89 mgr->activate();
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"));
98 return 1;
100 // Bind the example Naming Context, if necessary
101 CosNaming::NamingContext_var example_nc;
102 CosNaming::Name name;
103 name.length(1);
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
116 name.length(2);
117 name[1].id = CORBA::string_dup("Hello");
119 // Create an object
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"));
127 name.length(1);
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"));
134 if (shutdown_ns_)
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",
151 ior_shutdown_file,
152 ior.in ()),
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().
159 // Accept requests
160 orb_->run();
161 orb_->destroy();
163 // Shutdown the Naming Services.
164 namingServiceA_.end();
165 if (!shutdown_ns_)
167 namingServiceB_.end();
170 return 0;
172 catch (const CORBA::Exception& ex)
174 ex._tao_print_exception ("CORBA exception: ");
177 return -1;
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();
192 return 0;