Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_2800_Regression / server.cpp
bloba0dc4495e41293a0b865239f1be96428c244719c
1 #include "Hello.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Task.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
8 class TestTask : public ACE_Task_Base
10 public:
11 TestTask (int argc, ACE_TCHAR **argv);
12 virtual int svc ();
14 int parse_args (int argc, ACE_TCHAR **argv);
16 void end();
17 private:
18 CORBA::ORB_var orb_;
19 CORBA::Boolean shutdown_ns_;
22 TestTask::TestTask(int argc, ACE_TCHAR **argv)
24 orb_ = CORBA::ORB_init (argc, argv, "ServerORB");
25 shutdown_ns_ = false;
26 parse_args (argc, argv);
29 void TestTask::end()
31 orb_->shutdown(0);
32 this->wait();
35 int
36 TestTask::parse_args (int argc, ACE_TCHAR **argv)
38 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s"));
39 int c;
41 while ((c = get_opts ()) != -1)
42 switch (c)
44 case 's':
45 shutdown_ns_ = true;
46 break;
47 case 'o':
48 ior_output_file = get_opts.opt_arg ();
49 break;
51 // Indicates successful parsing of the command line
52 return 0;
55 int TestTask::svc()
58 try {
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();
65 mgr->activate();
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"));
74 return 1;
76 // Bind the example Naming Context, if necessary
77 CosNaming::NamingContext_var example_nc;
78 CosNaming::Name name;
79 name.length(1);
80 name[0].id = CORBA::string_dup("example");
81 try
83 obj = root->resolve(name);
84 example_nc =
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
93 name.length(2);
94 name[1].id = CORBA::string_dup("Hello");
96 // Create an object
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"));
104 name.length(1);
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",
119 ior_output_file,
120 ior.in ()),
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().
129 // Accept requests
130 orb_->run();
131 orb_->destroy();
133 return 0;
135 catch (const CORBA::Exception& ex)
137 ex._tao_print_exception ("CORBA exception: ");
140 return -1;
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();
155 return 0;