2 //=============================================================================
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 //=============================================================================
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
,
29 client_number_ (client_number
)
34 MT_Client_Task::svc (void)
36 if (this->mT_Client_
.init (this->argc_
,
38 this->client_number_
) == -1)
41 return this->mT_Client_
.run ();
45 MT_Client::MT_Client ()
52 // Reads the Object A IOR from a file
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",
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"),
74 ACE_OS::close (f_handle
);
79 // Parses the command line arguments and returns an error status.
82 MT_Client::parse_args (void)
84 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("df:g:h:i:n:s:"));
88 while ((c
= get_opts ()) != -1)
91 case 'd': // debug flag
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
101 ACE_ERROR_RETURN ((LM_ERROR
,
102 "Unable to read ior from %s\n",
103 get_opts
.opt_arg ()),
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
113 ACE_ERROR_RETURN ((LM_ERROR
,
114 "Unable to read ior from %s\n",
115 get_opts
.opt_arg ()),
119 case 'i': this->iterations_
= ACE_OS::atoi (get_opts
.opt_arg ());
127 ACE_ERROR_RETURN ((LM_ERROR
,
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",
139 // Indicates successful parsing of command line.
144 MT_Client::run (void)
148 for (unsigned long i
= 0; i
< this->iterations_
; i
++)
151 ACE_DEBUG ((LM_DEBUG
,
152 "(%P|%t) MT_Client::run: %d of %d\n",
157 // call the recursive object MT_Object for nested upcalls
159 this->mT_Object_var_
->yadda (0,
163 catch (const CORBA::Exception
& ex
)
165 ex
._tao_print_exception ("MT_Client:run");
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
,
186 // Make a copy of argv since ORB_init will change it.
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
;
197 ACE_OS::sprintf (buf
, "thread_%lx", ACE_Utils::truncate_cast
<long> ((intptr_t)this));
200 CORBA::ORB_init (this->argc_
,
204 // Parse command line and verify parameters.
205 if (this->parse_args () == -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"),
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"),
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"),
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");
259 // This function runs the test.
262 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
268 TAO_ORB_Manager orb_manager
;
270 int r
= orb_manager
.init (argc
,
274 ACE_ERROR_RETURN ((LM_ERROR
,
275 "ERROR: ORB_Manager initialization failed.\n"),
278 ACE_DEBUG ((LM_DEBUG
,"\n\tMT_Client: client\n\n"));
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
,
294 if (server
->activate () != 0)
297 ACE_ERROR_RETURN ((LM_ERROR
,
298 "CLIENT ERROR: Unable to activate "
299 "MT_Server_Task.\n"),
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
++)
323 // wait for the server thread to end
324 result
|= server_thr_mgr
.wait ();
330 catch (const CORBA::Exception
& ex
)
332 ex
._tao_print_exception ("main");