Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Supplier_i.cpp
blobfb6ed593ca44b3a7305939d1649abc2bc60dec57
1 //=============================================================================
2 /**
3 * @file Supplier_i.cpp
5 * Implementation of the Supplier class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
8 */
9 //=============================================================================
12 #include "Supplier_i.h"
13 #include "tao/debug.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/Read_Buffer.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/OS_NS_unistd.h"
18 #include "ace/Reactor.h"
19 #include "ace/OS_NS_string.h"
20 #include "ace/OS_NS_fcntl.h"
22 // Constructor.
24 Supplier::Supplier (void)
25 : ior_ (0),
26 use_naming_service_ (1),
27 notifier_ (),
28 f_ptr_ (0),
29 loop_count_ (10),
30 period_value_ (1)
32 // No-op.
35 Supplier::~Supplier (void)
37 // Release the memory allocated for ior_.
38 ACE_OS::free (this->ior_);
40 // Close the stream.
41 ACE_OS::fclose (f_ptr_);
43 ACE_DEBUG ((LM_DEBUG,
44 "Market Status Supplier daemon exiting!\n"));
47 // Reads the Server factory IOR from a file.
49 int
50 Supplier::read_ior (ACE_TCHAR *filename)
52 // Open the file for reading.
53 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
55 if (f_handle == ACE_INVALID_HANDLE)
56 ACE_ERROR_RETURN ((LM_ERROR,
57 "Unable to open %s for reading\n",
58 filename),
59 -1);
61 ACE_Read_Buffer ior_buffer (f_handle);
62 char *data = ior_buffer.read ();
64 if (data == 0)
65 ACE_ERROR_RETURN ((LM_ERROR,
66 "Unable to read ior\n"),
67 -1);
69 this->ior_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
70 ior_buffer.alloc ()->free (data);
72 ACE_OS::close (f_handle);
74 return 0;
77 // Parses the command line arguments and returns an error status.
79 int
80 Supplier::parse_args (void)
82 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dn:f:i:xk:xs"));
84 int c;
85 int result;
87 while ((c = get_opts ()) != -1)
88 switch (c)
90 case 'd': // Debug flag
91 TAO_debug_level++; //****
92 break;
94 case 'n': // Period_value: time between two successive stockfeeds.
95 this->period_value_ = ACE_OS::atoi (get_opts.opt_arg ());
96 break;
98 case 'i': // Stock market information is got from a file.
99 result = this->read_file (get_opts.opt_arg ());
100 if (result < 0)
101 ACE_ERROR_RETURN ((LM_ERROR,
102 "Unable to read stock information from %s : %p\n",
103 get_opts.opt_arg ()),
104 -1);
105 break;
107 case 'k': // Ior provide on command line
108 this->ior_ = ACE_OS::strdup (get_opts.opt_arg ());
109 break;
111 case 'f': // Read the IOR from the file.
112 result = this->read_ior (get_opts.opt_arg ());
113 if (result < 0)
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "Unable to read ior from %s : %p\n",
116 get_opts.opt_arg ()),
117 -1);
118 break;
120 case 's': // Don't use the naming service
121 this->use_naming_service_ = 0;
122 break;
124 case '?':
125 default:
126 ACE_ERROR_RETURN ((LM_ERROR,
127 "usage: %s"
128 " [-d]"
129 " [-n period]"
130 " [-f ior-file]"
131 " [-i input_filename]"
132 " [-k ior]"
133 " [-x]"
134 " [-s]"
135 "\n",
136 this->argv_ [0]),
137 -1);
140 // Indicates successful parsing of command line.
141 return 0;
144 // Give the stock status information to the Notifier.
147 Supplier::send_market_status (const char *stock_name,
148 long value)
154 // Make the RMI.
155 this->notifier_->market_status (stock_name,
156 value);
158 catch (const CORBA::SystemException& sysex)
160 sysex._tao_print_exception (
161 "System Exception : Supplier::send_market_status");
162 return -1;
164 catch (const CORBA::UserException& userex)
166 userex._tao_print_exception (
167 "User Exception : Supplier::send_market_status");
168 return -1;
170 return 0;
173 // Execute client example code.
176 Supplier::run (void)
179 long timer_id = 0;
181 ACE_DEBUG ((LM_DEBUG,
182 "Market Status Supplier Daemon is running...\n"));
184 // This sets the period for the stock-feed.
185 ACE_Time_Value period (period_value_);
187 // "Your time starts now!" ;) the timer is scheduled to begin work.
188 timer_id = reactor_used ()->schedule_timer (supplier_timer_handler_,
189 "Periodic stockfeed",
190 period,
191 period);
192 if (timer_id == -1)
193 ACE_ERROR_RETURN ((LM_ERROR,
194 "%p\n",
195 "schedule_timer"),
196 -1);
198 // The reactor starts executing in a loop.
199 return this->reactor_used ()->run_reactor_event_loop ();
204 Supplier::via_naming_service (void)
209 // Initialization of the naming service.
210 if (naming_services_client_.init (orb_.in ()) != 0)
211 ACE_ERROR_RETURN ((LM_ERROR,
212 " (%P|%t) Unable to initialize "
213 "the TAO_Naming_Client.\n"),
214 -1);
215 CosNaming::Name notifier_ref_name (1);
216 notifier_ref_name.length (1);
217 notifier_ref_name[0].id = CORBA::string_dup ("Notifier");
219 CORBA::Object_var notifier_obj =
220 this->naming_services_client_->resolve (notifier_ref_name);
222 // The CORBA::Object_var object is downcast to Notifier_var
223 // using the <_narrow> method.
224 this->notifier_ =
225 Notifier::_narrow (notifier_obj.in ());
227 catch (const CORBA::SystemException& sysex)
229 sysex._tao_print_exception (
230 "System Exception : Supplier::via_naming_service\n");
231 return -1;
233 catch (const CORBA::UserException& userex)
235 userex._tao_print_exception (
236 "User Exception : Supplier::via_naming_service\n");
237 return -1;
240 return 0;
243 // Init function.
246 Supplier::init (int argc, ACE_TCHAR **argv)
248 this->argc_ = argc;
249 this->argv_ = argv;
253 // Retrieve the ORB.
254 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
256 // Parse command line and verify parameters.
257 if (this->parse_args () == -1)
258 return -1;
260 // Create the Timer_Handler.
261 ACE_NEW_RETURN (supplier_timer_handler_,
262 Supplier_Timer_Handler (this,
263 this->reactor_used (),
264 this->f_ptr_),
265 -1);
267 if (this->use_naming_service_)
268 return via_naming_service ();
270 if (this->ior_ == 0)
271 ACE_ERROR_RETURN ((LM_ERROR,
272 "%s: no ior specified\n",
273 this->argv_[0]),
274 -1);
275 CORBA::Object_var notifier_object =
276 this->orb_->string_to_object (this->ior_);
278 if (CORBA::is_nil (notifier_object.in ()))
279 ACE_ERROR_RETURN ((LM_ERROR,
280 "invalid ior <%s>\n",
281 this->ior_),
282 -1);
283 // The downcasting from CORBA::Object_var to Notifier_var is
284 // done using the <_narrow> method.
285 this->notifier_ = Notifier::_narrow (notifier_object.in ());
287 catch (const CORBA::SystemException& sysex)
289 sysex._tao_print_exception ("System Exception : Supplier::init");
290 return -1;
292 catch (const CORBA::UserException& userex)
294 userex._tao_print_exception ("User Exception : Supplier::init");
295 return -1;
298 return 0;
301 ACE_Reactor*
302 Supplier::reactor_used (void) const
304 return ACE_Reactor::instance ();
307 // The stock market information is read from a file.
310 Supplier::read_file (ACE_TCHAR *filename)
312 f_ptr_ = ACE_OS::fopen (filename, "r");
314 ACE_DEBUG ((LM_DEBUG,
315 "filename = %s\n",
316 filename));
318 // the stock values are to be read from a file.
319 if (f_ptr_ == 0)
320 ACE_ERROR_RETURN ((LM_ERROR,
321 "Unable to open %s for writing: %p\n",
322 filename),
323 -1);
324 return 0;