2 //=============================================================================
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
,
24 TAO_ORB_Manager
* orb_manager_ptr
)
25 :ACE_Task
<ACE_SYNCH
> (thr_mgr_ptr
),
28 orb_manager_ptr_ (orb_manager_ptr
)
33 MT_Server_Task::svc ()
35 if (this->mT_Server_
.init (this->argc_
,
37 this->orb_manager_ptr_
) == -1)
40 return this->mT_Server_
.run_ORB_briefly ();
44 MT_Server::MT_Server ()
52 // Reads the MT Object IOR from a file
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",
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"),
73 ACE_OS::close (f_handle
);
79 MT_Server::parse_args ()
81 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("d:f:g:h:i:n:s:"));
84 while ((c
= get_opts ()) != -1)
87 case 'd': // debug flag.
90 case 'h': // read the IOR from the file.
92 result
= this->read_ior (get_opts
.opt_arg ());
93 // read IOR for MT Object
95 ACE_ERROR_RETURN ((LM_ERROR
,
96 "Unable to read ior from %s : %p\n",
105 case 's': this->iterations_
= ACE_OS::atoi (get_opts
.opt_arg ());
109 ACE_ERROR_RETURN ((LM_ERROR
,
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"
123 // Indicates successful parsing of command line.
128 MT_Server::init (int argc
,
130 TAO_ORB_Manager
* orb_manager_ptr
)
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"),
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
,
149 // ~~ check for the return value here
152 this->orb_manager_ptr_
->activate_under_child_poa ("MT",
153 &this->mT_Object_i_
);
156 ACE_DEBUG ((LM_DEBUG
,
157 "The IOR is: <%s>\n",
161 if (this->ior_output_file_
)
163 ACE_OS::fprintf (this->ior_output_file_
,
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"),
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"),
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"),
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");
211 int r
= this->orb_manager_ptr_
->run ();
214 ACE_ERROR_RETURN ((LM_ERROR
,
218 catch (const CORBA::Exception
& ex
)
220 ex
._tao_print_exception ("MT_Server::run");
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 ());
265 MT_Object::_narrow (object_act
.in ());
267 this->mT_Object_var_
->yadda (0,
271 catch (const CORBA::Exception
& ex
)
273 ex
._tao_print_exception ("MT_Server::run_ORB_briefly");