Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / NestedUpcall / Triangle_Test / initiator.cpp
blob079ff88d929958e2c54daf522d76b91b33b1a479
2 //=============================================================================
3 /**
4 * @file initiator.cpp
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)
21 : object_A_key_ (0),
22 object_B_key_ (0),
23 object_A_var_ (0),
24 object_B_var_ (0),
25 initiator_i_ptr_ (0),
26 argc_ (0),
27 argv_ (0)
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
35 int
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",
44 filename),
45 -1);
46 ACE_Read_Buffer ior_buffer (f_handle);
48 if (A_B == 0)
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"),
54 -1);
56 else
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"),
62 -1);
65 ACE_OS::close (f_handle);
66 return 0;
70 int
71 Initiator_Server::parse_args (void)
73 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:g:"));
74 int c, result;
76 while ((c = get_opts ()) != -1)
77 switch (c)
79 case 'd': // debug flag.
80 TAO_debug_level++;
81 break;
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
85 if (result < 0)
86 ACE_ERROR_RETURN ((LM_ERROR,
87 "Unable to read ior from %s : %p\n",
88 get_opts.opt_arg ()),
89 -1);
90 break;
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
94 if (result < 0)
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "Unable to read ior from %s : %p\n",
97 get_opts.opt_arg ()),
98 -1);
99 break;
100 case '?':
101 default:
102 ACE_ERROR_RETURN ((LM_ERROR,
103 "usage: %s"
104 " [-d]"
105 " [-f] <object_A_ior_file>"
106 " [-g] <object_B_ior_file>"
107 "\n",
108 argv_ [0]),
112 // Indicates successful parsing of command line.
113 return 0;
117 Initiator_Server::init (int argc,
118 ACE_TCHAR** argv)
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,
123 argv,
124 "child_poa");
126 this->argc_ = argc;
127 this->argv_ = argv;
129 this->parse_args ();
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",
135 this->argv_[0]),
136 -1);
140 CORBA::ORB_var orb = this->orb_manager_.orb();
141 // Get Object A
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",
152 object_A_key_),
153 -1);
155 #if 0
156 ACE_DEBUG ((LM_DEBUG,
157 "Object A IOR: %s\n",
158 this->object_A_key_));
159 #endif /*if 0*/
161 ACE_DEBUG ((LM_DEBUG, "Object A received OK\n"));
163 // Get Object B
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",
174 object_B_key_),
175 -1);
177 #if 0
178 ACE_DEBUG ((LM_DEBUG,
179 "Object B IOR: %s\n",
180 this->object_A_key_));
181 #endif /*if 0*/
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");
190 return -1;
192 catch (const CORBA::UserException& userex)
194 userex._tao_print_exception ("User Exception");
195 return -1;
198 ACE_NEW_RETURN (this->initiator_i_ptr_,
199 Initiator_i(this->object_A_var_.in(),
200 this->object_B_var_.in()),
201 -1);
203 this->str_ =
204 this->orb_manager_.activate (this->initiator_i_ptr_);
206 #if 0
207 ACE_DEBUG ((LM_DEBUG,
208 "The IOR is: <%s>\n",
209 this->str_.in ()));
210 #endif /*if 0*/
212 return 0;
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");
235 return -1;
237 catch (const CORBA::UserException& userex)
239 userex._tao_print_exception ("User Exception");
240 return -1;
243 return 0;
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;
279 int retval =
280 initiator_Server.init (argc, argv);
282 if (retval == -1)
283 return 1;
284 else
286 initiator_Server.run ();
289 catch (const CORBA::SystemException& sysex)
291 sysex._tao_print_exception ("System Exception");
292 return -1;
294 catch (const CORBA::UserException& userex)
296 userex._tao_print_exception ("User Exception");
297 return -1;
299 return 0;