Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / airplane_server_i.cpp
blob7b76fea438ce8acec62203917fa33ba951a1e3f5
1 #include "airplane_server_i.h"
3 #include "tao/IORTable/IORTable.h"
4 #include "tao/ImR_Client/ImR_Client.h"
5 #include "tao/debug.h"
6 #include "tao/PortableServer/Root_POA.h"
8 #include "ace/Get_Opt.h"
9 #include "ace/Read_Buffer.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_sys_time.h"
12 #include "ace/OS_NS_unistd.h"
14 // The server name of the Aiprlane Server
15 const char SERVER_NAME[] = "airplane_server";
17 Airplane_Server_i::Airplane_Server_i ()
18 : argc_ (0),
19 argv_ (0),
20 orb_ (),
21 root_poa_ (),
22 airplane_poa_ (),
23 poa_manager_ (),
24 server_impl_ (0),
25 ior_output_file_ (0),
26 pid_output_file_ (0),
27 server_name_(SERVER_NAME)
29 // Nothing
32 int
33 Airplane_Server_i::parse_args ()
35 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:s:p:"));
36 int c;
38 while ((c = get_opts ()) != -1)
39 switch (c)
41 case 'd': // debug flag.
42 TAO_debug_level++;
43 break;
44 case 'o': // output the IOR to a file.
45 this->ior_output_file_ = ACE_OS::fopen (get_opts.opt_arg (), "w");
46 if (this->ior_output_file_ == 0)
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "Unable to open %s for writing: %p\n",
49 get_opts.opt_arg ()), -1);
50 break;
51 case 'p': // output the pid to a file.
52 this->pid_output_file_ = ACE_OS::fopen (get_opts.opt_arg (), "w");
53 if (this->pid_output_file_ == 0)
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "Unable to open %s for writing: %p\n",
56 get_opts.opt_arg ()), -1);
57 break;
58 case 's': // extension to the server name.
59 this->server_name_ = ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ());
60 break;
61 case '?': // display help for use of the server.
62 default:
63 ACE_ERROR_RETURN ((LM_ERROR,
64 "usage: %s"
65 " [-d]"
66 " [-o <ior_output_file>]"
67 " [-p <pid_output_file>]"
68 " [-s <the server name>]"
69 "\n",
70 argv_ [0]),
71 1);
74 // Indicates successful parsing of command line.
75 return 0;
78 int
79 Airplane_Server_i::init (int argc, ACE_TCHAR** argv)
81 try
83 // Initialize the ORB
84 this->orb_ = CORBA::ORB_init (argc, argv);
86 // Save pointers to the command line arguments
87 this->argc_ = argc;
88 this->argv_ = argv;
90 // Now check the arguments for our options
91 int retval = this->parse_args ();
93 if (retval != 0)
94 return retval;
96 // Get the POA from the ORB.
97 CORBA::Object_var obj =
98 this->orb_->resolve_initial_references ("RootPOA");
99 ACE_ASSERT(! CORBA::is_nil (obj.in ()));
101 // Narrow the object to a POA.
102 root_poa_ = PortableServer::POA::_narrow (obj.in ());
104 // Get the POA_Manager.
105 this->poa_manager_ = this->root_poa_->the_POAManager ();
107 // We now need to create a POA with the persistent and user_id policies,
108 // since they are need for use with the Implementation Repository.
110 CORBA::PolicyList policies (2);
111 policies.length (2);
113 policies[0] =
114 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
116 policies[1] =
117 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
119 // Since the Implementation Repository keys off of the POA name, we need
120 // to use the server_name_ as the POA's name.
121 this->airplane_poa_ =
122 this->root_poa_->create_POA (this->server_name_.c_str(),
123 this->poa_manager_.in (),
124 policies);
126 // Creation of the new POA is over, so destroy the Policy_ptr's.
127 for (CORBA::ULong i = 0; i < policies.length (); ++i)
129 CORBA::Policy_ptr policy = policies[i];
130 policy->destroy ();
133 ACE_NEW_RETURN (this->server_impl_, Airplane_i, -1);
135 PortableServer::ObjectId_var server_id =
136 PortableServer::string_to_ObjectId ("server");
138 this->airplane_poa_->activate_object_with_id (server_id.in (),
139 this->server_impl_);
141 obj = this->airplane_poa_->id_to_reference (server_id.in ());
142 CORBA::String_var ior =
143 this->orb_->object_to_string (obj.in ());
144 if (TAO_debug_level > 0)
145 ACE_DEBUG ((LM_DEBUG, "The ImRified IOR is: <%C>\n", ior.in ()));
147 TAO_Root_POA* tmp_poa = dynamic_cast<TAO_Root_POA*>(airplane_poa_.in());
148 obj = tmp_poa->id_to_reference_i (server_id.in (), false);
149 CORBA::String_var plain_ior =
150 this->orb_->object_to_string (obj.in ());
151 if (TAO_debug_level > 0)
152 ACE_DEBUG ((LM_DEBUG, "The plain IOR is: <%C>\n", plain_ior.in ()));
154 // Note : The IORTable will only be used for those clients who try to
155 // invoke indirectly using a simple object_key reference
156 // like "corbaloc::localhost:8888/airplane_server".
157 obj = this->orb_->resolve_initial_references ("IORTable");
159 IORTable::Table_var adapter =
160 IORTable::Table::_narrow (obj.in ());
161 ACE_ASSERT(! CORBA::is_nil (adapter.in ()));
162 adapter->bind (this->server_name_.c_str(), plain_ior.in ());
164 this->poa_manager_->activate ();
166 if (this->ior_output_file_)
168 ACE_OS::fprintf (this->ior_output_file_, "%s", ior.in ());
169 ACE_OS::fclose (this->ior_output_file_);
172 if (this->pid_output_file_)
174 int pid = static_cast<int> (ACE_OS::getpid ());
175 ACE_OS::fprintf (this->pid_output_file_, "%d\n", pid);
176 ACE_OS::fclose (this->pid_output_file_);
179 catch (const CORBA::Exception& ex)
181 ex._tao_print_exception ("Airplane_Server_i::init");
182 throw;
185 return 0;
189 Airplane_Server_i::run ()
193 ACE_Time_Value tv(60);
194 ACE_Time_Value tvStart = ACE_OS::gettimeofday();
196 this->orb_->run (tv);
198 ACE_Time_Value tvEnd = ACE_OS::gettimeofday();
200 this->root_poa_->destroy(1, 1);
201 this->orb_->destroy();
203 if (tvEnd - tvStart > tv - ACE_Time_Value(5))
204 return 1;
206 catch (const CORBA::Exception& ex)
208 ex._tao_print_exception ("Airplane_Server_i::run");
209 throw;
213 return 0;
216 Airplane_Server_i::~Airplane_Server_i ()
218 delete this->server_impl_;