Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_3673_Regression / server.cpp
blob57073f3c85ad5590c389dfa15ff71ef0c4962877
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()
57 try {
58 // Get reference to Root POA
59 CORBA::Object_var obj = orb_->resolve_initial_references("RootPOA");
60 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
62 // Activate POA Manager
63 PortableServer::POAManager_var mgr = poa->the_POAManager();
64 mgr->activate();
66 // Find the Naming Service
67 obj = orb_->string_to_object ("corbaloc:iiop:1.2@localhost:9932/NameService");
68 CosNaming::NamingContext_var root =
69 CosNaming::NamingContext::_narrow(obj.in());
71 if (CORBA::is_nil(root.in())) {
72 ACE_ERROR ((LM_ERROR, "Error, Nil Naming Context reference\n"));
73 return 1;
75 // Bind the example Naming Context, if necessary
76 CosNaming::NamingContext_var example_nc;
77 CosNaming::Name name;
78 name.length(1);
79 name[0].id = CORBA::string_dup("example");
80 try
82 obj = root->resolve(name);
83 example_nc =
84 CosNaming::NamingContext::_narrow(obj.in());
86 catch (const CosNaming::NamingContext::NotFound&)
88 example_nc = root->bind_new_context(name);
91 // Bind the Test object
92 name.length(2);
93 name[1].id = CORBA::string_dup("Hello");
95 // Create an object
96 Hello servant(orb_.in ());
97 PortableServer::ObjectId_var oid = poa->activate_object(&servant);
98 obj = poa->id_to_reference(oid.in());
99 root->rebind(name, obj.in());
101 ACE_DEBUG ((LM_INFO, "Hello object bound in Naming Service B\n"));
103 name.length(1);
104 obj = orb_->string_to_object ("corbaloc:iiop:1.2@localhost:9931/NameService");
105 root = CosNaming::NamingContext::_narrow(obj.in());
106 root->bind_context (name, example_nc.in ());
108 ACE_DEBUG ((LM_INFO, "'example' context of NS B bound in Naming Service A\n"));
110 CORBA::String_var ior =
111 orb_->object_to_string (obj.in ());
113 // Output the IOR to the <ior_output_file>
114 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
115 if (output_file == 0)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 "Cannot open output file %s for writing IOR: %C\n",
118 ior_output_file,
119 ior.in ()),
121 ACE_OS::fprintf (output_file, "%s", ior.in ());
122 ACE_OS::fclose (output_file);
124 ACE_DEBUG ((LM_INFO, "Wrote IOR file\n"));
126 // Normally we run the orb and the orb is shutdown by
127 // calling TestTask::end().
128 // Accept requests
129 orb_->run();
130 orb_->destroy();
132 return 0;
134 catch (const CORBA::Exception& ex)
136 ex._tao_print_exception ("CORBA exception: ");
139 return -1;
142 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
144 // Start the Test task
145 TestTask test_ (argc, argv);
146 if (test_.activate() == -1)
148 ACE_ERROR_RETURN ((LM_ERROR, "Unable to start test task.\n"), -1);
151 // Wait the tasks to finish.
152 test_.thr_mgr ()->wait();
154 return 0;