2 //=============================================================================
6 * This class implements a simple server for the
7 * Nested Upcalls - Triangle test.
9 * @author Michael Kircher
11 //=============================================================================
14 #include "initiator.h"
15 #include "tao/debug.h"
16 #include "ace/Read_Buffer.h"
17 #include "ace/OS_NS_fcntl.h"
18 #include "ace/OS_NS_unistd.h"
20 Initiator_Server::Initiator_Server (void)
31 // Reads the Object A/B IOR from a file
32 // A_B == 0, means read Object A's IOR
33 // A_B == 1, means read Object B's IOR
36 Initiator_Server::read_ior (ACE_TCHAR
*filename
, unsigned int A_B
)
38 // Open the file for reading.
39 ACE_HANDLE f_handle
= ACE_OS::open (filename
,0);
41 if (f_handle
== ACE_INVALID_HANDLE
)
42 ACE_ERROR_RETURN ((LM_ERROR
,
43 "Unable to open %s for reading: %p\n",
46 ACE_Read_Buffer
ior_buffer (f_handle
);
50 this->object_A_key_
= ior_buffer
.read ();
51 if (this->object_A_key_
== 0)
52 ACE_ERROR_RETURN ((LM_ERROR
,
53 "Unable to allocate memory to read ior: %p\n"),
58 this->object_B_key_
= ior_buffer
.read ();
59 if (this->object_B_key_
== 0)
60 ACE_ERROR_RETURN ((LM_ERROR
,
61 "Unable to allocate memory to read ior: %p\n"),
65 ACE_OS::close (f_handle
);
71 Initiator_Server::parse_args (void)
73 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("df:g:"));
76 while ((c
= get_opts ()) != -1)
79 case 'd': // debug flag.
82 case 'f': // read the IOR from the file.
83 result
= this->read_ior (get_opts
.opt_arg (),0);
84 // read IOR for Object A
86 ACE_ERROR_RETURN ((LM_ERROR
,
87 "Unable to read ior from %s : %p\n",
91 case 'g': // read the IOR from the file.
92 result
= this->read_ior (get_opts
.opt_arg (),1);
93 // read IOR for Object A
95 ACE_ERROR_RETURN ((LM_ERROR
,
96 "Unable to read ior from %s : %p\n",
102 ACE_ERROR_RETURN ((LM_ERROR
,
105 " [-f] <object_A_ior_file>"
106 " [-g] <object_B_ior_file>"
112 // Indicates successful parsing of command line.
117 Initiator_Server::init (int argc
,
120 // Call the init of TAO_ORB_Manager to create a child POA
121 // under the root POA.
122 this->orb_manager_
.init_child_poa (argc
,
130 // ~~ check for the return value here
132 if (this->object_A_key_
== 0 || this->object_B_key_
== 0)
133 ACE_ERROR_RETURN ((LM_ERROR
,
134 "%s: The two objects A and B are missing\n",
140 CORBA::ORB_var orb
= this->orb_manager_
.orb();
143 CORBA::Object_var object_A_obj_var
=
144 orb
->string_to_object (this->object_A_key_
);
146 this->object_A_var_
=
147 Object_A::_narrow (object_A_obj_var
.in());
149 if (CORBA::is_nil (this->object_A_var_
.in ()))
150 ACE_ERROR_RETURN ((LM_ERROR
,
151 "invalid object A key <%s>\n",
156 ACE_DEBUG ((LM_DEBUG
,
157 "Object A IOR: %s\n",
158 this->object_A_key_
));
161 ACE_DEBUG ((LM_DEBUG
, "Object A received OK\n"));
165 CORBA::Object_var object_B_obj_var
=
166 orb
->string_to_object (this->object_B_key_
);
168 this->object_B_var_
=
169 Object_B::_narrow (object_B_obj_var
.in());
171 if (CORBA::is_nil (this->object_B_var_
.in ()))
172 ACE_ERROR_RETURN ((LM_ERROR
,
173 "invalid object b key <%s>\n",
178 ACE_DEBUG ((LM_DEBUG
,
179 "Object B IOR: %s\n",
180 this->object_A_key_
));
183 ACE_DEBUG ((LM_DEBUG
, "Object B received OK\n"));
185 this->orb_manager_
.activate_poa_manager ();
187 catch (const CORBA::SystemException
& sysex
)
189 sysex
._tao_print_exception ("System Exception");
192 catch (const CORBA::UserException
& userex
)
194 userex
._tao_print_exception ("User Exception");
198 ACE_NEW_RETURN (this->initiator_i_ptr_
,
199 Initiator_i(this->object_A_var_
.in(),
200 this->object_B_var_
.in()),
204 this->orb_manager_
.activate (this->initiator_i_ptr_
);
207 ACE_DEBUG ((LM_DEBUG
,
208 "The IOR is: <%s>\n",
217 Initiator_Server::run (void)
221 ACE_DEBUG ((LM_DEBUG
,
222 "Initiator_Server::run: Trying to invoke "
223 "foo on Object A\n"));
225 Initiator_var initiator
= this->initiator_i_ptr_
->_this ();
227 this->object_A_var_
->foo (initiator
.in ());
228 ACE_DEBUG ((LM_DEBUG
,
229 "Initiator_Server::run: Returned from invoke "
230 "foo on Object A\n"));
232 catch (const CORBA::SystemException
& sysex
)
234 sysex
._tao_print_exception ("System Exception");
237 catch (const CORBA::UserException
& userex
)
239 userex
._tao_print_exception ("User Exception");
246 Initiator_Server::~Initiator_Server (void)
248 if (this->object_A_key_
!= 0)
249 ACE_Allocator::instance ()->free (this->object_A_key_
);
250 if (this->object_B_key_
!= 0)
251 ACE_Allocator::instance ()->free (this->object_B_key_
);
253 this->object_A_var_
->shutdown ();
254 this->object_B_var_
->shutdown ();
258 this->orb_manager_
.deactivate (this->str_
.in ());
260 catch (const CORBA::Exception
& ex
)
262 ex
._tao_print_exception (
263 "Initiator_Server::~Initiator_Server");
266 delete this->initiator_i_ptr_
;
270 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
272 ACE_DEBUG ((LM_DEBUG
,
273 "\n \t NestedUpCalls.Triangle_Test: Initiator Server \n\n"));
277 Initiator_Server initiator_Server
;
280 initiator_Server
.init (argc
, argv
);
286 initiator_Server
.run ();
289 catch (const CORBA::SystemException
& sysex
)
291 sysex
._tao_print_exception ("System Exception");
294 catch (const CORBA::UserException
& userex
)
296 userex
._tao_print_exception ("User Exception");