Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_Naming / Federation / server.cpp
blob145b8648b4625765e89eaede0358e4c1ddf969aa
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");
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 (
69 "corbaloc:iiop:1.2@localhost:9932/NameService");
71 CosNaming::NamingContext_var rootB =
72 CosNaming::NamingContext::_narrow (obj.in ());
74 if (CORBA::is_nil (rootB.in ())) {
75 ACE_ERROR ((LM_ERROR,
76 ACE_TEXT ("Error, Nil Naming Context reference\n")));
77 return 1;
79 // Bind the example Naming Context, if necessary
80 CosNaming::NamingContext_var example_nc;
81 CosNaming::Name name;
82 name.length(1);
83 name[0].id = CORBA::string_dup( "example");
84 try
86 obj = rootB->resolve (name);
87 example_nc =
88 CosNaming::NamingContext::_narrow (obj.in ());
90 catch (const CosNaming::NamingContext::NotFound&)
92 example_nc = rootB->bind_new_context (name);
95 // Bind the Test object
96 name.length (2);
97 name[1].id = CORBA::string_dup ("Hello");
99 // Create an object
100 Hello servant (orb_.in ());
101 PortableServer::ObjectId_var oid = poa->activate_object (&servant);
102 obj = poa->id_to_reference (oid.in ());
103 rootB->rebind (name, obj.in ());
105 ACE_DEBUG ((LM_INFO,
106 ACE_TEXT ("Hello object bound in Naming Service B\n")));
108 name.length (1);
109 name[0].id = CORBA::string_dup ("nsB");
111 obj = orb_->string_to_object (
112 "corbaloc:iiop:1.2@localhost:9931/NameService");
114 CosNaming::NamingContext_var rootA =
115 CosNaming::NamingContext::_narrow (obj.in ());
117 rootA->bind_context (name, rootB.in ());
119 ACE_DEBUG ((LM_INFO,
120 ACE_TEXT ("Root context of NS B bound in Naming Service A ")
121 ACE_TEXT ("under name 'nsB'\n")));
123 CORBA::String_var ior =
124 orb_->object_to_string (obj.in ());
126 // Output the IOR to the <ior_output_file>
127 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
128 if (output_file == 0)
129 ACE_ERROR_RETURN ((LM_ERROR,
130 ACE_TEXT ("Cannot open output file %s for writing ")
131 ACE_TEXT ("IOR: %C\n"),
132 ior_output_file,
133 ior.in ()),
135 ACE_OS::fprintf (output_file, ACE_TEXT ("%s"), ior.in ());
136 ACE_OS::fclose (output_file);
138 ACE_DEBUG ((LM_INFO,
139 ACE_TEXT ("Wrote IOR file\n")));
141 // Normally we run the orb and the orb is shutdown by
142 // calling TestTask::end().
143 // Accept requests
144 orb_->run();
145 orb_->destroy();
147 return 0;
149 catch (const CORBA::Exception& ex)
151 ex._tao_print_exception (ACE_TEXT ("CORBA exception: "));
154 return -1;
157 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
159 // Start the Test task
160 TestTask test_ (argc, argv);
161 if (test_.activate() == -1)
163 ACE_ERROR_RETURN ((LM_ERROR,
164 ACE_TEXT ("Unable to start test task.\n")),
165 -1);
168 // Wait the tasks to finish.
169 test_.thr_mgr ()->wait();
171 return 0;