Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / NestedUpcall / MT_Client_Test / local_server.cpp
blob0b8ef63068c6662fb88680f70ad8c4a4a9bcbf0e
2 //=============================================================================
3 /**
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,
22 int argc,
23 ACE_TCHAR **argv,
24 TAO_ORB_Manager* orb_manager_ptr)
25 :ACE_Task<ACE_SYNCH> (thr_mgr_ptr),
26 argc_ (argc),
27 argv_ (argv),
28 orb_manager_ptr_ (orb_manager_ptr)
32 int
33 MT_Server_Task::svc (void)
35 if (this->mT_Server_.init (this->argc_,
36 this->argv_,
37 this->orb_manager_ptr_) == -1)
38 return 1;
39 else
40 return this->mT_Server_.run_ORB_briefly ();
44 MT_Server::MT_Server ()
45 : object_key_ (0),
46 ior_output_file_ (0),
47 orb_manager_ptr_ (0),
48 iterations_ (1)
52 // Reads the MT Object IOR from a file
53 int
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",
62 filename),
63 -1);
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"),
71 -1);
73 ACE_OS::close (f_handle);
74 return 0;
79 int
80 MT_Server::parse_args (void)
82 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("d:f:g:h:i:n:s:"));
83 int c;
85 while ((c = get_opts ()) != -1)
86 switch (c)
88 case 'd': // debug flag.
89 TAO_debug_level++;
90 break;
91 case 'h': // read the IOR from the file.
92 int result;
93 result = this->read_ior (get_opts.opt_arg ());
94 // read IOR for MT Object
95 if (result < 0)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 "Unable to read ior from %s : %p\n",
98 get_opts.opt_arg ()),
99 -1);
100 break;
101 case 'f':
102 case 'g':
103 case 'i':
104 case 'n':
105 break;
106 case 's': this->iterations_ = ACE_OS::atoi (get_opts.opt_arg ());
107 break;
108 case '?':
109 default:
110 ACE_ERROR_RETURN ((LM_ERROR,
111 "usage: %s"
112 " [-d]\n"
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"
119 "\n",
120 argv_ [0]),
121 -1);
124 // Indicates successful parsing of command line.
125 return 0;
129 MT_Server::init (int argc,
130 ACE_TCHAR** argv,
131 TAO_ORB_Manager* orb_manager_ptr)
133 this->argc_ = argc;
134 this->argv_ = argv;
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"),
138 -1);
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,
145 argv,
146 "child_poa");
149 this->parse_args ();
150 // ~~ check for the return value here
152 this->str_ =
153 this->orb_manager_ptr_->activate_under_child_poa ("MT",
154 &this->mT_Object_i_);
156 #if 0
157 ACE_DEBUG ((LM_DEBUG,
158 "The IOR is: <%s>\n",
159 this->str_.in ()));
160 #endif /*if 0*/
162 if (this->ior_output_file_)
164 ACE_OS::fprintf (this->ior_output_file_,
165 "%s",
166 this->str_.in ());
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"),
174 -1);
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"),
184 -1);
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"),
192 -1);
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");
201 return -1;
204 return 0;
208 MT_Server::run ()
212 int r = this->orb_manager_ptr_->run ();
214 if (r == -1)
215 ACE_ERROR_RETURN ((LM_ERROR,
216 "MT_Server::run"),
217 -1);
219 catch (const CORBA::Exception& ex)
221 ex._tao_print_exception ("MT_Server::run");
222 return -1;
224 return 0;
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 ());
265 MT_Object_var tmp =
266 MT_Object::_narrow (object_act.in ());
268 this->mT_Object_var_->yadda (0,
269 tmp.in ());
272 catch (const CORBA::Exception& ex)
274 ex._tao_print_exception ("MT_Server::run_ORB_briefly");
275 return -1;
278 return 0;