Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / examples / Callback_Quoter / Supplier_i.cpp
blob97b4b7ae79f92ef1c2a1f4c547e9efb605d1fdb6
1 //=============================================================================
2 /**
3 * @file Supplier_i.cpp
5 * Implementation of the Supplier class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
8 */
9 //=============================================================================
11 #include "Supplier_i.h"
12 #include "tao/debug.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/Read_Buffer.h"
15 #include "ace/OS_NS_stdio.h"
16 #include "ace/OS_NS_unistd.h"
17 #include "ace/Reactor.h"
18 #include "ace/OS_NS_string.h"
19 #include "ace/OS_NS_fcntl.h"
21 // Constructor.
22 Supplier::Supplier ()
23 : ior_ (0),
24 use_naming_service_ (1),
25 notifier_ (),
26 f_ptr_ (0),
27 loop_count_ (10),
28 period_value_ (1)
32 Supplier::~Supplier ()
34 // Release the memory allocated for ior_.
35 ACE_OS::free (this->ior_);
37 // Close the stream.
38 ACE_OS::fclose (f_ptr_);
40 ACE_DEBUG ((LM_DEBUG,
41 "Market Status Supplier daemon exiting!\n"));
44 // Reads the Server factory IOR from a file.
45 int
46 Supplier::read_ior (ACE_TCHAR *filename)
48 // Open the file for reading.
49 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
51 if (f_handle == ACE_INVALID_HANDLE)
52 ACE_ERROR_RETURN ((LM_ERROR,
53 "Unable to open %s for reading\n",
54 filename),
55 -1);
57 ACE_Read_Buffer ior_buffer (f_handle);
58 char *data = ior_buffer.read ();
60 if (data == 0)
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "Unable to read ior\n"),
63 -1);
65 this->ior_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
66 ior_buffer.alloc ()->free (data);
68 ACE_OS::close (f_handle);
70 return 0;
73 // Parses the command line arguments and returns an error status.
74 int
75 Supplier::parse_args ()
77 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dn:f:i:xk:xs"));
79 int c;
80 int result;
82 while ((c = get_opts ()) != -1)
83 switch (c)
85 case 'd': // Debug flag
86 TAO_debug_level++; //****
87 break;
89 case 'n': // Period_value: time between two successive stockfeeds.
90 this->period_value_ = ACE_OS::atoi (get_opts.opt_arg ());
91 break;
93 case 'i': // Stock market information is got from a file.
94 result = this->read_file (get_opts.opt_arg ());
95 if (result < 0)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 "Unable to read stock information from %s : %p\n",
98 get_opts.opt_arg ()),
99 -1);
100 break;
102 case 'k': // Ior provide on command line
103 this->ior_ = ACE_OS::strdup (get_opts.opt_arg ());
104 break;
106 case 'f': // Read the IOR from the file.
107 result = this->read_ior (get_opts.opt_arg ());
108 if (result < 0)
109 ACE_ERROR_RETURN ((LM_ERROR,
110 "Unable to read ior from %s : %p\n",
111 get_opts.opt_arg ()),
112 -1);
113 break;
115 case 's': // Don't use the naming service
116 this->use_naming_service_ = 0;
117 break;
119 case '?':
120 default:
121 ACE_ERROR_RETURN ((LM_ERROR,
122 "usage: %s"
123 " [-d]"
124 " [-n period]"
125 " [-f ior-file]"
126 " [-i input_filename]"
127 " [-k ior]"
128 " [-x]"
129 " [-s]"
130 "\n",
131 this->argv_ [0]),
132 -1);
135 // Indicates successful parsing of command line.
136 return 0;
139 // Give the stock status information to the Notifier.
141 Supplier::send_market_status (const char *stock_name, long value)
145 // Make the RMI.
146 this->notifier_->market_status (stock_name,
147 value);
149 catch (const CORBA::SystemException& sysex)
151 sysex._tao_print_exception (
152 "System Exception : Supplier::send_market_status");
153 return -1;
155 catch (const CORBA::UserException& userex)
157 userex._tao_print_exception (
158 "User Exception : Supplier::send_market_status");
159 return -1;
161 return 0;
164 // Execute client example code.
166 Supplier::run ()
168 long timer_id = 0;
170 ACE_DEBUG ((LM_DEBUG,
171 "Market Status Supplier Daemon is running...\n"));
173 // This sets the period for the stock-feed.
174 ACE_Time_Value period (period_value_);
176 // "Your time starts now!" ;) the timer is scheduled to begin work.
177 timer_id = reactor_used ()->schedule_timer (supplier_timer_handler_,
178 "Periodic stockfeed",
179 period,
180 period);
181 if (timer_id == -1)
182 ACE_ERROR_RETURN ((LM_ERROR,
183 "%p\n",
184 "schedule_timer"),
185 -1);
187 // The reactor starts executing in a loop.
188 return this->reactor_used ()->run_reactor_event_loop ();
192 Supplier::via_naming_service ()
196 // Initialization of the naming service.
197 if (naming_services_client_.init (orb_.in ()) != 0)
198 ACE_ERROR_RETURN ((LM_ERROR,
199 " (%P|%t) Unable to initialize "
200 "the TAO_Naming_Client.\n"),
201 -1);
202 CosNaming::Name notifier_ref_name (1);
203 notifier_ref_name.length (1);
204 notifier_ref_name[0].id = CORBA::string_dup ("Notifier");
206 CORBA::Object_var notifier_obj =
207 this->naming_services_client_->resolve (notifier_ref_name);
209 // The CORBA::Object_var object is downcast to Notifier_var
210 // using the <_narrow> method.
211 this->notifier_ =
212 Notifier::_narrow (notifier_obj.in ());
214 catch (const CORBA::SystemException& sysex)
216 sysex._tao_print_exception (
217 "System Exception : Supplier::via_naming_service\n");
218 return -1;
220 catch (const CORBA::UserException& userex)
222 userex._tao_print_exception (
223 "User Exception : Supplier::via_naming_service\n");
224 return -1;
227 return 0;
230 // Init function.
232 Supplier::init (int argc, ACE_TCHAR **argv)
234 this->argc_ = argc;
235 this->argv_ = argv;
239 // Retrieve the ORB.
240 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
242 // Parse command line and verify parameters.
243 if (this->parse_args () == -1)
244 return -1;
246 // Create the Timer_Handler.
247 ACE_NEW_RETURN (supplier_timer_handler_,
248 Supplier_Timer_Handler (this,
249 this->reactor_used (),
250 this->f_ptr_),
251 -1);
253 if (this->use_naming_service_)
254 return via_naming_service ();
256 if (this->ior_ == 0)
257 ACE_ERROR_RETURN ((LM_ERROR,
258 "%s: no ior specified\n",
259 this->argv_[0]),
260 -1);
261 CORBA::Object_var notifier_object =
262 this->orb_->string_to_object (this->ior_);
264 if (CORBA::is_nil (notifier_object.in ()))
265 ACE_ERROR_RETURN ((LM_ERROR,
266 "invalid ior <%s>\n",
267 this->ior_),
268 -1);
269 // The downcasting from CORBA::Object_var to Notifier_var is
270 // done using the <_narrow> method.
271 this->notifier_ = Notifier::_narrow (notifier_object.in ());
273 catch (const CORBA::SystemException& sysex)
275 sysex._tao_print_exception ("System Exception : Supplier::init");
276 return -1;
278 catch (const CORBA::UserException& userex)
280 userex._tao_print_exception ("User Exception : Supplier::init");
281 return -1;
284 return 0;
287 ACE_Reactor*
288 Supplier::reactor_used () const
290 return ACE_Reactor::instance ();
293 // The stock market information is read from a file.
295 Supplier::read_file (ACE_TCHAR *filename)
297 f_ptr_ = ACE_OS::fopen (filename, "r");
299 ACE_DEBUG ((LM_DEBUG,
300 "filename = %s\n",
301 filename));
303 // the stock values are to be read from a file.
304 if (f_ptr_ == 0)
305 ACE_ERROR_RETURN ((LM_ERROR,
306 "Unable to open %s for writing: %p\n",
307 filename),
308 -1);
309 return 0;