1 //=============================================================================
5 * Implementation of the Supplier class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
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"
24 use_naming_service_ (1),
32 Supplier::~Supplier ()
34 // Release the memory allocated for ior_.
35 ACE_OS::free (this->ior_
);
38 ACE_OS::fclose (f_ptr_
);
41 "Market Status Supplier daemon exiting!\n"));
44 // Reads the Server factory IOR from a file.
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",
57 ACE_Read_Buffer
ior_buffer (f_handle
);
58 char *data
= ior_buffer
.read ();
61 ACE_ERROR_RETURN ((LM_ERROR
,
62 "Unable to read ior\n"),
65 this->ior_
= ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data
));
66 ior_buffer
.alloc ()->free (data
);
68 ACE_OS::close (f_handle
);
73 // Parses the command line arguments and returns an error status.
75 Supplier::parse_args ()
77 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("dn:f:i:xk:xs"));
82 while ((c
= get_opts ()) != -1)
85 case 'd': // Debug flag
86 TAO_debug_level
++; //****
89 case 'n': // Period_value: time between two successive stockfeeds.
90 this->period_value_
= ACE_OS::atoi (get_opts
.opt_arg ());
93 case 'i': // Stock market information is got from a file.
94 result
= this->read_file (get_opts
.opt_arg ());
96 ACE_ERROR_RETURN ((LM_ERROR
,
97 "Unable to read stock information from %s : %p\n",
102 case 'k': // Ior provide on command line
103 this->ior_
= ACE_OS::strdup (get_opts
.opt_arg ());
106 case 'f': // Read the IOR from the file.
107 result
= this->read_ior (get_opts
.opt_arg ());
109 ACE_ERROR_RETURN ((LM_ERROR
,
110 "Unable to read ior from %s : %p\n",
111 get_opts
.opt_arg ()),
115 case 's': // Don't use the naming service
116 this->use_naming_service_
= 0;
121 ACE_ERROR_RETURN ((LM_ERROR
,
126 " [-i input_filename]"
135 // Indicates successful parsing of command line.
139 // Give the stock status information to the Notifier.
141 Supplier::send_market_status (const char *stock_name
, long value
)
146 this->notifier_
->market_status (stock_name
,
149 catch (const CORBA::SystemException
& sysex
)
151 sysex
._tao_print_exception (
152 "System Exception : Supplier::send_market_status");
155 catch (const CORBA::UserException
& userex
)
157 userex
._tao_print_exception (
158 "User Exception : Supplier::send_market_status");
164 // Execute client example code.
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",
182 ACE_ERROR_RETURN ((LM_ERROR
,
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"),
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.
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");
220 catch (const CORBA::UserException
& userex
)
222 userex
._tao_print_exception (
223 "User Exception : Supplier::via_naming_service\n");
232 Supplier::init (int argc
, ACE_TCHAR
**argv
)
240 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
242 // Parse command line and verify parameters.
243 if (this->parse_args () == -1)
246 // Create the Timer_Handler.
247 ACE_NEW_RETURN (supplier_timer_handler_
,
248 Supplier_Timer_Handler (this,
249 this->reactor_used (),
253 if (this->use_naming_service_
)
254 return via_naming_service ();
257 ACE_ERROR_RETURN ((LM_ERROR
,
258 "%s: no ior specified\n",
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",
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");
278 catch (const CORBA::UserException
& userex
)
280 userex
._tao_print_exception ("User Exception : Supplier::init");
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
,
303 // the stock values are to be read from a file.
305 ACE_ERROR_RETURN ((LM_ERROR
,
306 "Unable to open %s for writing: %p\n",