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 ()
27 : stock_name_ ("Unknown"),
34 use_naming_service_ (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)
49 "remove_stdin_handler"));
53 // Reads the Server factory IOR from a file.
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",
66 ACE_Read_Buffer
ior_buffer (f_handle
);
67 char *data
= ior_buffer
.read ();
70 ACE_ERROR_RETURN ((LM_ERROR
,
71 "Unable to read ior: %p\n"),
74 this->ior_
= ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data
));
75 ior_buffer
.alloc ()->free (data
);
77 ACE_OS::close (f_handle
);
82 // Parses the command line arguments and returns an error status.
84 Consumer_Handler::parse_args ()
86 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("a:t:d:f:xk:xs"));
90 while ((c
= get_opts ()) != -1)
93 case 'd': // debug flag
94 TAO_debug_level
++; //****
97 case 'k': // ior provide on command line
98 this->ior_
= ACE_OS::strdup (get_opts
.opt_arg ());
101 case 'f': // read the IOR from the file.
102 result
= this->read_ior (get_opts
.opt_arg ());
104 ACE_ERROR_RETURN ((LM_ERROR
,
105 "Unable to read ior from %s : %p\n",
106 get_opts
.opt_arg ()),
110 case 's': // don't use the naming service
111 this->use_naming_service_
= 0;
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;
120 this->threshold_value_
= ACE_OS::atoi (get_opts
.opt_arg ());
129 ACE_ERROR_RETURN ((LM_ERROR
,
143 // Indicates successful parsing of command line.
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"),
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.
170 Notifier::_narrow (notifier_obj
.in ());
172 catch (const CORBA::Exception
& ex
)
174 ex
._tao_print_exception (
175 "Consumer_Handler::via_naming_service\n");
184 Consumer_Handler::init (int argc
, ACE_TCHAR
**argv
)
189 // Register our <Input_Handler> to handle STDIN events, which will
190 // trigger the <handle_input> method to process these events.
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"),
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),
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
,
221 "register_stdin_handler"),
224 // Register the signal event handler for ^C
225 ACE_NEW_RETURN (consumer_signal_handler_
,
226 Consumer_Signal_Handler (this),
229 if (this->reactor_used ()->register_handler
231 consumer_signal_handler_
) == -1)
232 ACE_ERROR_RETURN ((LM_ERROR
,
234 "register_handler for SIGINT"),
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"),
248 ACE_ERROR_RETURN ((LM_ERROR
,
249 "%s: no ior specified\n",
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",
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");
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_
,
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"));
321 catch (const CORBA::Exception
& ex
)
323 ex
._tao_print_exception ("Consumer_Handler::init");
331 Consumer_Handler::reactor_used () const
333 return this->orb_
->orb_core ()->reactor ();