Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Notifier_Input_Handler.cpp
blobd88b3ad1849e9e7af83b663494109b94a72dd448
1 //=============================================================================
2 /**
3 * @file Notifier_Input_Handler.cpp
5 * Implementation of the callback quoter Notifier_Input_Handler class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
8 */
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"
18 // Constructor.
19 Notifier_Input_Handler::Notifier_Input_Handler ()
20 : ior_output_file_ (0),
21 argc_ (0),
22 argv_ (0),
23 using_naming_service_ (1)
27 // Destructor.
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)
34 ACE_ERROR ((LM_ERROR,
35 "%p\n",
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.
41 int
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)
47 return -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.
55 try
57 Notifier_var notifier_obj = notifier_i_._this ();
59 this->orb_manager_.activate_poa_manager ();
61 naming_server_->rebind (notifier_obj_name,
62 notifier_obj.in());
64 catch (const CosNaming::NamingContext::AlreadyBound&)
66 ACE_ERROR_RETURN ((LM_ERROR,
67 "Unable to bind %s\n",
68 "Notifier"),
69 -1);
72 return 0;
75 // Parse the command-line arguments and set options.
76 int
77 Notifier_Input_Handler::parse_args ()
79 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("df:s "));
80 int c;
82 while ((c = get_opts ()) != -1)
83 switch (c)
85 case 'd': // debug flag.
86 TAO_debug_level++; ///*****
87 break;
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",
94 get_opts.opt_arg ()),
95 -1);
96 break;
98 case 's': // don't use the naming service
99 this->using_naming_service_ = 0;
100 break;
102 case '?': // display help for use of the server.
103 default:
104 ACE_ERROR_RETURN ((LM_ERROR,
105 "usage: %s"
106 " [-d]"
107 " [-f] <ior_output_file>"
108 " [-s]"
109 "\n",
110 argv_ [0]),
114 // Indicates successful parsing of command line.
115 return 0;
118 // Initialize the server.
120 Notifier_Input_Handler::init (int argc,
121 ACE_TCHAR *argv[])
123 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
124 // create the child poa under the root POA.
125 this->argc_ = argc;
126 this->argv_ = argv;
128 if (this->orb_manager_.init_child_poa (this->argc_,
129 this->argv_,
130 "child_poa") == -1)
131 ACE_ERROR_RETURN ((LM_ERROR,
132 "%p\n",
133 "init_child_poa"),
134 -1);
136 int retval = this->parse_args ();
138 if (retval != 0)
139 return retval;
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
147 (this,
148 orb->orb_core ()->reactor (),
149 orb->orb_core ()->thr_mgr ()) == -1)
150 ACE_ERROR_RETURN ((LM_ERROR,
151 "%p\n",
152 "register_stdin_handler"),
153 -1);
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",
161 &this->notifier_i_);
163 ACE_DEBUG ((LM_DEBUG,
164 "The IOR is: <%s>\n",
165 str.in ()));
167 if (this->ior_output_file_)
169 ACE_OS::fprintf (this->ior_output_file_,
170 "%s",
171 str.in ());
172 ACE_OS::fclose (this->ior_output_file_);
175 if (this->using_naming_service_)
177 this->init_naming_service ();
179 return 0;
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 ();
190 if (result == -1)
192 ACE_ERROR_RETURN ((LM_ERROR, "Notifier_Input_Handler::run"), -1);
195 return 0;
199 Notifier_Input_Handler::handle_input (ACE_HANDLE)
203 char buf[BUFSIZ];
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,
213 "%C",
214 buf));
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");
226 return -1;
229 return 0;