Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / docs / tutorials / Quoter / RTCORBA / Distributor.cpp
bloba6c5d8b6b964b95f72d575ea1863a88aff0d82e0
1 // ACE header files
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "orbsvcs/CosNamingC.h"
6 // STL strings
7 #include <string>
9 // local header files
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.
20 CORBA::Long rate = 1;
22 u_int update_freq = 1;
24 // A flag that indicates use of the Naming Service.
25 bool use_naming = false;
27 static int
28 parse_args (int argc, ACE_TCHAR *argv[])
30 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:r:u:c"));
31 int c;
33 while ((c = get_opts ()) != -1)
35 switch (c)
37 case 'o':
38 ior = get_opts.opt_arg ();
39 break;
40 case 'r':
41 rate = ACE_OS::atoi (get_opts.opt_arg ());
42 break;
43 case 'u':
44 update_freq = ACE_OS::atoi (get_opts.opt_arg ());
45 case 'c':
46 use_naming = true;
47 break;
49 case '?':
50 default:
51 ACE_ERROR_RETURN ((LM_ERROR,
52 "usage: %s\n"
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"
57 "\n",
58 argv [0]),
59 -1);
63 return 0;
66 static int
67 set_distributor_reference (CORBA::ORB_ptr orb,
68 CORBA::Object_ptr obj)
70 if (use_naming)
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);
81 name.length (1);
83 name[0].id = distributor_name.c_str ();
85 // Register the servant with the Naming Service
86 try
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);
98 else
100 // Write the object reference for the <stock_distributor> to a
101 // file so the <StockBroker> can read it when it's
102 // bootstrapping.
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",
108 ior.c_str ()),
109 -1);
110 ACE_OS::fprintf (output_file, "%s", str.in ());
111 ACE_OS::fclose (output_file);
114 return 0;
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)
128 return 1;
130 // Get the RootPOA.
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 ();
136 mgr->activate ();
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",
146 ior.c_str ()),
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)
153 return 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"));
164 orb->run ();
166 // Cleanup the POA and ORB.
167 poa->destroy (1, 1);
168 orb->destroy ();
170 catch (const CORBA::Exception &ex)
172 ex._tao_print_exception ("Admin: ");
174 return 1;
176 return 0;