Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / NestedUpcall / MT_Client_Test / client.cpp
blob03e0d81e9ea0c9009561feb817c1d9eb67f03571
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 (void)
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 (void)
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 (void)
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 (void)
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)
186 // Make a copy of argv since ORB_init will change it.
187 this->argc_ = argc;
188 this->argv_ = new ACE_TCHAR *[argc];
189 for (int i = 0; i < argc; i++)
190 this->argv_[i] = argv[i];
192 this->client_number_ = client_number;
196 char buf[64];
197 ACE_OS::sprintf (buf, "thread_%lx", ACE_Utils::truncate_cast<long> ((intptr_t)this));
199 this->orb_var_ =
200 CORBA::ORB_init (this->argc_,
201 this->argv_,
202 buf);
204 // Parse command line and verify parameters.
205 if (this->parse_args () == -1)
206 return -1;
208 if (this->object_key_ == 0)
209 ACE_ERROR_RETURN ((LM_ERROR,
210 "The IOR is nil, not able to get the object.\n"),
211 -1);
213 CORBA::Object_var object_var =
214 this->orb_var_->string_to_object (this->object_key_);
216 if (CORBA::is_nil (object_var.in()))
217 ACE_ERROR_RETURN ((LM_ERROR,
218 "No proper object has been returned.\n"),
219 -1);
221 this->mT_Object_var_ = MT_Object::_narrow (object_var.in());
223 if (CORBA::is_nil (this->mT_Object_var_.in()))
225 ACE_ERROR_RETURN ((LM_ERROR,
226 "We have no proper reference to the Object.\n"),
227 -1);
230 if (TAO_debug_level > 0)
231 ACE_DEBUG ((LM_DEBUG, "We have a proper reference to the Object.\n"));
233 CORBA::Object_var poa_object =
234 this->orb_var_->resolve_initial_references("RootPOA");
236 if (CORBA::is_nil (poa_object.in ()))
237 ACE_ERROR_RETURN ((LM_ERROR,
238 " (%P|%t) Unable to initialize the POA.\n"),
241 PortableServer::POA_var root_poa =
242 PortableServer::POA::_narrow (poa_object.in ());
244 PortableServer::POAManager_var poa_manager =
245 root_poa->the_POAManager ();
247 poa_manager->activate ();
249 catch (const CORBA::Exception& ex)
251 ex._tao_print_exception ("MT_Client::init");
252 return -1;
255 return 0;
259 // This function runs the test.
262 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
264 int result = 0;
268 TAO_ORB_Manager orb_manager;
270 int r = orb_manager.init (argc,
271 argv);
273 if (r != 0)
274 ACE_ERROR_RETURN ((LM_ERROR,
275 "ERROR: ORB_Manager initialization failed.\n"),
276 -1);
278 ACE_DEBUG ((LM_DEBUG,"\n\tMT_Client: client\n\n"));
280 int i;
281 int threads = 1;
283 for (i = 0; i < argc; i++)
284 if (ACE_OS::strcmp (argv[i], ACE_TEXT("-n")) == 0)
285 threads = ACE_OS::atoi(argv[i + 1]);
287 // create a separate server thread
288 ACE_Thread_Manager server_thr_mgr;
289 // starting the server thread
290 MT_Server_Task *server = new MT_Server_Task (&server_thr_mgr,
291 argc,
292 argv,
293 &orb_manager);
294 if (server->activate () != 0)
296 delete server;
297 ACE_ERROR_RETURN ((LM_ERROR,
298 "CLIENT ERROR: Unable to activate "
299 "MT_Server_Task.\n"),
300 -1);
303 // starting the client threads
304 MT_Client_Task **clients = new MT_Client_Task*[threads];
306 for (i = 0; i < threads; i++)
307 clients[i] = new MT_Client_Task (argc, argv, i);
309 for (i = 0; i < threads; i++)
310 if (clients[i]->activate () != 0)
311 ACE_ERROR_RETURN ((LM_ERROR,
312 "CLIENT ERROR: Unable to activate "
313 "MT_Client_Task.\n"),
314 -1); // @@ Memory leak!
316 result = ACE_Thread_Manager::instance ()->wait ();
318 for (i = 0; i < threads; i++)
319 delete clients[i];
321 delete [] clients;
323 // wait for the server thread to end
324 result |= server_thr_mgr.wait ();
326 delete server;
328 orb_manager.fini ();
330 catch (const CORBA::Exception& ex)
332 ex._tao_print_exception ("main");
335 return result;