1 #include "airplane_server_i.h"
3 #include "tao/IORTable/IORTable.h"
4 #include "tao/ImR_Client/ImR_Client.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 ()
27 server_name_(SERVER_NAME
)
33 Airplane_Server_i::parse_args ()
35 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("do:s:p:"));
38 while ((c
= get_opts ()) != -1)
41 case 'd': // debug flag.
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);
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);
58 case 's': // extension to the server name.
59 this->server_name_
= ACE_TEXT_ALWAYS_CHAR (get_opts
.opt_arg ());
61 case '?': // display help for use of the server.
63 ACE_ERROR_RETURN ((LM_ERROR
,
66 " [-o <ior_output_file>]"
67 " [-p <pid_output_file>]"
68 " [-s <the server name>]"
74 // Indicates successful parsing of command line.
79 Airplane_Server_i::init (int argc
, ACE_TCHAR
** argv
)
84 this->orb_
= CORBA::ORB_init (argc
, argv
);
86 // Save pointers to the command line arguments
90 // Now check the arguments for our options
91 int retval
= this->parse_args ();
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);
114 this->root_poa_
->create_id_assignment_policy (PortableServer::USER_ID
);
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 (),
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
];
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 (),
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");
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))
206 catch (const CORBA::Exception
& ex
)
208 ex
._tao_print_exception ("Airplane_Server_i::run");
216 Airplane_Server_i::~Airplane_Server_i ()
218 delete this->server_impl_
;