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 (void)
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
);
80 MT_Server::parse_args (void)
82 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("d:f:g:h:i:n:s:"));
85 while ((c
= get_opts ()) != -1)
88 case 'd': // debug flag.
91 case 'h': // read the IOR from the file.
93 result
= this->read_ior (get_opts
.opt_arg ());
94 // read IOR for MT Object
96 ACE_ERROR_RETURN ((LM_ERROR
,
97 "Unable to read ior from %s : %p\n",
106 case 's': this->iterations_
= ACE_OS::atoi (get_opts
.opt_arg ());
110 ACE_ERROR_RETURN ((LM_ERROR
,
113 " [-f] first server ior file\n"
114 " [-g] second server ior file\n"
115 " [-h] third server ior file\n"
116 " [-i] client iterations\n"
117 " [-n] number of client threads\n"
118 " [-s] number of server iterations\n"
124 // Indicates successful parsing of command line.
129 MT_Server::init (int argc
,
131 TAO_ORB_Manager
* orb_manager_ptr
)
135 if ((this->orb_manager_ptr_
= orb_manager_ptr
) == 0)
136 ACE_ERROR_RETURN ((LM_ERROR
,
137 "MT_Server::init: ORB_Manager is nil!\n"),
142 // Call the init of TAO_ORB_Manager to create a child POA
143 // under the root POA.
144 this->orb_manager_ptr_
->init_child_poa (argc
,
150 // ~~ check for the return value here
153 this->orb_manager_ptr_
->activate_under_child_poa ("MT",
154 &this->mT_Object_i_
);
157 ACE_DEBUG ((LM_DEBUG
,
158 "The IOR is: <%s>\n",
162 if (this->ior_output_file_
)
164 ACE_OS::fprintf (this->ior_output_file_
,
167 ACE_OS::fclose (this->ior_output_file_
);
170 // retrieve the object reference to the distant mt object
171 if (this->object_key_
== 0)
172 ACE_ERROR_RETURN ((LM_ERROR
,
173 "The IOR is nil, not able to get the object.\n"),
176 CORBA::ORB_var orb_var
= this->orb_manager_ptr_
->orb ();
178 CORBA::Object_var object_var
=
179 orb_var
->string_to_object (this->object_key_
);
181 if (CORBA::is_nil (object_var
.in()))
182 ACE_ERROR_RETURN ((LM_ERROR
,
183 "No proper object has been returned.\n"),
186 this->mT_Object_var_
= MT_Object::_narrow (object_var
.in());
188 if (CORBA::is_nil (this->mT_Object_var_
.in()))
190 ACE_ERROR_RETURN ((LM_ERROR
,
191 "We have no proper reference to the Object.\n"),
195 if (TAO_debug_level
> 0)
196 ACE_DEBUG ((LM_DEBUG
, "We have a proper reference to the Object.\n"));
198 catch (const CORBA::Exception
& ex
)
200 ex
._tao_print_exception ("MT_Client::init");
212 int r
= this->orb_manager_ptr_
->run ();
215 ACE_ERROR_RETURN ((LM_ERROR
,
219 catch (const CORBA::Exception
& ex
)
221 ex
._tao_print_exception ("MT_Server::run");
227 MT_Server::~MT_Server (void)
229 if (this->object_key_
!= 0)
230 ACE_OS::free (this->object_key_
);
234 if (this->orb_manager_ptr_
)
235 this->orb_manager_ptr_
->deactivate_under_child_poa (this->str_
.in ());
237 catch (const CORBA::Exception
& ex
)
239 ex
._tao_print_exception ("MT_Client::~MT_Client");
245 MT_Server::run_ORB_briefly (void)
247 if (this->iterations_
> 0)
251 ACE_DEBUG ((LM_DEBUG
,
252 "(%P|%t) MT_Server::run: "
253 "going to call distant MT Object\n"));
255 PortableServer::POA_var root_poa
=
256 this->orb_manager_ptr_
->root_poa ();
258 for (unsigned int i
= 0; i
< this->iterations_
; i
++)
260 PortableServer::ObjectId_var id
=
261 root_poa
->activate_object (&mT_Object_i_
);
263 CORBA::Object_var object_act
= root_poa
->id_to_reference (id
.in ());
266 MT_Object::_narrow (object_act
.in ());
268 this->mT_Object_var_
->yadda (0,
272 catch (const CORBA::Exception
& ex
)
274 ex
._tao_print_exception ("MT_Server::run_ORB_briefly");