Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / DevGuideExamples / Multithreading / GracefulShutdown / MessengerServer.cpp
blob4bef4422ce4c3fe469a908784272f9268b380534
1 #include "MessengerServer.h"
2 #include "Messenger_i.h"
3 #include "MessengerShutdownTimer.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Reactor.h"
6 #include "tao/ORB_Core.h"
7 #include <iostream>
8 #include <fstream>
10 ACE_TString ior_output_file = ACE_TEXT ("test.ior");
12 // By default, shutdown when client calls Messenger::shutdown().
13 MessengerServer::ShutdownMethod s_method = MessengerServer::s_client_call;
15 int loop_iterations;
16 int timeout;
18 // Constructor.
19 MessengerServer::MessengerServer (CORBA::ORB_ptr orb)
20 : orb_(CORBA::ORB::_duplicate(orb))
21 , monitor_(0)
25 // Destructor.
26 MessengerServer::~MessengerServer ()
28 if (monitor_ != 0)
29 delete monitor_;
30 orb_->destroy ();
33 // run the ORB's event loop continuously
34 void MessengerServer::run ()
36 std::cout << "Running the ORB event loop continuously..." << std::endl;
37 orb_->run ();
38 std::cout << "Finished running the ORB." << std::endl;
41 // run the ORB's event loop for some number of seconds
42 void MessengerServer::run (int seconds)
44 std::cout << "Running the ORB event loop for " << seconds
45 << " seconds..." << std::endl;
46 ACE_Time_Value tv ((long)seconds, 0);
47 orb_->run (tv);
48 std::cout << "Finished running the ORB." << std::endl;
51 // handle ORB events in a polling loop for some number of iterations
52 void MessengerServer::poll (int iterations)
54 std::cout << "Polling for ORB events for " << iterations
55 << " iterations..." << std::endl;
56 int x = iterations;
59 std::cout << "iteration: " << iterations - x << std::endl;
60 ACE_Time_Value tv (1,0);
61 orb_->perform_work (tv);
63 while ((--x > 0) && (orb_->orb_core ()->has_shutdown() == 0));
64 std::cout << "Finished running the ORB." << std::endl;
67 // schedule a shutdown timer with the ORB's reactor to timeout
68 // after some seconds
69 void MessengerServer::schedule_shutdown_timer (int seconds)
71 // Create a shutdown timer.
72 std::cout << "Creating shutdown timer" << std::endl;
73 MessengerShutdownTimer * timer = new MessengerShutdownTimer (orb_.in());
75 // Schedule the timer to shutdown the ORB, with no repeat.
76 ACE_Time_Value tv ((long)seconds, 0);
77 std::cout << "Scheduling shutdown timer" << std::endl;
78 orb_->orb_core ()->reactor ()->schedule_timer (
79 timer, // handler : ACE_Event_Handler *
80 0, // args : void *
81 tv // relative timeout value : ACE_Time_Value &
85 // spawn thread to monitor console and shutdown on console input
86 void MessengerServer::shutdown_on_console_input ()
88 // Spawn a thread to monitor console and shutdown on console input.
89 monitor_ = new MessengerServer::ConsoleMonitor (orb_.in());
90 monitor_->activate ();
91 std::cout << "Enter any input on keyboard to shut down ORB..." << std::endl;
94 // parse arguments
95 int MessengerServer::parse_args (int argc, ACE_TCHAR* argv[])
97 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xp:t:cr:o:"));
98 int c;
100 while ((c = get_opts ()) != -1)
102 switch (c)
104 case 'x':
105 s_method = MessengerServer::s_client_call;
106 break;
107 case 'p':
108 s_method = MessengerServer::s_polling_loop;
109 loop_iterations = ACE_OS::atoi(get_opts.opt_arg());
110 break;
111 case 't':
112 s_method = MessengerServer::s_timer;
113 timeout = ACE_OS::atoi(get_opts.opt_arg());
114 break;
115 case 'c':
116 s_method = MessengerServer::s_console_input;
117 break;
118 case 'o':
119 ior_output_file = get_opts.optarg;
120 break;
121 case 'r':
122 s_method = MessengerServer::s_run_time_value;
123 timeout = ACE_OS::atoi(get_opts.opt_arg());
124 break;
125 case '?':
126 default:
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "usage: %s\n"
129 "-x (default) - shutdown on client invocation\n"
130 "-p <n> - use polling loop for <n> iterations\n"
131 "-t <n> - schedule timer for <n> seconds\n"
132 "-o <iorfile>\n"
133 "-c - shutdown on console input\n"
134 "-r <n> - run ORB for <n> seconds\n",
135 argv[0]),
136 -1);
139 return 0;
143 // ------------------------------------------------------------------
144 // main
145 // ------------------------------------------------------------------
147 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
149 try {
150 // Initialize the ORB.
151 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
153 // Create a MessengerServer object.
154 MessengerServer * server = new MessengerServer (orb.in());
155 std::unique_ptr<MessengerServer> safe_ptr (server);
157 // Parse arguments to determine how we should shutdown.
158 if (server->parse_args (argc, argv) != 0)
159 return 1;
161 //Get reference to the RootPOA.
162 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
163 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
165 // Activate the POAManager.
166 PortableServer::POAManager_var mgr = poa->the_POAManager();
167 mgr->activate();
169 // Create a servant.
170 Messenger_i messenger_servant (orb.in());
172 // Register the servant with the RootPOA, obtain its object
173 // reference, stringify it, and write it to a file.
174 PortableServer::ObjectId_var oid =
175 poa->activate_object( &messenger_servant );
176 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
177 CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
178 std::ofstream iorFile(ACE_TEXT_ALWAYS_CHAR (ior_output_file.c_str ()));
179 iorFile << str.in() << std::endl;
180 iorFile.close();
181 std::cout << "IOR written to file " <<
182 ACE_TEXT_ALWAYS_CHAR (ior_output_file.c_str ()) << std::endl;
184 switch (s_method)
186 // shutdown on client invocation
187 case MessengerServer::s_client_call:
188 std::cout << "Will shutdown on client invocation." << std::endl;
189 server->run ();
190 break;
192 // shutdown after some iterations through loop
193 case MessengerServer::s_polling_loop:
194 server->poll (loop_iterations);
195 break;
197 // schedule a timer to shutdown
198 case MessengerServer::s_timer:
199 server->schedule_shutdown_timer (timeout);
200 server->run ();
201 break;
203 // shutdown on console input
204 case MessengerServer::s_console_input:
205 server->shutdown_on_console_input ();
206 server->run ();
207 break;
209 // use CORBA::ORB::run() with time value
210 case MessengerServer::s_run_time_value:
211 server->run (timeout);
212 break;
215 catch(const CORBA::Exception& ex) {
216 std::cerr << "CORBA exception: " << ex << std::endl;
217 return 1;
220 return 0;