Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / NestedUpcall / MT_Client_Test / client.cpp
blob74e9b576e00a45d3b3f514cc91456f77da0ecb1b
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * Start one server thread calling a distant MT Object several times,
7 * also starting several client threads which call the MT Object too.
8 * The server does nested upcalls.
10 * @author Michael Kircher
12 //=============================================================================
15 #include "client.h"
16 #include "local_server.h"
17 #include "tao/debug.h"
18 #include "tao/Utils/ORB_Manager.h"
19 #include "ace/Read_Buffer.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_NS_fcntl.h"
22 #include "ace/Malloc_Base.h"
23 #include "ace/Truncate.h"
25 MT_Client_Task::MT_Client_Task (int argc, ACE_TCHAR **argv,
26 int client_number)
27 : argc_ (argc),
28 argv_ (argv),
29 client_number_ (client_number)
33 int
34 MT_Client_Task::svc ()
36 if (this->mT_Client_.init (this->argc_,
37 this->argv_,
38 this->client_number_) == -1)
39 return 1;
40 else
41 return this->mT_Client_.run ();
44 // Constructor.
45 MT_Client::MT_Client ()
46 : object_key_ (0),
47 iterations_ (1)
52 // Reads the Object A IOR from a file
54 int
55 MT_Client::read_ior (ACE_TCHAR *filename)
57 // Open the file for reading.
58 ACE_HANDLE f_handle = ACE_OS::open (filename,0);
60 if (f_handle == ACE_INVALID_HANDLE)
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "Unable to open %s for reading\n",
63 filename),
64 -1);
66 ACE_Read_Buffer ior_buffer (f_handle);
68 this->object_key_ = ior_buffer.read ();
69 if (this->object_key_ == 0)
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "Unable to allocate memory to read ior: %p\n"),
72 -1);
74 ACE_OS::close (f_handle);
75 return 0;
79 // Parses the command line arguments and returns an error status.
81 int
82 MT_Client::parse_args ()
84 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:g:h:i:n:s:"));
85 int c;
86 int result;
88 while ((c = get_opts ()) != -1)
89 switch (c)
91 case 'd': // debug flag
92 TAO_debug_level++;
93 break;
94 // Depending on the thread ID we pick the IOR
95 case 'f': // read the IOR from the file.
96 if ((this->client_number_ % 2) == 0)
98 result = this->read_ior (get_opts.opt_arg ());
99 // read IOR for MT Object
100 if (result < 0)
101 ACE_ERROR_RETURN ((LM_ERROR,
102 "Unable to read ior from %s\n",
103 get_opts.opt_arg ()),
104 -1);
106 break;
107 case 'g': // read the IOR from the file.
108 if ((this->client_number_ % 2) == 1)
110 result = this->read_ior (get_opts.opt_arg ());
111 // read IOR for Object A
112 if (result < 0)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "Unable to read ior from %s\n",
115 get_opts.opt_arg ()),
116 -1);
118 break;
119 case 'i': this->iterations_ = ACE_OS::atoi (get_opts.opt_arg ());
120 break;
121 case 'h':
122 case 'n':
123 case 's':
124 break;
125 case '?':
126 default:
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "usage: %s\n"
129 " [-f] first server ior file\n"
130 " [-g] second server ior file\n"
131 " [-h] third server ior file\n"
132 " [-i] client iterations\n"
133 " [-n] number of client threads\n"
134 " [-s] number of server iterations\n",
135 this->argv_ [0]),
136 -1);
139 // Indicates successful parsing of command line.
140 return 0;
144 MT_Client::run ()
148 for (unsigned long i = 0; i < this->iterations_; i++)
150 #if 0
151 ACE_DEBUG ((LM_DEBUG,
152 "(%P|%t) MT_Client::run: %d of %d\n",
154 this->iterations_));
155 #endif /*if 0*/
157 // call the recursive object MT_Object for nested upcalls
158 // testing
159 this->mT_Object_var_->yadda (0,
163 catch (const CORBA::Exception& ex)
165 ex._tao_print_exception ("MT_Client:run");
166 return -1;
169 return 0;
172 MT_Client::~MT_Client ()
174 if (this->object_key_ != 0)
175 ACE_Allocator::instance ()->free (this->object_key_);
176 if (this->argv_ != 0)
177 delete [] this->argv_;
182 MT_Client::init (int argc, ACE_TCHAR **argv,
183 int client_number)
185 // Make a copy of argv since ORB_init will change it.
186 this->argc_ = argc;
187 this->argv_ = new ACE_TCHAR *[argc];
188 for (int i = 0; i < argc; i++)
189 this->argv_[i] = argv[i];
191 this->client_number_ = client_number;
195 char buf[64];
196 ACE_OS::sprintf (buf, "thread_%lx", ACE_Utils::truncate_cast<long> ((intptr_t)this));
198 this->orb_var_ =
199 CORBA::ORB_init (this->argc_,
200 this->argv_,
201 buf);
203 // Parse command line and verify parameters.
204 if (this->parse_args () == -1)
205 return -1;
207 if (this->object_key_ == 0)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 "The IOR is nil, not able to get the object.\n"),
210 -1);
212 CORBA::Object_var object_var =
213 this->orb_var_->string_to_object (this->object_key_);
215 if (CORBA::is_nil (object_var.in()))
216 ACE_ERROR_RETURN ((LM_ERROR,
217 "No proper object has been returned.\n"),
218 -1);
220 this->mT_Object_var_ = MT_Object::_narrow (object_var.in());
222 if (CORBA::is_nil (this->mT_Object_var_.in()))
224 ACE_ERROR_RETURN ((LM_ERROR,
225 "We have no proper reference to the Object.\n"),
226 -1);
229 if (TAO_debug_level > 0)
230 ACE_DEBUG ((LM_DEBUG, "We have a proper reference to the Object.\n"));
232 CORBA::Object_var poa_object =
233 this->orb_var_->resolve_initial_references("RootPOA");
235 if (CORBA::is_nil (poa_object.in ()))
236 ACE_ERROR_RETURN ((LM_ERROR,
237 " (%P|%t) Unable to initialize the POA.\n"),
240 PortableServer::POA_var root_poa =
241 PortableServer::POA::_narrow (poa_object.in ());
243 PortableServer::POAManager_var poa_manager =
244 root_poa->the_POAManager ();
246 poa_manager->activate ();
248 catch (const CORBA::Exception& ex)
250 ex._tao_print_exception ("MT_Client::init");
251 return -1;
254 return 0;
258 // This function runs the test.
261 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
263 int result = 0;
267 TAO_ORB_Manager orb_manager;
269 int r = orb_manager.init (argc,
270 argv);
272 if (r != 0)
273 ACE_ERROR_RETURN ((LM_ERROR,
274 "ERROR: ORB_Manager initialization failed.\n"),
275 -1);
277 ACE_DEBUG ((LM_DEBUG,"\n\tMT_Client: client\n\n"));
279 int i;
280 int threads = 1;
282 for (i = 0; i < argc; i++)
283 if (ACE_OS::strcmp (argv[i], ACE_TEXT("-n")) == 0)
284 threads = ACE_OS::atoi(argv[i + 1]);
286 // create a separate server thread
287 ACE_Thread_Manager server_thr_mgr;
288 // starting the server thread
289 MT_Server_Task *server = new MT_Server_Task (&server_thr_mgr,
290 argc,
291 argv,
292 &orb_manager);
293 if (server->activate () != 0)
295 delete server;
296 ACE_ERROR_RETURN ((LM_ERROR,
297 "CLIENT ERROR: Unable to activate "
298 "MT_Server_Task.\n"),
299 -1);
302 // starting the client threads
303 MT_Client_Task **clients = new MT_Client_Task*[threads];
305 for (i = 0; i < threads; i++)
306 clients[i] = new MT_Client_Task (argc, argv, i);
308 for (i = 0; i < threads; i++)
309 if (clients[i]->activate () != 0)
310 ACE_ERROR_RETURN ((LM_ERROR,
311 "CLIENT ERROR: Unable to activate "
312 "MT_Client_Task.\n"),
313 -1); // @@ Memory leak!
315 result = ACE_Thread_Manager::instance ()->wait ();
317 for (i = 0; i < threads; i++)
318 delete clients[i];
320 delete [] clients;
322 // wait for the server thread to end
323 result |= server_thr_mgr.wait ();
325 delete server;
327 orb_manager.fini ();
329 catch (const CORBA::Exception& ex)
331 ex._tao_print_exception ("main");
334 return result;