1 //=============================================================================
3 * @file Notifier_Input_Handler.cpp
5 * Implementation of the callback quoter Notifier_Input_Handler class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
11 #include "Notifier_Input_Handler.h"
12 #include "tao/debug.h"
13 #include "tao/ORB_Core.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/OS_NS_unistd.h"
16 #include "ace/OS_NS_ctype.h"
19 Notifier_Input_Handler::Notifier_Input_Handler ()
20 : ior_output_file_ (0),
23 using_naming_service_ (1)
28 Notifier_Input_Handler::~Notifier_Input_Handler ()
30 // Make sure to cleanup the STDIN handler.
31 if (ACE_Event_Handler::remove_stdin_handler
32 (this->notifier_i_
.orb_
->orb_core ()->reactor (),
33 this->notifier_i_
.orb_
->orb_core ()->thr_mgr ()) == -1)
36 "remove_stdin_handler"));
39 // The naming service is initialized and the naming context as well as
40 // the object name is bound to the naming server.
42 Notifier_Input_Handler::init_naming_service ()
44 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
46 if (this->naming_server_
.init (orb
.in ()) == -1)
49 // create the name for the naming service
50 CosNaming::Name
notifier_obj_name (1);
51 notifier_obj_name
.length (1);
52 notifier_obj_name
[0].id
= CORBA::string_dup ("Notifier");
54 // (re)Bind the object.
57 Notifier_var notifier_obj
= notifier_i_
._this ();
59 this->orb_manager_
.activate_poa_manager ();
61 naming_server_
->rebind (notifier_obj_name
,
64 catch (const CosNaming::NamingContext::AlreadyBound
&)
66 ACE_ERROR_RETURN ((LM_ERROR
,
67 "Unable to bind %s\n",
75 // Parse the command-line arguments and set options.
77 Notifier_Input_Handler::parse_args ()
79 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("df:s "));
82 while ((c
= get_opts ()) != -1)
85 case 'd': // debug flag.
86 TAO_debug_level
++; ///*****
89 case 'f': // output the IOR toi a file.
90 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
91 if (this->ior_output_file_
== 0)
92 ACE_ERROR_RETURN ((LM_ERROR
,
93 "Unable to open %s for writing: %p\n",
98 case 's': // don't use the naming service
99 this->using_naming_service_
= 0;
102 case '?': // display help for use of the server.
104 ACE_ERROR_RETURN ((LM_ERROR
,
107 " [-f] <ior_output_file>"
114 // Indicates successful parsing of command line.
118 // Initialize the server.
120 Notifier_Input_Handler::init (int argc
,
123 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
124 // create the child poa under the root POA.
128 if (this->orb_manager_
.init_child_poa (this->argc_
,
131 ACE_ERROR_RETURN ((LM_ERROR
,
136 int retval
= this->parse_args ();
141 // Register our <Input_Handler> to handle STDIN events, which will
142 // trigger the <handle_input> method to process these events.
144 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
146 if (ACE_Event_Handler::register_stdin_handler
148 orb
->orb_core ()->reactor (),
149 orb
->orb_core ()->thr_mgr ()) == -1)
150 ACE_ERROR_RETURN ((LM_ERROR
,
152 "register_stdin_handler"),
155 // Stash our ORB pointer for later reference.
156 this->notifier_i_
.orb (orb
.in ());
158 // Activate the servant in the POA.
159 CORBA::String_var str
=
160 this->orb_manager_
.activate_under_child_poa ("Notifier",
163 ACE_DEBUG ((LM_DEBUG
,
164 "The IOR is: <%s>\n",
167 if (this->ior_output_file_
)
169 ACE_OS::fprintf (this->ior_output_file_
,
172 ACE_OS::fclose (this->ior_output_file_
);
175 if (this->using_naming_service_
)
177 this->init_naming_service ();
183 Notifier_Input_Handler::run ()
185 // Run the main event loop for the ORB.
186 ACE_DEBUG ((LM_DEBUG
, " Type \"q\" to quit \n"));
188 int const result
= this->orb_manager_
.run ();
192 ACE_ERROR_RETURN ((LM_ERROR
, "Notifier_Input_Handler::run"), -1);
199 Notifier_Input_Handler::handle_input (ACE_HANDLE
)
205 // The string could read contains \n\0 hence using ACE_OS::read
206 // which returns the no of bytes read and hence i can manipulate
207 // and remove the devil from the picture i.e '\n' ! ;)
208 ssize_t
const strlen
= ACE_OS::read (ACE_STDIN
, buf
, sizeof buf
);
209 if (buf
[strlen
-1] == '\n')
210 buf
[strlen
-1] = '\0';
212 ACE_DEBUG ((LM_DEBUG
,
216 if (ACE_OS::ace_tolower(buf
[0]) == 'q')
218 // @@ Please remove this call if it's not used.
219 // (this->notifier_i_.consumer_map_).close();
220 this->notifier_i_
.shutdown ();
223 catch (const CORBA::Exception
& ex
)
225 ex
._tao_print_exception ("Input_Handler::init");