2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "orbsvcs/CosNamingC.h"
9 #include "DistributorC.h"
18 // Name of the file that stores the StockDistributor IOR.
19 static std::string ior
= "file://StockDistributor.ior";
21 // Name of the StockDistributor registered with the Naming Service.
22 static std::string distributor_name
= "StockDistributor";
24 // The default command, which starts the StockDistributor.
25 static Option_Types option
= START
;
27 // The default rate, which sends data out once per second.
28 static std::string rate
= "1";
30 // A flag that indicates use of the Naming Service.
31 static bool use_naming
= false;
33 // Parse the command-line arguments and set the global options.
36 parse_args (int argc
, ACE_TCHAR
*argv
[])
38 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:r:bsc"));
41 while ((c
= get_opts ()) != -1)
46 ior
= get_opts
.opt_arg ();
50 rate
= get_opts
.opt_arg ();
59 use_naming
= get_opts
.opt_arg ();
64 ACE_ERROR_RETURN ((LM_ERROR
,
66 "-o <Distributor IOR> (the ior file of stock distributor (default is file://StockDistributor.ior))\n"
67 "-r <rate> (set the distribution rate to 'n' seconds (default is 1))\n"
68 "-b (start the stock distributor)\n"
69 "-s (stop the stock distributor)\n"
79 static CORBA::Object_ptr
80 get_distributor_reference (CORBA::ORB_ptr orb
)
84 CORBA::Object_var tmp
=
85 orb
->resolve_initial_references ("NameService");
87 CosNaming::NamingContext_var pns
=
88 CosNaming::NamingContext::_narrow (tmp
.in ());
90 CosNaming::Name
name (1);
92 name
[0].id
= distributor_name
.c_str ();
94 return pns
->resolve (name
);
97 // Read and destringify the Stock_Distributor object's IOR.
98 return orb
->string_to_object (ior
.c_str ());
102 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
106 // Initialiaze the ORB.
107 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
109 // This call MUST come after ORB_init(), which may need to
110 // extract -ORB options first.
111 if (parse_args (argc
, argv
) != 0)
114 CORBA::Object_var obj
= get_distributor_reference (orb
.in ());
116 // Narrow the IOR to a Stock_Distributor object reference.
117 Stock::StockDistributor_var stock_distributor
=
118 Stock::StockDistributor::_narrow(obj
.in ());
120 if (CORBA::is_nil (stock_distributor
.in ()))
121 ACE_ERROR_RETURN ((LM_DEBUG
,
122 "Nil StockDistributor object reference <%s>\n",
128 ACE_DEBUG ((LM_DEBUG
, "Stop the stock distributor.\n"));
129 stock_distributor
->stop ();
131 else if (option
== RATE
)
133 CORBA::Long notify_rate
= ACE_OS::atoi (rate
.c_str ());
134 ACE_DEBUG ((LM_DEBUG
,
135 "Set the distribution notification rate to %d seconds.\n",
137 stock_distributor
->notification_rate (notify_rate
);
139 else // option == START
141 ACE_DEBUG ((LM_DEBUG
, "Start the stock distributor.\n"));
142 stock_distributor
->start ();
145 // Since we are a "pure client" we don't need to run the ORB's event loop!
149 catch (const CORBA::Exception
&ex
)
151 ex
._tao_print_exception ("Exception caught:");