Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / nestea_server_i.cpp
blob5e81ec83a88ed77f50f6eae82242e4190655f702
1 #include "nestea_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"
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*/)
23 : argc_ (0),
24 argv_ (0),
25 orb_ (),
26 root_poa_ (),
27 nestea_poa_ (),
28 poa_manager_ (),
29 server_impl_ (0),
30 ior_output_file_ (0)
32 // Nothing
35 Nestea_Server_i::~Nestea_Server_i (void)
37 delete this->server_impl_;
40 int
41 Nestea_Server_i::parse_args (void)
43 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:"));
44 int c;
46 while ((c = get_opts ()) != -1)
47 switch (c)
49 case 'd': // debug flag.
50 TAO_debug_level++;
51 break;
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);
58 break;
59 case '?': // display help for use of the server.
60 default:
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "usage: %s"
63 " [-d]"
64 " [-r]"
65 " [-o] <ior_output_file>"
66 "\n",
67 argv_ [0]),
68 1);
71 // Indicates successful parsing of command line.
72 return 0;
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>");
92 int
93 Nestea_Server_i::init (int argc, ACE_TCHAR** argv)
95 printEnvVars();
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
106 this->argc_ = argc;
107 this->argv_ = argv;
109 // Now check the arguments for our options
110 int retval = this->parse_args ();
112 if (retval != 0)
113 return retval;
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);
128 policies.length (2);
130 policies[0] =
131 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
133 policies[1] =
134 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
136 this->nestea_poa_ =
137 this->root_poa_->create_POA (poa_name,
138 this->poa_manager_.in (),
139 policies);
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];
145 policy->destroy ();
148 ACE_NEW_RETURN (this->server_impl_,
149 Nestea_i (orb_.in(), NESTEA_DATA_FILENAME),
150 -1);
152 PortableServer::ObjectId_var server_id =
153 PortableServer::string_to_ObjectId ("server");
155 this->nestea_poa_->activate_object_with_id (server_id.in (),
156 this->server_impl_);
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");
188 throw;
192 return 0;
196 Nestea_Server_i::run (void)
198 int status = 0;
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)
211 status = -1;
212 ex._tao_print_exception ("Nestea_i::run");
213 throw;
216 return status;