Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Consumer_Handler.cpp
blobe82cacb225d66f5a0d8dea42186f4ceb1e81e7e0
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 (void)
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)
40 Consumer_Handler::~Consumer_Handler (void)
42 // Make sure to cleanup the STDIN handler.
44 if (this->interactive_ == 1)
46 if (ACE_Event_Handler::remove_stdin_handler
47 (this->orb_->orb_core ()->reactor (),
48 this->orb_->orb_core ()->thr_mgr ()) == -1)
49 ACE_ERROR ((LM_ERROR,
50 "%p\n",
51 "remove_stdin_handler"));
55 // Reads the Server factory IOR from a file.
57 int
58 Consumer_Handler::read_ior (ACE_TCHAR *filename)
60 // Open the file for reading.
61 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
63 if (f_handle == ACE_INVALID_HANDLE)
64 ACE_ERROR_RETURN ((LM_ERROR,
65 "Unable to open %s for reading: %p\n",
66 filename),
67 -1);
69 ACE_Read_Buffer ior_buffer (f_handle);
70 char *data = ior_buffer.read ();
72 if (data == 0)
73 ACE_ERROR_RETURN ((LM_ERROR,
74 "Unable to read ior: %p\n"),
75 -1);
77 this->ior_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
78 ior_buffer.alloc ()->free (data);
80 ACE_OS::close (f_handle);
82 return 0;
85 // Parses the command line arguments and returns an error status.
87 int
88 Consumer_Handler::parse_args (void)
90 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("a:t:d:f:xk:xs"));
91 int c;
92 int result;
94 while ((c = get_opts ()) != -1)
95 switch (c)
97 case 'd': // debug flag
98 TAO_debug_level++; //****
99 break;
101 case 'k': // ior provide on command line
102 this->ior_ = ACE_OS::strdup (get_opts.opt_arg ());
103 break;
105 case 'f': // read the IOR from the file.
106 result = this->read_ior (get_opts.opt_arg ());
107 if (result < 0)
108 ACE_ERROR_RETURN ((LM_ERROR,
109 "Unable to read ior from %s : %p\n",
110 get_opts.opt_arg ()),
111 -1);
112 break;
114 case 's': // don't use the naming service
115 this->use_naming_service_ = 0;
116 break;
118 case 'a': // to be given only on using run_test.pl
119 this->stock_name_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
120 this->interactive_ = 0;
121 break;
123 case 't':
124 this->threshold_value_ = ACE_OS::atoi (get_opts.opt_arg ());
125 break;
128 case 'x':
129 this->shutdown_ = 1;
130 break;
132 case '?':
133 default:
134 ACE_ERROR_RETURN ((LM_ERROR,
135 "usage: %s"
136 " [-d]"
137 " [-f ior-file]"
138 " [-k ior]"
139 " [-x]"
140 " [-s]"
141 " [-a stock_name]"
142 " [-t threshold]"
143 "\n",
144 this->argv_ [0]),
145 -1);
148 // Indicates successful parsing of command line.
149 return 0;
152 // this method uses the naming service to obtain the server object refernce.
155 Consumer_Handler::via_naming_service (void)
159 // Initialization of the naming service.
160 if (naming_services_client_.init (orb_.in ()) != 0)
161 ACE_ERROR_RETURN ((LM_ERROR,
162 " (%P|%t) Unable to initialize "
163 "the TAO_Naming_Client.\n"),
164 -1);
166 CosNaming::Name notifier_ref_name (1);
167 notifier_ref_name.length (1);
168 notifier_ref_name[0].id = CORBA::string_dup ("Notifier");
170 CORBA::Object_var notifier_obj =
171 this->naming_services_client_->resolve (notifier_ref_name);
173 // The CORBA::Object_var object is downcast to Notifier_var using
174 // the <_narrow> method.
175 this->server_ =
176 Notifier::_narrow (notifier_obj.in ());
180 catch (const CORBA::Exception& ex)
182 ex._tao_print_exception (
183 "Consumer_Handler::via_naming_service\n");
184 return -1;
187 return 0;
190 // Init function.
192 Consumer_Handler::init (int argc, ACE_TCHAR **argv)
195 this->argc_ = argc;
196 this->argv_ = argv;
198 // Register our <Input_Handler> to handle STDIN events, which will
199 // trigger the <handle_input> method to process these events.
203 // Retrieve the ORB.
204 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
206 // Parse command line and verify parameters.
207 if (this->parse_args () == -1)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 "parse_args failed\n"),
210 -1);
212 if (this->interactive_ == 1)
214 ACE_DEBUG ((LM_DEBUG,
215 " Services provided:\n"
216 " * Registration <type 'r'>\n"
217 " * Unregistration <type 'u'>\n"
218 " * Quit <type 'q'>\n"));
220 ACE_NEW_RETURN (consumer_input_handler_,
221 Consumer_Input_Handler (this),
222 -1);
224 if (ACE_Event_Handler::register_stdin_handler
225 (consumer_input_handler_,
226 this->orb_->orb_core ()->reactor (),
227 this->orb_->orb_core ()->thr_mgr ()) == -1)
228 ACE_ERROR_RETURN ((LM_ERROR,
229 "%p\n",
230 "register_stdin_handler"),
231 -1);
233 // Register the signal event handler for ^C
234 ACE_NEW_RETURN (consumer_signal_handler_,
235 Consumer_Signal_Handler (this),
236 -1);
238 if (this->reactor_used ()->register_handler
239 (SIGINT,
240 consumer_signal_handler_) == -1)
241 ACE_ERROR_RETURN ((LM_ERROR,
242 "%p\n",
243 "register_handler for SIGINT"),
244 -1);
246 // use the naming service.
247 if (this->use_naming_service_)
249 if (via_naming_service () == -1)
250 ACE_ERROR_RETURN ((LM_ERROR,
251 "via_naming_service failed\n"),
252 -1);
254 else
257 if (this->ior_ == 0)
258 ACE_ERROR_RETURN ((LM_ERROR,
259 "%s: no ior specified\n",
260 this->argv_[0]),
261 -1);
263 CORBA::Object_var server_object =
264 this->orb_->string_to_object (this->ior_);
266 if (CORBA::is_nil (server_object.in ()))
267 ACE_ERROR_RETURN ((LM_ERROR,
268 "invalid ior <%s>\n",
269 this->ior_),
270 -1);
271 // The downcasting from CORBA::Object_var to Notifier_var is
272 // done using the <_narrow> method.
273 this->server_ = Notifier::_narrow (server_object.in ());
277 catch (const CORBA::Exception& ex)
279 ex._tao_print_exception ("Consumer_Handler::init");
280 return -1;
283 return 0;
287 Consumer_Handler::run (void)
292 // Obtain and activate the RootPOA.
293 CORBA::Object_var obj =
294 this->orb_->resolve_initial_references ("RootPOA");
296 PortableServer::POA_var root_poa =
297 PortableServer::POA::_narrow (obj.in ());
299 PortableServer::POAManager_var poa_manager=
300 root_poa->the_POAManager ();
302 poa_manager->activate ();
304 ACE_NEW_RETURN (this->consumer_servant_,
305 Consumer_i (),
306 -1);
307 // Set the orb in the consumer_ object.
308 this->consumer_servant_->orb (this->orb_.in ());
310 // Get the consumer stub (i.e consumer object) pointer.
311 this->consumer_var_ =
312 this->consumer_servant_->_this ();
314 if (this->interactive_ == 0)
317 // Register with the server.
318 this->server_->register_callback (this->stock_name_.c_str (),
319 this->threshold_value_,
320 this->consumer_var_.in ());
322 // Note the registration.
323 this->registered_ = 1;
324 this->unregistered_ = 0;
326 ACE_DEBUG ((LM_DEBUG,
327 "registeration done!\n"));
330 // Run the ORB.
331 this->orb_->run ();
334 catch (const CORBA::Exception& ex)
336 ex._tao_print_exception ("Consumer_Handler::init");
337 return -1;
340 return 0;
343 ACE_Reactor *
344 Consumer_Handler::reactor_used (void) const
346 return this->orb_->orb_core ()->reactor ();