Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_Naming / Federation / server.cpp
blobeb1707d67557b5e3f4a7f64a46d5f58458fb0f0f
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");
62 PortableServer::POA_var poa = PortableServer::POA::_narrow (obj.in ());
64 // Activate POA Manager
65 PortableServer::POAManager_var mgr = poa->the_POAManager ();
66 mgr->activate ();
68 // Find the Naming Service
69 obj = orb_->string_to_object (
70 "corbaloc:iiop:1.2@localhost:9932/NameService");
72 CosNaming::NamingContext_var rootB =
73 CosNaming::NamingContext::_narrow (obj.in ());
75 if (CORBA::is_nil (rootB.in ())) {
76 ACE_ERROR ((LM_ERROR,
77 ACE_TEXT ("Error, Nil Naming Context reference\n")));
78 return 1;
80 // Bind the example Naming Context, if necessary
81 CosNaming::NamingContext_var example_nc;
82 CosNaming::Name name;
83 name.length(1);
84 name[0].id = CORBA::string_dup( "example");
85 try
87 obj = rootB->resolve (name);
88 example_nc =
89 CosNaming::NamingContext::_narrow (obj.in ());
91 catch (const CosNaming::NamingContext::NotFound&)
93 example_nc = rootB->bind_new_context (name);
96 // Bind the Test object
97 name.length (2);
98 name[1].id = CORBA::string_dup ("Hello");
100 // Create an object
101 Hello servant (orb_.in ());
102 PortableServer::ObjectId_var oid = poa->activate_object (&servant);
103 obj = poa->id_to_reference (oid.in ());
104 rootB->rebind (name, obj.in ());
106 ACE_DEBUG ((LM_INFO,
107 ACE_TEXT ("Hello object bound in Naming Service B\n")));
109 name.length (1);
110 name[0].id = CORBA::string_dup ("nsB");
112 obj = orb_->string_to_object (
113 "corbaloc:iiop:1.2@localhost:9931/NameService");
115 CosNaming::NamingContext_var rootA =
116 CosNaming::NamingContext::_narrow (obj.in ());
118 rootA->bind_context (name, rootB.in ());
120 ACE_DEBUG ((LM_INFO,
121 ACE_TEXT ("Root context of NS B bound in Naming Service A ")
122 ACE_TEXT ("under name 'nsB'\n")));
124 CORBA::String_var ior =
125 orb_->object_to_string (obj.in ());
127 // Output the IOR to the <ior_output_file>
128 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
129 if (output_file == 0)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 ACE_TEXT ("Cannot open output file %s for writing ")
132 ACE_TEXT ("IOR: %C\n"),
133 ior_output_file,
134 ior.in ()),
136 ACE_OS::fprintf (output_file, ACE_TEXT ("%s"), ior.in ());
137 ACE_OS::fclose (output_file);
139 ACE_DEBUG ((LM_INFO,
140 ACE_TEXT ("Wrote IOR file\n")));
142 // Normally we run the orb and the orb is shutdown by
143 // calling TestTask::end().
144 // Accept requests
145 orb_->run();
146 orb_->destroy();
148 return 0;
150 catch (const CORBA::Exception& ex)
152 ex._tao_print_exception (ACE_TEXT ("CORBA exception: "));
155 return -1;
158 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
160 // Start the Test task
161 TestTask test_ (argc, argv);
162 if (test_.activate() == -1)
164 ACE_ERROR_RETURN ((LM_ERROR,
165 ACE_TEXT ("Unable to start test task.\n")),
166 -1);
169 // Wait the tasks to finish.
170 test_.thr_mgr ()->wait();
172 return 0;