2 //=============================================================================
4 * @file Consumer_Handler.cpp
6 * Implementation of the Consumer_Handler class.
8 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
10 //=============================================================================
13 #include "Consumer_Handler.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"),
34 use_naming_service_ (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)
51 "remove_stdin_handler"));
55 // Reads the Server factory IOR from a file.
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",
69 ACE_Read_Buffer
ior_buffer (f_handle
);
70 char *data
= ior_buffer
.read ();
73 ACE_ERROR_RETURN ((LM_ERROR
,
74 "Unable to read ior: %p\n"),
77 this->ior_
= ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data
));
78 ior_buffer
.alloc ()->free (data
);
80 ACE_OS::close (f_handle
);
85 // Parses the command line arguments and returns an error status.
88 Consumer_Handler::parse_args (void)
90 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("a:t:d:f:xk:xs"));
94 while ((c
= get_opts ()) != -1)
97 case 'd': // debug flag
98 TAO_debug_level
++; //****
101 case 'k': // ior provide on command line
102 this->ior_
= ACE_OS::strdup (get_opts
.opt_arg ());
105 case 'f': // read the IOR from the file.
106 result
= this->read_ior (get_opts
.opt_arg ());
108 ACE_ERROR_RETURN ((LM_ERROR
,
109 "Unable to read ior from %s : %p\n",
110 get_opts
.opt_arg ()),
114 case 's': // don't use the naming service
115 this->use_naming_service_
= 0;
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;
124 this->threshold_value_
= ACE_OS::atoi (get_opts
.opt_arg ());
134 ACE_ERROR_RETURN ((LM_ERROR
,
148 // Indicates successful parsing of command line.
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"),
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.
176 Notifier::_narrow (notifier_obj
.in ());
180 catch (const CORBA::Exception
& ex
)
182 ex
._tao_print_exception (
183 "Consumer_Handler::via_naming_service\n");
192 Consumer_Handler::init (int argc
, ACE_TCHAR
**argv
)
198 // Register our <Input_Handler> to handle STDIN events, which will
199 // trigger the <handle_input> method to process these events.
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"),
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),
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
,
230 "register_stdin_handler"),
233 // Register the signal event handler for ^C
234 ACE_NEW_RETURN (consumer_signal_handler_
,
235 Consumer_Signal_Handler (this),
238 if (this->reactor_used ()->register_handler
240 consumer_signal_handler_
) == -1)
241 ACE_ERROR_RETURN ((LM_ERROR
,
243 "register_handler for SIGINT"),
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"),
258 ACE_ERROR_RETURN ((LM_ERROR
,
259 "%s: no ior specified\n",
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",
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");
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_
,
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"));
334 catch (const CORBA::Exception
& ex
)
336 ex
._tao_print_exception ("Consumer_Handler::init");
344 Consumer_Handler::reactor_used (void) const
346 return this->orb_
->orb_core ()->reactor ();