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"
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
;
19 MessengerServer::MessengerServer (CORBA::ORB_ptr orb
)
20 : orb_(CORBA::ORB::_duplicate(orb
))
26 MessengerServer::~MessengerServer ()
33 // run the ORB's event loop continuously
34 void MessengerServer::run ()
36 std::cout
<< "Running the ORB event loop continuously..." << std::endl
;
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);
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
;
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
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 *
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
;
95 int MessengerServer::parse_args (int argc
, ACE_TCHAR
* argv
[])
97 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("xp:t:cr:o:"));
100 while ((c
= get_opts ()) != -1)
105 s_method
= MessengerServer::s_client_call
;
108 s_method
= MessengerServer::s_polling_loop
;
109 loop_iterations
= ACE_OS::atoi(get_opts
.opt_arg());
112 s_method
= MessengerServer::s_timer
;
113 timeout
= ACE_OS::atoi(get_opts
.opt_arg());
116 s_method
= MessengerServer::s_console_input
;
119 ior_output_file
= get_opts
.optarg
;
122 s_method
= MessengerServer::s_run_time_value
;
123 timeout
= ACE_OS::atoi(get_opts
.opt_arg());
127 ACE_ERROR_RETURN ((LM_ERROR
,
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"
133 "-c - shutdown on console input\n"
134 "-r <n> - run ORB for <n> seconds\n",
143 // ------------------------------------------------------------------
145 // ------------------------------------------------------------------
147 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
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)
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();
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
;
181 std::cout
<< "IOR written to file " <<
182 ACE_TEXT_ALWAYS_CHAR (ior_output_file
.c_str ()) << std::endl
;
186 // shutdown on client invocation
187 case MessengerServer::s_client_call
:
188 std::cout
<< "Will shutdown on client invocation." << std::endl
;
192 // shutdown after some iterations through loop
193 case MessengerServer::s_polling_loop
:
194 server
->poll (loop_iterations
);
197 // schedule a timer to shutdown
198 case MessengerServer::s_timer
:
199 server
->schedule_shutdown_timer (timeout
);
203 // shutdown on console input
204 case MessengerServer::s_console_input
:
205 server
->shutdown_on_console_input ();
209 // use CORBA::ORB::run() with time value
210 case MessengerServer::s_run_time_value
:
211 server
->run (timeout
);
215 catch(const CORBA::Exception
& ex
) {
216 std::cerr
<< "CORBA exception: " << ex
<< std::endl
;