Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Consumer_Handler.cpp
blob96809c6d36484e770a4c04b6414d826b5c0764cc
2 //=============================================================================
3 /**
4 * @file Consumer_Handler.cpp
6 * Implementation of the Consumer_Handler class.
8 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "Consumer_Handler.h"
15 #include "tao/ORB.h"
16 #include "tao/ORB_Core.h"
17 #include "tao/debug.h"
19 #include "ace/Read_Buffer.h"
20 #include "ace/Get_Opt.h"
21 #include "ace/Read_Buffer.h"
22 #include "ace/Reactor.h"
23 #include "ace/Event_Handler.h"
24 #include "ace/OS_NS_fcntl.h"
26 Consumer_Handler::Consumer_Handler ()
27 : stock_name_ ("Unknown"),
28 threshold_value_ (0),
29 server_ (),
30 registered_ (0),
31 unregistered_ (0),
32 ior_ (0),
33 shutdown_ (0),
34 use_naming_service_ (1),
35 interactive_ (1)
39 Consumer_Handler::~Consumer_Handler ()
41 // Make sure to cleanup the STDIN handler.
42 if (this->interactive_ == 1)
44 if (ACE_Event_Handler::remove_stdin_handler
45 (this->orb_->orb_core ()->reactor (),
46 this->orb_->orb_core ()->thr_mgr ()) == -1)
47 ACE_ERROR ((LM_ERROR,
48 "%p\n",
49 "remove_stdin_handler"));
53 // Reads the Server factory IOR from a file.
54 int
55 Consumer_Handler::read_ior (ACE_TCHAR *filename)
57 // Open the file for reading.
58 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
60 if (f_handle == ACE_INVALID_HANDLE)
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "Unable to open %s for reading: %p\n",
63 filename),
64 -1);
66 ACE_Read_Buffer ior_buffer (f_handle);
67 char *data = ior_buffer.read ();
69 if (data == 0)
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "Unable to read ior: %p\n"),
72 -1);
74 this->ior_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
75 ior_buffer.alloc ()->free (data);
77 ACE_OS::close (f_handle);
79 return 0;
82 // Parses the command line arguments and returns an error status.
83 int
84 Consumer_Handler::parse_args ()
86 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("a:t:d:f:xk:xs"));
87 int c;
88 int result;
90 while ((c = get_opts ()) != -1)
91 switch (c)
93 case 'd': // debug flag
94 TAO_debug_level++; //****
95 break;
97 case 'k': // ior provide on command line
98 this->ior_ = ACE_OS::strdup (get_opts.opt_arg ());
99 break;
101 case 'f': // read the IOR from the file.
102 result = this->read_ior (get_opts.opt_arg ());
103 if (result < 0)
104 ACE_ERROR_RETURN ((LM_ERROR,
105 "Unable to read ior from %s : %p\n",
106 get_opts.opt_arg ()),
107 -1);
108 break;
110 case 's': // don't use the naming service
111 this->use_naming_service_ = 0;
112 break;
114 case 'a': // to be given only on using run_test.pl
115 this->stock_name_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
116 this->interactive_ = 0;
117 break;
119 case 't':
120 this->threshold_value_ = ACE_OS::atoi (get_opts.opt_arg ());
121 break;
123 case 'x':
124 this->shutdown_ = 1;
125 break;
127 case '?':
128 default:
129 ACE_ERROR_RETURN ((LM_ERROR,
130 "usage: %s"
131 " [-d]"
132 " [-f ior-file]"
133 " [-k ior]"
134 " [-x]"
135 " [-s]"
136 " [-a stock_name]"
137 " [-t threshold]"
138 "\n",
139 this->argv_ [0]),
140 -1);
143 // Indicates successful parsing of command line.
144 return 0;
147 // This method uses the naming service to obtain the server object reference.
149 Consumer_Handler::via_naming_service ()
153 // Initialization of the naming service.
154 if (naming_services_client_.init (orb_.in ()) != 0)
155 ACE_ERROR_RETURN ((LM_ERROR,
156 " (%P|%t) Unable to initialize "
157 "the TAO_Naming_Client.\n"),
158 -1);
160 CosNaming::Name notifier_ref_name (1);
161 notifier_ref_name.length (1);
162 notifier_ref_name[0].id = CORBA::string_dup ("Notifier");
164 CORBA::Object_var notifier_obj =
165 this->naming_services_client_->resolve (notifier_ref_name);
167 // The CORBA::Object_var object is downcast to Notifier_var using
168 // the <_narrow> method.
169 this->server_ =
170 Notifier::_narrow (notifier_obj.in ());
172 catch (const CORBA::Exception& ex)
174 ex._tao_print_exception (
175 "Consumer_Handler::via_naming_service\n");
176 return -1;
179 return 0;
182 // Init function.
184 Consumer_Handler::init (int argc, ACE_TCHAR **argv)
186 this->argc_ = argc;
187 this->argv_ = argv;
189 // Register our <Input_Handler> to handle STDIN events, which will
190 // trigger the <handle_input> method to process these events.
194 // Retrieve the ORB.
195 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
197 // Parse command line and verify parameters.
198 if (this->parse_args () == -1)
199 ACE_ERROR_RETURN ((LM_ERROR,
200 "parse_args failed\n"),
201 -1);
203 if (this->interactive_ == 1)
205 ACE_DEBUG ((LM_DEBUG,
206 " Services provided:\n"
207 " * Registration <type 'r'>\n"
208 " * Unregistration <type 'u'>\n"
209 " * Quit <type 'q'>\n"));
211 ACE_NEW_RETURN (consumer_input_handler_,
212 Consumer_Input_Handler (this),
213 -1);
215 if (ACE_Event_Handler::register_stdin_handler
216 (consumer_input_handler_,
217 this->orb_->orb_core ()->reactor (),
218 this->orb_->orb_core ()->thr_mgr ()) == -1)
219 ACE_ERROR_RETURN ((LM_ERROR,
220 "%p\n",
221 "register_stdin_handler"),
222 -1);
224 // Register the signal event handler for ^C
225 ACE_NEW_RETURN (consumer_signal_handler_,
226 Consumer_Signal_Handler (this),
227 -1);
229 if (this->reactor_used ()->register_handler
230 (SIGINT,
231 consumer_signal_handler_) == -1)
232 ACE_ERROR_RETURN ((LM_ERROR,
233 "%p\n",
234 "register_handler for SIGINT"),
235 -1);
237 // use the naming service.
238 if (this->use_naming_service_)
240 if (via_naming_service () == -1)
241 ACE_ERROR_RETURN ((LM_ERROR,
242 "via_naming_service failed\n"),
243 -1);
245 else
247 if (this->ior_ == 0)
248 ACE_ERROR_RETURN ((LM_ERROR,
249 "%s: no ior specified\n",
250 this->argv_[0]),
251 -1);
253 CORBA::Object_var server_object =
254 this->orb_->string_to_object (this->ior_);
256 if (CORBA::is_nil (server_object.in ()))
257 ACE_ERROR_RETURN ((LM_ERROR,
258 "invalid ior <%s>\n",
259 this->ior_),
260 -1);
261 // The downcasting from CORBA::Object_var to Notifier_var is
262 // done using the <_narrow> method.
263 this->server_ = Notifier::_narrow (server_object.in ());
267 catch (const CORBA::Exception& ex)
269 ex._tao_print_exception ("Consumer_Handler::init");
270 return -1;
273 return 0;
277 Consumer_Handler::run ()
281 // Obtain and activate the RootPOA.
282 CORBA::Object_var obj =
283 this->orb_->resolve_initial_references ("RootPOA");
285 PortableServer::POA_var root_poa =
286 PortableServer::POA::_narrow (obj.in ());
288 PortableServer::POAManager_var poa_manager=
289 root_poa->the_POAManager ();
291 poa_manager->activate ();
293 ACE_NEW_RETURN (this->consumer_servant_,
294 Consumer_i (),
295 -1);
296 // Set the orb in the consumer_ object.
297 this->consumer_servant_->orb (this->orb_.in ());
299 // Get the consumer stub (i.e consumer object) pointer.
300 this->consumer_var_ =
301 this->consumer_servant_->_this ();
303 if (this->interactive_ == 0)
305 // Register with the server.
306 this->server_->register_callback (this->stock_name_.c_str (),
307 this->threshold_value_,
308 this->consumer_var_.in ());
310 // Note the registration.
311 this->registered_ = 1;
312 this->unregistered_ = 0;
314 ACE_DEBUG ((LM_DEBUG,
315 "registeration done!\n"));
318 // Run the ORB.
319 this->orb_->run ();
321 catch (const CORBA::Exception& ex)
323 ex._tao_print_exception ("Consumer_Handler::init");
324 return -1;
327 return 0;
330 ACE_Reactor *
331 Consumer_Handler::reactor_used () const
333 return this->orb_->orb_core ()->reactor ();