1 //=============================================================================
5 * Implementation of the Supplier class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
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"
24 Supplier::Supplier (void)
26 use_naming_service_ (1),
35 Supplier::~Supplier (void)
37 // Release the memory allocated for ior_.
38 ACE_OS::free (this->ior_
);
41 ACE_OS::fclose (f_ptr_
);
44 "Market Status Supplier daemon exiting!\n"));
47 // Reads the Server factory IOR from a file.
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",
61 ACE_Read_Buffer
ior_buffer (f_handle
);
62 char *data
= ior_buffer
.read ();
65 ACE_ERROR_RETURN ((LM_ERROR
,
66 "Unable to read ior\n"),
69 this->ior_
= ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data
));
70 ior_buffer
.alloc ()->free (data
);
72 ACE_OS::close (f_handle
);
77 // Parses the command line arguments and returns an error status.
80 Supplier::parse_args (void)
82 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("dn:f:i:xk:xs"));
87 while ((c
= get_opts ()) != -1)
90 case 'd': // Debug flag
91 TAO_debug_level
++; //****
94 case 'n': // Period_value: time between two successive stockfeeds.
95 this->period_value_
= ACE_OS::atoi (get_opts
.opt_arg ());
98 case 'i': // Stock market information is got from a file.
99 result
= this->read_file (get_opts
.opt_arg ());
101 ACE_ERROR_RETURN ((LM_ERROR
,
102 "Unable to read stock information from %s : %p\n",
103 get_opts
.opt_arg ()),
107 case 'k': // Ior provide on command line
108 this->ior_
= ACE_OS::strdup (get_opts
.opt_arg ());
111 case 'f': // Read the IOR from the file.
112 result
= this->read_ior (get_opts
.opt_arg ());
114 ACE_ERROR_RETURN ((LM_ERROR
,
115 "Unable to read ior from %s : %p\n",
116 get_opts
.opt_arg ()),
120 case 's': // Don't use the naming service
121 this->use_naming_service_
= 0;
126 ACE_ERROR_RETURN ((LM_ERROR
,
131 " [-i input_filename]"
140 // Indicates successful parsing of command line.
144 // Give the stock status information to the Notifier.
147 Supplier::send_market_status (const char *stock_name
,
155 this->notifier_
->market_status (stock_name
,
158 catch (const CORBA::SystemException
& sysex
)
160 sysex
._tao_print_exception (
161 "System Exception : Supplier::send_market_status");
164 catch (const CORBA::UserException
& userex
)
166 userex
._tao_print_exception (
167 "User Exception : Supplier::send_market_status");
173 // Execute client example code.
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",
193 ACE_ERROR_RETURN ((LM_ERROR
,
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"),
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.
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");
233 catch (const CORBA::UserException
& userex
)
235 userex
._tao_print_exception (
236 "User Exception : Supplier::via_naming_service\n");
246 Supplier::init (int argc
, ACE_TCHAR
**argv
)
254 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
256 // Parse command line and verify parameters.
257 if (this->parse_args () == -1)
260 // Create the Timer_Handler.
261 ACE_NEW_RETURN (supplier_timer_handler_
,
262 Supplier_Timer_Handler (this,
263 this->reactor_used (),
267 if (this->use_naming_service_
)
268 return via_naming_service ();
271 ACE_ERROR_RETURN ((LM_ERROR
,
272 "%s: no ior specified\n",
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",
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");
292 catch (const CORBA::UserException
& userex
)
294 userex
._tao_print_exception ("User Exception : Supplier::init");
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
,
318 // the stock values are to be read from a file.
320 ACE_ERROR_RETURN ((LM_ERROR
,
321 "Unable to open %s for writing: %p\n",