2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "orbsvcs/CosNamingC.h"
10 #include "Distributor_i.h"
11 #include "Stock_Database.h"
13 // Name of the file that stores the StockDistributor IOR.
14 static std::string ior
= "StockDistributor.ior";
16 // Name of the StockDistributor registered with the Naming Service.
17 static std::string distributor_name
= "StockDistributor";
19 // The default rate, which sends data out once per second.
22 u_int update_freq
= 1;
24 // A flag that indicates use of the Naming Service.
25 bool use_naming
= false;
28 parse_args (int argc
, ACE_TCHAR
*argv
[])
30 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:r:u:c"));
33 while ((c
= get_opts ()) != -1)
38 ior
= get_opts
.opt_arg ();
41 rate
= ACE_OS::atoi (get_opts
.opt_arg ());
44 update_freq
= ACE_OS::atoi (get_opts
.opt_arg ());
51 ACE_ERROR_RETURN ((LM_ERROR
,
53 "-o <Distributor IOR> (default is file://StockDistributor.ior)\n"
54 "-r <Distributor notification rate> (default is 1 second)\n"
55 "-u <Database update rate> (default is 1 second)\n"
56 "-c Flag that indicates that the object should be registered with naming service\n"
67 set_distributor_reference (CORBA::ORB_ptr orb
,
68 CORBA::Object_ptr obj
)
72 // Naming Service related operations
73 CORBA::Object_var naming_context_object
=
74 orb
->resolve_initial_references ("NameService");
76 CosNaming::NamingContext_var naming_context
=
77 CosNaming::NamingContext::_narrow (naming_context_object
.in ());
79 // Initialize the Naming Sequence
80 CosNaming::Name
name (1);
83 name
[0].id
= distributor_name
.c_str ();
85 // Register the servant with the Naming Service
88 // Register the servant with the Naming Service
89 naming_context
->bind (name
, obj
);
91 catch (CosNaming::NamingContext::AlreadyBound
&)
93 ACE_DEBUG ((LM_DEBUG
, "%s already bound, rebinding....\n",
94 distributor_name
.c_str ()));
95 naming_context
->rebind (name
, obj
);
100 // Write the object reference for the <stock_distributor> to a
101 // file so the <StockBroker> can read it when it's
103 CORBA::String_var str
= orb
->object_to_string (obj
);
104 FILE *output_file
= ACE_OS::fopen (ior
.c_str (), "w");
105 if (output_file
== 0)
106 ACE_ERROR_RETURN ((LM_ERROR
,
107 "Cannot open output file for writing IOR: %s\n",
110 ACE_OS::fprintf (output_file
, "%s", str
.in ());
111 ACE_OS::fclose (output_file
);
118 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
122 // Initalize the ORB.
123 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
125 // This call MUST come after ORB_init(), which may need to
126 // extract -ORB options first.
127 if (parse_args (argc
, argv
) != 0)
131 CORBA::Object_var obj
= orb
->resolve_initial_references ("RootPOA");
132 PortableServer::POA_var poa
= PortableServer::POA::_narrow (obj
.in ());
134 // Activate the POAManager.
135 PortableServer::POAManager_var mgr
= poa
->the_POAManager ();
138 // Create the factory object. Create a <Stock::StockDistributor>.
139 StockDistributorHome_i
stock_distributor_home (orb
.in ());
141 Stock::StockDistributor_var stock_distributor
= stock_distributor_home
.create ();
143 if (CORBA::is_nil (stock_distributor
.in ()))
144 ACE_ERROR_RETURN ((LM_DEBUG
,
145 "Nil StockDistributor object reference <%s>\n",
149 // Store the distributor reference in a location that's
150 // accessible by the client.
151 if (set_distributor_reference (orb
.in (),
152 stock_distributor
.in ()) == -1)
155 // Set the initial notification rate.
156 stock_distributor
->notification_rate (rate
);
158 // Set the database update rate
159 STOCK_DATABASE
->update_rate (update_freq
);
161 // Enter into the event looping.
162 ACE_DEBUG ((LM_DEBUG
,
163 "*** message: ready for transmission...\n"));
166 // Cleanup the POA and ORB.
170 catch (const CORBA::Exception
&ex
)
172 ex
._tao_print_exception ("Admin: ");