Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / NestedUpcall / MT_Client_Test / local_server.cpp
blob0d911aa049aa56ad575f7ae33c6e526c265f8697
2 //=============================================================================
3 /**
4 * @file local_server.cpp
6 * This server will run the ORB briefly and then make
7 * several calls on the distant MT Object.
9 * @author Michael Kircher
11 //=============================================================================
14 #include "local_server.h"
15 #include "tao/debug.h"
16 #include "ace/Read_Buffer.h"
17 #include "ace/OS_NS_unistd.h"
18 #include "ace/OS_NS_fcntl.h"
19 #include "tao/Utils/ORB_Manager.h"
21 MT_Server_Task::MT_Server_Task (ACE_Thread_Manager* thr_mgr_ptr,
22 int argc,
23 ACE_TCHAR **argv,
24 TAO_ORB_Manager* orb_manager_ptr)
25 :ACE_Task<ACE_SYNCH> (thr_mgr_ptr),
26 argc_ (argc),
27 argv_ (argv),
28 orb_manager_ptr_ (orb_manager_ptr)
32 int
33 MT_Server_Task::svc ()
35 if (this->mT_Server_.init (this->argc_,
36 this->argv_,
37 this->orb_manager_ptr_) == -1)
38 return 1;
39 else
40 return this->mT_Server_.run_ORB_briefly ();
44 MT_Server::MT_Server ()
45 : object_key_ (0),
46 ior_output_file_ (0),
47 orb_manager_ptr_ (0),
48 iterations_ (1)
52 // Reads the MT Object IOR from a file
53 int
54 MT_Server::read_ior (ACE_TCHAR *filename)
56 // Open the file for reading.
57 ACE_HANDLE f_handle = ACE_OS::open (filename,0);
59 if (f_handle == ACE_INVALID_HANDLE)
60 ACE_ERROR_RETURN ((LM_ERROR,
61 "Unable to open %s for reading\n",
62 filename),
63 -1);
65 ACE_Read_Buffer ior_buffer (f_handle);
67 this->object_key_ = ior_buffer.read ();
68 if (this->object_key_ == 0)
69 ACE_ERROR_RETURN ((LM_ERROR,
70 "Unable to allocate memory to read ior: %p\n"),
71 -1);
73 ACE_OS::close (f_handle);
74 return 0;
78 int
79 MT_Server::parse_args ()
81 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("d:f:g:h:i:n:s:"));
82 int c;
84 while ((c = get_opts ()) != -1)
85 switch (c)
87 case 'd': // debug flag.
88 TAO_debug_level++;
89 break;
90 case 'h': // read the IOR from the file.
91 int result;
92 result = this->read_ior (get_opts.opt_arg ());
93 // read IOR for MT Object
94 if (result < 0)
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "Unable to read ior from %s : %p\n",
97 get_opts.opt_arg ()),
98 -1);
99 break;
100 case 'f':
101 case 'g':
102 case 'i':
103 case 'n':
104 break;
105 case 's': this->iterations_ = ACE_OS::atoi (get_opts.opt_arg ());
106 break;
107 case '?':
108 default:
109 ACE_ERROR_RETURN ((LM_ERROR,
110 "usage: %s"
111 " [-d]\n"
112 " [-f] first server ior file\n"
113 " [-g] second server ior file\n"
114 " [-h] third server ior file\n"
115 " [-i] client iterations\n"
116 " [-n] number of client threads\n"
117 " [-s] number of server iterations\n"
118 "\n",
119 argv_ [0]),
120 -1);
123 // Indicates successful parsing of command line.
124 return 0;
128 MT_Server::init (int argc,
129 ACE_TCHAR** argv,
130 TAO_ORB_Manager* orb_manager_ptr)
132 this->argc_ = argc;
133 this->argv_ = argv;
134 if ((this->orb_manager_ptr_ = orb_manager_ptr) == 0)
135 ACE_ERROR_RETURN ((LM_ERROR,
136 "MT_Server::init: ORB_Manager is nil!\n"),
137 -1);
141 // Call the init of TAO_ORB_Manager to create a child POA
142 // under the root POA.
143 this->orb_manager_ptr_->init_child_poa (argc,
144 argv,
145 "child_poa");
148 this->parse_args ();
149 // ~~ check for the return value here
151 this->str_ =
152 this->orb_manager_ptr_->activate_under_child_poa ("MT",
153 &this->mT_Object_i_);
155 #if 0
156 ACE_DEBUG ((LM_DEBUG,
157 "The IOR is: <%s>\n",
158 this->str_.in ()));
159 #endif /*if 0*/
161 if (this->ior_output_file_)
163 ACE_OS::fprintf (this->ior_output_file_,
164 "%s",
165 this->str_.in ());
166 ACE_OS::fclose (this->ior_output_file_);
169 // retrieve the object reference to the distant mt object
170 if (this->object_key_ == 0)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 "The IOR is nil, not able to get the object.\n"),
173 -1);
175 CORBA::ORB_var orb_var = this->orb_manager_ptr_->orb ();
177 CORBA::Object_var object_var =
178 orb_var->string_to_object (this->object_key_);
180 if (CORBA::is_nil (object_var.in()))
181 ACE_ERROR_RETURN ((LM_ERROR,
182 "No proper object has been returned.\n"),
183 -1);
185 this->mT_Object_var_ = MT_Object::_narrow (object_var.in());
187 if (CORBA::is_nil (this->mT_Object_var_.in()))
189 ACE_ERROR_RETURN ((LM_ERROR,
190 "We have no proper reference to the Object.\n"),
191 -1);
194 if (TAO_debug_level > 0)
195 ACE_DEBUG ((LM_DEBUG, "We have a proper reference to the Object.\n"));
197 catch (const CORBA::Exception& ex)
199 ex._tao_print_exception ("MT_Client::init");
200 return -1;
203 return 0;
207 MT_Server::run ()
211 int r = this->orb_manager_ptr_->run ();
213 if (r == -1)
214 ACE_ERROR_RETURN ((LM_ERROR,
215 "MT_Server::run"),
216 -1);
218 catch (const CORBA::Exception& ex)
220 ex._tao_print_exception ("MT_Server::run");
221 return -1;
223 return 0;
226 MT_Server::~MT_Server ()
228 if (this->object_key_ != 0)
229 ACE_OS::free (this->object_key_);
233 if (this->orb_manager_ptr_)
234 this->orb_manager_ptr_->deactivate_under_child_poa (this->str_.in ());
236 catch (const CORBA::Exception& ex)
238 ex._tao_print_exception ("MT_Client::~MT_Client");
244 MT_Server::run_ORB_briefly ()
246 if (this->iterations_ > 0)
250 ACE_DEBUG ((LM_DEBUG,
251 "(%P|%t) MT_Server::run: "
252 "going to call distant MT Object\n"));
254 PortableServer::POA_var root_poa =
255 this->orb_manager_ptr_->root_poa ();
257 for (unsigned int i = 0; i < this->iterations_; i++)
259 PortableServer::ObjectId_var id =
260 root_poa->activate_object (&mT_Object_i_);
262 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
264 MT_Object_var tmp =
265 MT_Object::_narrow (object_act.in ());
267 this->mT_Object_var_->yadda (0,
268 tmp.in ());
271 catch (const CORBA::Exception& ex)
273 ex._tao_print_exception ("MT_Server::run_ORB_briefly");
274 return -1;
277 return 0;