2 //=============================================================================
4 * @file Notifier_Input_Handler.cpp
6 * Implementation of the callback quoter Notifier_Input_Handler class.
8 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
10 //=============================================================================
13 #include "Notifier_Input_Handler.h"
14 #include "tao/debug.h"
15 #include "tao/ORB_Core.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/OS_NS_unistd.h"
18 #include "ace/OS_NS_ctype.h"
22 Notifier_Input_Handler::Notifier_Input_Handler (void)
23 : ior_output_file_ (0),
26 using_naming_service_ (1)
32 Notifier_Input_Handler::~Notifier_Input_Handler (void)
34 // Make sure to cleanup the STDIN handler.
36 if (ACE_Event_Handler::remove_stdin_handler
37 (this->notifier_i_
.orb_
->orb_core ()->reactor (),
38 this->notifier_i_
.orb_
->orb_core ()->thr_mgr ()) == -1)
41 "remove_stdin_handler"));
45 // The naming service is initialized and the naming context as well as
46 // the object name is bound to the naming server.
49 Notifier_Input_Handler::init_naming_service (void)
52 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
54 if (this->naming_server_
.init (orb
.in ()) == -1)
57 // create the name for the naming service
58 CosNaming::Name
notifier_obj_name (1);
59 notifier_obj_name
.length (1);
60 notifier_obj_name
[0].id
= CORBA::string_dup ("Notifier");
62 // (re)Bind the object.
65 Notifier_var notifier_obj
= notifier_i_
._this ();
67 this->orb_manager_
.activate_poa_manager ();
69 naming_server_
->rebind (notifier_obj_name
,
73 catch (const CosNaming::NamingContext::AlreadyBound
&)
75 ACE_ERROR_RETURN ((LM_ERROR
,
76 "Unable to bind %s\n",
84 // Parse the command-line arguments and set options.
86 Notifier_Input_Handler::parse_args (void)
88 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("df:s "));
91 while ((c
= get_opts ()) != -1)
94 case 'd': // debug flag.
95 TAO_debug_level
++; ///*****
98 case 'f': // output the IOR toi a file.
99 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
100 if (this->ior_output_file_
== 0)
101 ACE_ERROR_RETURN ((LM_ERROR
,
102 "Unable to open %s for writing: %p\n",
103 get_opts
.opt_arg ()),
107 case 's': // don't use the naming service
108 this->using_naming_service_
= 0;
111 case '?': // display help for use of the server.
113 ACE_ERROR_RETURN ((LM_ERROR
,
116 " [-f] <ior_output_file>"
123 // Indicates successful parsing of command line.
127 // Initialize the server.
130 Notifier_Input_Handler::init (int argc
,
134 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
135 // create the child poa under the root POA.
140 if (this->orb_manager_
.init_child_poa (this->argc_
,
143 ACE_ERROR_RETURN ((LM_ERROR
,
148 int retval
= this->parse_args ();
153 // Register our <Input_Handler> to handle STDIN events, which will
154 // trigger the <handle_input> method to process these events.
156 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
158 if (ACE_Event_Handler::register_stdin_handler
160 orb
->orb_core ()->reactor (),
161 orb
->orb_core ()->thr_mgr ()) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR
,
164 "register_stdin_handler"),
167 // Stash our ORB pointer for later reference.
168 this->notifier_i_
.orb (orb
.in ());
170 // Activate the servant in the POA.
171 CORBA::String_var str
=
172 this->orb_manager_
.activate_under_child_poa ("Notifier",
175 ACE_DEBUG ((LM_DEBUG
,
176 "The IOR is: <%s>\n",
179 if (this->ior_output_file_
)
181 ACE_OS::fprintf (this->ior_output_file_
,
184 ACE_OS::fclose (this->ior_output_file_
);
187 if (this->using_naming_service_
)
189 this->init_naming_service ();
195 Notifier_Input_Handler::run (void)
197 // Run the main event loop for the ORB.
200 ACE_DEBUG ((LM_DEBUG
,
201 " Type \"q\" to quit \n"));
203 int result
= this->orb_manager_
.run ();
207 ACE_ERROR_RETURN ((LM_ERROR
,
208 "Notifier_Input_Handler::run"),
216 Notifier_Input_Handler::handle_input (ACE_HANDLE
)
223 // The string could read contains \n\0 hence using ACE_OS::read
224 // which returns the no of bytes read and hence i can manipulate
225 // and remove the devil from the picture i.e '\n' ! ;)
227 ssize_t strlen
= ACE_OS::read (ACE_STDIN
,
230 if (buf
[strlen
-1] == '\n')
231 buf
[strlen
-1] = '\0';
233 ACE_DEBUG ((LM_DEBUG
,
237 if (ACE_OS::ace_tolower(buf
[0]) == 'q')
239 // @@ Please remove this call if it's not used.
240 // (this->notifier_i_.consumer_map_).close();
241 this->notifier_i_
.shutdown ();
244 catch (const CORBA::Exception
& ex
)
246 ex
._tao_print_exception ("Input_Handler::init");