Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Notifier_Input_Handler.cpp
blob112232beb13f11425208c7d63b965481bad9e581
2 //=============================================================================
3 /**
4 * @file Notifier_Input_Handler.cpp
6 * Implementation of the callback quoter Notifier_Input_Handler class.
8 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 */
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"
20 // Constructor.
22 Notifier_Input_Handler::Notifier_Input_Handler (void)
23 : ior_output_file_ (0),
24 argc_ (0),
25 argv_ (0),
26 using_naming_service_ (1)
30 // Destructor.
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)
39 ACE_ERROR ((LM_ERROR,
40 "%p\n",
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.
48 int
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)
55 return -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.
63 try
65 Notifier_var notifier_obj = notifier_i_._this ();
67 this->orb_manager_.activate_poa_manager ();
69 naming_server_->rebind (notifier_obj_name,
70 notifier_obj.in());
73 catch (const CosNaming::NamingContext::AlreadyBound&)
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "Unable to bind %s\n",
77 "Notifier"),
78 -1);
81 return 0;
84 // Parse the command-line arguments and set options.
85 int
86 Notifier_Input_Handler::parse_args (void)
88 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("df:s "));
89 int c;
91 while ((c = get_opts ()) != -1)
92 switch (c)
94 case 'd': // debug flag.
95 TAO_debug_level++; ///*****
96 break;
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 ()),
104 -1);
105 break;
107 case 's': // don't use the naming service
108 this->using_naming_service_ = 0;
109 break;
111 case '?': // display help for use of the server.
112 default:
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "usage: %s"
115 " [-d]"
116 " [-f] <ior_output_file>"
117 " [-s]"
118 "\n",
119 argv_ [0]),
123 // Indicates successful parsing of command line.
124 return 0;
127 // Initialize the server.
130 Notifier_Input_Handler::init (int argc,
131 ACE_TCHAR *argv[])
134 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
135 // create the child poa under the root POA.
137 this->argc_ = argc;
138 this->argv_ = argv;
140 if (this->orb_manager_.init_child_poa (this->argc_,
141 this->argv_,
142 "child_poa") == -1)
143 ACE_ERROR_RETURN ((LM_ERROR,
144 "%p\n",
145 "init_child_poa"),
146 -1);
148 int retval = this->parse_args ();
150 if (retval != 0)
151 return retval;
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
159 (this,
160 orb->orb_core ()->reactor (),
161 orb->orb_core ()->thr_mgr ()) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR,
163 "%p\n",
164 "register_stdin_handler"),
165 -1);
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",
173 &this->notifier_i_);
175 ACE_DEBUG ((LM_DEBUG,
176 "The IOR is: <%s>\n",
177 str.in ()));
179 if (this->ior_output_file_)
181 ACE_OS::fprintf (this->ior_output_file_,
182 "%s",
183 str.in ());
184 ACE_OS::fclose (this->ior_output_file_);
187 if (this->using_naming_service_)
189 this->init_naming_service ();
191 return 0;
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 ();
205 if (result == -1)
207 ACE_ERROR_RETURN ((LM_ERROR,
208 "Notifier_Input_Handler::run"),
209 -1);
212 return 0;
216 Notifier_Input_Handler::handle_input (ACE_HANDLE)
218 char buf[BUFSIZ];
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,
228 buf,
229 sizeof buf);
230 if (buf[strlen -1] == '\n')
231 buf[strlen -1] = '\0';
233 ACE_DEBUG ((LM_DEBUG,
234 "%s",
235 buf));
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");
247 return -1;
250 return 0;