1 #include "nestea_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"
13 // The file to save the persistent state to.
14 const ACE_TCHAR NESTEA_DATA_FILENAME
[] = ACE_TEXT("nestea.dat");
16 // The server name of the Nestea Server
17 const char SERVER_NAME
[] = "nestea_server";
19 const int SELF_DESTRUCT_SECS
= 8; // Must coordinate with run_test.pl
21 Nestea_Server_i::Nestea_Server_i (const char * /*filename*/)
34 Nestea_Server_i::~Nestea_Server_i ()
36 delete this->server_impl_
;
40 Nestea_Server_i::parse_args ()
42 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("do:"));
45 while ((c
= get_opts ()) != -1)
48 case 'd': // debug flag.
51 case 'o': // output the IOR to a file.
52 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
53 if (this->ior_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 '?': // display help for use of the server.
60 ACE_ERROR_RETURN ((LM_ERROR
,
64 " [-o] <ior_output_file>"
70 // Indicates successful parsing of command line.
75 // The init() method does quite a few things.
77 // - Initialize the ORB
78 // - Create a persistent POA for the server
79 // - Activate the POA Manager
80 // - Activate the servant under the POA
81 // - Uses the IR helper class to alter the object
82 // - Creates an IOR from the servant and outputs it to a file
84 static void printEnvVars() {
85 char* useimr
= ACE_OS::getenv("TAO_USE_IMR");
86 char* ior
= ACE_OS::getenv("ImplRepoServiceIOR");
87 ACE_OS::printf("nestea_server: TAO_USE_IMR=%s\n", useimr
!= 0 ? useimr
: "<null>");
88 ACE_OS::printf("nestea_server: ImplRepoServiceIOR=%s\n", ior
!= 0 ? ior
: "<null>");
92 Nestea_Server_i::init (int argc
, ACE_TCHAR
** argv
)
95 // Since the Implementation Repository keys off of the POA name, we need
96 // to use the SERVER_NAME as the POA's name.
97 const char *poa_name
= SERVER_NAME
;
101 // Initialize the ORB
102 this->orb_
= CORBA::ORB_init (argc
, argv
);
104 // Save pointers to the command line arguments
108 // Now check the arguments for our options
109 int retval
= this->parse_args ();
114 // Get the POA from the ORB.
115 CORBA::Object_var obj
=
116 this->orb_
->resolve_initial_references ("RootPOA");
117 ACE_ASSERT(! CORBA::is_nil (obj
.in ()));
119 this->root_poa_
= PortableServer::POA::_narrow (obj
.in ());
121 this->poa_manager_
= this->root_poa_
->the_POAManager ();
123 // We now need to create a POA with the persistent and user_id policies,
124 // since they are need for use with the Implementation Repository.
126 CORBA::PolicyList
policies (2);
130 this->root_poa_
->create_id_assignment_policy (PortableServer::USER_ID
);
133 this->root_poa_
->create_lifespan_policy (PortableServer::PERSISTENT
);
136 this->root_poa_
->create_POA (poa_name
,
137 this->poa_manager_
.in (),
140 // Creation of the new POA is over, so destroy the Policy_ptr's.
141 for (CORBA::ULong i
= 0; i
< policies
.length (); ++i
)
143 CORBA::Policy_ptr policy
= policies
[i
];
147 ACE_NEW_RETURN (this->server_impl_
,
148 Nestea_i (orb_
.in(), NESTEA_DATA_FILENAME
),
151 PortableServer::ObjectId_var server_id
=
152 PortableServer::string_to_ObjectId ("server");
154 this->nestea_poa_
->activate_object_with_id (server_id
.in (),
157 obj
= this->nestea_poa_
->id_to_reference (server_id
.in ());
158 CORBA::String_var ior
=
159 this->orb_
->object_to_string (obj
.in ());
160 if (TAO_debug_level
> 0)
161 ACE_DEBUG ((LM_DEBUG
, "The IOR is: <%s>\n", ior
.in ()));
163 TAO_Root_POA
* tmp_poa
= dynamic_cast<TAO_Root_POA
*>(nestea_poa_
.in());
164 obj
= tmp_poa
->id_to_reference_i (server_id
.in (), false);
165 CORBA::String_var rawior
=
166 this->orb_
->object_to_string (obj
.in ());
168 obj
= this->orb_
->resolve_initial_references ("IORTable");
170 IORTable::Table_var adapter
=
171 IORTable::Table::_narrow (obj
.in ());
172 ACE_ASSERT(! CORBA::is_nil (adapter
.in ()));
174 adapter
->bind (poa_name
, rawior
.in());
176 this->poa_manager_
->activate ();
178 if (this->ior_output_file_
)
180 ACE_OS::fprintf (this->ior_output_file_
, "%s", ior
.in ());
181 ACE_OS::fclose (this->ior_output_file_
);
184 catch (const CORBA::Exception
& ex
)
186 ex
._tao_print_exception ("Nestea_i::init");
195 Nestea_Server_i::run ()
201 ACE_Time_Value
tv(SELF_DESTRUCT_SECS
);
203 this->orb_
->run (tv
);
205 this->root_poa_
->destroy(1, 1);
206 this->orb_
->destroy();
208 catch (const CORBA::Exception
& ex
)
211 ex
._tao_print_exception ("Nestea_i::run");