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"
14 // The file to save the persistent state to.
15 const ACE_TCHAR NESTEA_DATA_FILENAME
[] = ACE_TEXT("nestea.dat");
17 // The server name of the Nestea Server
18 const char SERVER_NAME
[] = "nestea_server";
20 const int SELF_DESTRUCT_SECS
= 8; // Must coordinate with run_test.pl
22 Nestea_Server_i::Nestea_Server_i (const char * /*filename*/)
35 Nestea_Server_i::~Nestea_Server_i (void)
37 delete this->server_impl_
;
41 Nestea_Server_i::parse_args (void)
43 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("do:"));
46 while ((c
= get_opts ()) != -1)
49 case 'd': // debug flag.
52 case 'o': // output the IOR to a file.
53 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
54 if (this->ior_output_file_
== 0)
55 ACE_ERROR_RETURN ((LM_ERROR
,
56 "Unable to open %s for writing: %p\n",
57 get_opts
.opt_arg ()), -1);
59 case '?': // display help for use of the server.
61 ACE_ERROR_RETURN ((LM_ERROR
,
65 " [-o] <ior_output_file>"
71 // Indicates successful parsing of command line.
76 // The init() method does quite a few things.
78 // - Initialize the ORB
79 // - Create a persistent POA for the server
80 // - Activate the POA Manager
81 // - Activate the servant under the POA
82 // - Uses the IR helper class to alter the object
83 // - Creates an IOR from the servant and outputs it to a file
85 static void printEnvVars() {
86 char* useimr
= ACE_OS::getenv("TAO_USE_IMR");
87 char* ior
= ACE_OS::getenv("ImplRepoServiceIOR");
88 ACE_OS::printf("nestea_server: TAO_USE_IMR=%s\n", useimr
!= 0 ? useimr
: "<null>");
89 ACE_OS::printf("nestea_server: ImplRepoServiceIOR=%s\n", ior
!= 0 ? ior
: "<null>");
93 Nestea_Server_i::init (int argc
, ACE_TCHAR
** argv
)
96 // Since the Implementation Repository keys off of the POA name, we need
97 // to use the SERVER_NAME as the POA's name.
98 const char *poa_name
= SERVER_NAME
;
102 // Initialize the ORB
103 this->orb_
= CORBA::ORB_init (argc
, argv
);
105 // Save pointers to the command line arguments
109 // Now check the arguments for our options
110 int retval
= this->parse_args ();
115 // Get the POA from the ORB.
116 CORBA::Object_var obj
=
117 this->orb_
->resolve_initial_references ("RootPOA");
118 ACE_ASSERT(! CORBA::is_nil (obj
.in ()));
120 this->root_poa_
= PortableServer::POA::_narrow (obj
.in ());
122 this->poa_manager_
= this->root_poa_
->the_POAManager ();
124 // We now need to create a POA with the persistent and user_id policies,
125 // since they are need for use with the Implementation Repository.
127 CORBA::PolicyList
policies (2);
131 this->root_poa_
->create_id_assignment_policy (PortableServer::USER_ID
);
134 this->root_poa_
->create_lifespan_policy (PortableServer::PERSISTENT
);
137 this->root_poa_
->create_POA (poa_name
,
138 this->poa_manager_
.in (),
141 // Creation of the new POA is over, so destroy the Policy_ptr's.
142 for (CORBA::ULong i
= 0; i
< policies
.length (); ++i
)
144 CORBA::Policy_ptr policy
= policies
[i
];
148 ACE_NEW_RETURN (this->server_impl_
,
149 Nestea_i (orb_
.in(), NESTEA_DATA_FILENAME
),
152 PortableServer::ObjectId_var server_id
=
153 PortableServer::string_to_ObjectId ("server");
155 this->nestea_poa_
->activate_object_with_id (server_id
.in (),
158 obj
= this->nestea_poa_
->id_to_reference (server_id
.in ());
159 CORBA::String_var ior
=
160 this->orb_
->object_to_string (obj
.in ());
161 if (TAO_debug_level
> 0)
162 ACE_DEBUG ((LM_DEBUG
, "The IOR is: <%s>\n", ior
.in ()));
164 TAO_Root_POA
* tmp_poa
= dynamic_cast<TAO_Root_POA
*>(nestea_poa_
.in());
165 obj
= tmp_poa
->id_to_reference_i (server_id
.in (), false);
166 CORBA::String_var rawior
=
167 this->orb_
->object_to_string (obj
.in ());
169 obj
= this->orb_
->resolve_initial_references ("IORTable");
171 IORTable::Table_var adapter
=
172 IORTable::Table::_narrow (obj
.in ());
173 ACE_ASSERT(! CORBA::is_nil (adapter
.in ()));
175 adapter
->bind (poa_name
, rawior
.in());
177 this->poa_manager_
->activate ();
179 if (this->ior_output_file_
)
181 ACE_OS::fprintf (this->ior_output_file_
, "%s", ior
.in ());
182 ACE_OS::fclose (this->ior_output_file_
);
185 catch (const CORBA::Exception
& ex
)
187 ex
._tao_print_exception ("Nestea_i::init");
196 Nestea_Server_i::run (void)
202 ACE_Time_Value
tv(SELF_DESTRUCT_SECS
);
204 this->orb_
->run (tv
);
206 this->root_poa_
->destroy(1, 1);
207 this->orb_
->destroy();
209 catch (const CORBA::Exception
& ex
)
212 ex
._tao_print_exception ("Nestea_i::run");