Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / nestea_server_i.cpp
blobe53975115f00d67fb36f383564c1a7c13ae302f4
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"
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*/)
22 : argc_ (0),
23 argv_ (0),
24 orb_ (),
25 root_poa_ (),
26 nestea_poa_ (),
27 poa_manager_ (),
28 server_impl_ (0),
29 ior_output_file_ (0)
31 // Nothing
34 Nestea_Server_i::~Nestea_Server_i ()
36 delete this->server_impl_;
39 int
40 Nestea_Server_i::parse_args ()
42 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:"));
43 int c;
45 while ((c = get_opts ()) != -1)
46 switch (c)
48 case 'd': // debug flag.
49 TAO_debug_level++;
50 break;
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);
57 break;
58 case '?': // display help for use of the server.
59 default:
60 ACE_ERROR_RETURN ((LM_ERROR,
61 "usage: %s"
62 " [-d]"
63 " [-r]"
64 " [-o] <ior_output_file>"
65 "\n",
66 argv_ [0]),
67 1);
70 // Indicates successful parsing of command line.
71 return 0;
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>");
91 int
92 Nestea_Server_i::init (int argc, ACE_TCHAR** argv)
94 printEnvVars();
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;
99 try
101 // Initialize the ORB
102 this->orb_ = CORBA::ORB_init (argc, argv);
104 // Save pointers to the command line arguments
105 this->argc_ = argc;
106 this->argv_ = argv;
108 // Now check the arguments for our options
109 int retval = this->parse_args ();
111 if (retval != 0)
112 return retval;
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);
127 policies.length (2);
129 policies[0] =
130 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
132 policies[1] =
133 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
135 this->nestea_poa_ =
136 this->root_poa_->create_POA (poa_name,
137 this->poa_manager_.in (),
138 policies);
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];
144 policy->destroy ();
147 ACE_NEW_RETURN (this->server_impl_,
148 Nestea_i (orb_.in(), NESTEA_DATA_FILENAME),
149 -1);
151 PortableServer::ObjectId_var server_id =
152 PortableServer::string_to_ObjectId ("server");
154 this->nestea_poa_->activate_object_with_id (server_id.in (),
155 this->server_impl_);
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");
187 throw;
191 return 0;
195 Nestea_Server_i::run ()
197 int status = 0;
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)
210 status = -1;
211 ex._tao_print_exception ("Nestea_i::run");
212 throw;
215 return status;