Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / docs / tutorials / Quoter / RTCORBA / Admin.cpp
blobda0108f727c3e2b0cafa14e6ebd4609bfcbc5d03
1 // ACE header files
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "orbsvcs/CosNamingC.h"
5 // STL strings
6 #include <string>
8 // local header files
9 #include "DistributorC.h"
11 enum Option_Types
13 START,
14 STOP,
15 RATE
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.
35 static int
36 parse_args (int argc, ACE_TCHAR *argv[])
38 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:r:bsc"));
39 int c;
41 while ((c = get_opts ()) != -1)
43 switch (c)
45 case 'o':
46 ior = get_opts.opt_arg ();
47 break;
48 case 'r':
49 option = RATE;
50 rate = get_opts.opt_arg ();
51 break;
52 case 'b':
53 option = START;
54 break;
55 case 's':
56 option = STOP;
57 break;
58 case 'c':
59 use_naming = get_opts.opt_arg ();
60 break;
62 case '?':
63 default:
64 ACE_ERROR_RETURN ((LM_ERROR,
65 "usage: %s\n"
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"
70 "\n",
71 argv [0]),
72 -1);
76 return 0;
79 static CORBA::Object_ptr
80 get_distributor_reference (CORBA::ORB_ptr orb)
82 if (use_naming)
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);
91 name.length (1);
92 name[0].id = distributor_name.c_str ();
94 return pns->resolve (name);
96 else
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)
112 return 1;
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",
123 ior.c_str ()),
126 if (option == STOP)
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",
136 notify_rate));
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!
146 // Cleanup the ORB.
147 orb->destroy ();
149 catch (const CORBA::Exception &ex)
151 ex._tao_print_exception ("Exception caught:");
152 return 1;
155 return 0;