Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_2319_Regression / server.cpp
blob91cc45715f8b11155826acbed80dd7f28be60233
2 #include "ace/Thread_Manager.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/Get_Opt.h"
7 #include "TestS.h"
8 #include "TestC.h"
10 int num_calls = 10; // total calls client is going to make
12 // This should equal num_calls within 'sleep * num_calls' seconds
13 int calls_received = 0;
15 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
16 const ACE_TCHAR *ior_file = ACE_TEXT("test.ior");
18 /***************************/
19 /*** Servant Declaration ***/
20 /***************************/
22 class ST_AMH_Servant : public virtual POA_Test::AMH_Roundtrip
24 public:
25 ST_AMH_Servant (CORBA::ORB_ptr orb);
27 void test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
28 Test::Timestamp send_time);
30 protected:
31 CORBA::ORB_ptr orb_;
34 /****************************/
35 /**** Server Declaration ****/
36 /****************************/
38 /**
39 Class that performs all 'dirty' initialisation work that is common to
40 all the AMH servers and 'hides' all the common ORB functions.
42 class ST_AMH_Server
44 public:
45 ST_AMH_Server () = default;
46 virtual ~ST_AMH_Server ();
48 /// ORB inititalisation stuff
49 int start_orb_and_poa (const CORBA::ORB_var &_orb);
51 /// register the servant with the poa
52 virtual void register_servant (ST_AMH_Servant *servant);
54 /// orb-perform_work () abstraction
55 virtual void run_event_loop ();
57 public:
58 protected:
59 CORBA::ORB_ptr orb_;
60 PortableServer::POA_var root_poa_;
62 private:
63 /// Write servant IOR to file specified with the '-o' option
64 int write_ior_to_file (CORBA::String_var ior);
67 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 int parse_args (int argc, ACE_TCHAR *argv[])
75 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:o:k:"));
76 int c;
78 while ((c = get_opts ()) != -1)
79 switch (c)
81 case 'n':
82 num_calls = ACE_OS::atoi (get_opts.opt_arg ());
83 break;
84 case 'o':
85 ior_file = get_opts.opt_arg ();
86 break;
87 case 'k':
88 ior = get_opts.opt_arg ();
89 break;
90 default:
91 break;
93 return 0;
96 /***************************/
97 /*** Servant Definition ***/
98 /***************************/
100 // ------------------------------------------------------------------------
102 ST_AMH_Servant::ST_AMH_Servant (CORBA::ORB_ptr orb)
103 : orb_(CORBA::ORB::_duplicate(orb))
107 // ------------------------------------------------------------------------
109 void
110 ST_AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr,
111 Test::Timestamp)
113 ACE_OS::printf("Received Timestamp # %d\n", calls_received);
114 ACE_OS::sleep(1);
115 ++calls_received;
117 // When _tao_rh destructor is called, it shouldn't send anything to
118 // the client as well
121 /*** Server Declaration ***/
123 // ------------------------------------------------------------------------
125 ST_AMH_Server::~ST_AMH_Server ()
129 this->root_poa_->destroy (1, 1);
131 catch (const CORBA::Exception& ex)
133 ex._tao_print_exception ("Exception caught:");
137 // ------------------------------------------------------------------------
139 int ST_AMH_Server::start_orb_and_poa (const CORBA::ORB_var &_orb)
143 this->orb_ = CORBA::ORB::_duplicate(_orb.in ());
145 CORBA::Object_var poa_object =
146 this->orb_->resolve_initial_references("RootPOA");
148 if (CORBA::is_nil (poa_object.in ()))
149 ACE_ERROR_RETURN ((LM_ERROR,
150 " (%P|%t) Unable to initialize the POA.\n"),
153 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
155 PortableServer::POAManager_var poa_manager =
156 this->root_poa_->the_POAManager ();
158 poa_manager->activate ();
160 catch (const CORBA::Exception& ex)
162 ex._tao_print_exception ("Exception caught:");
163 return -1;
166 return 0;
169 // ------------------------------------------------------------------------
171 void ST_AMH_Server::register_servant (ST_AMH_Servant *servant)
175 CORBA::Object_var poa_object =
176 this->orb_->resolve_initial_references("RootPOA");
178 PortableServer::POA_var root_poa =
179 PortableServer::POA::_narrow (poa_object.in ());
181 PortableServer::ObjectId_var id =
182 root_poa->activate_object (servant);
184 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
186 Test::Roundtrip_var roundtrip = Test::Roundtrip::_narrow (object.in ());
188 CORBA::String_var iorstr = this->orb_->object_to_string(roundtrip.in ());
190 (void) this->write_ior_to_file(iorstr);
192 catch (const CORBA::Exception& ex)
194 ex._tao_print_exception ("Exception caught:");
198 // ------------------------------------------------------------------------
200 void ST_AMH_Server::run_event_loop ()
204 ACE_Time_Value period (1, 0);
205 while (1)
207 this->orb_->perform_work (&period);
209 // when all calls from client have been received, exit
210 if (calls_received == num_calls )
211 return;
214 catch (const CORBA::Exception&)
219 // ------------------------------------------------------------------------
222 ST_AMH_Server::write_ior_to_file (CORBA::String_var iorstr)
224 // If the ior_output_file exists, output the ior to it
225 FILE *output_file= ACE_OS::fopen (ior_file, "w");
226 if (output_file == 0)
228 ACE_ERROR ((LM_ERROR,
229 "Cannot open output file for writing IOR: %s",
230 ior_file));
231 return -1;
234 ACE_OS::fprintf (output_file, "%s", iorstr.in ());
235 ACE_OS::fclose (output_file);
236 return 0;
240 // ------------------------------------------------------------------------
242 static ACE_THR_FUNC_RETURN start_server(void* _arg)
244 ST_AMH_Server *amh_server = static_cast<ST_AMH_Server*>(_arg);
245 amh_server->run_event_loop();
246 return 0;
249 // ------------------------------------------------------------------------
251 static ACE_THR_FUNC_RETURN start_client(void* _arg)
253 Test::Roundtrip_var roundtrip(static_cast<Test::Roundtrip_ptr>(_arg));
255 // Do a couple of calls on the server. If the sever is trying to
256 // do something stupid like sending an exception to us, then it
257 // won't be able to handle more than 1 request from us.
258 Test::Timestamp time = 10;
260 for (int i = 0; i < num_calls; i++)
262 roundtrip->test_method(time);
263 ACE_DEBUG ((LM_DEBUG, "Sent call # %d\n", i));
266 return 0;
270 // ------------------------------------------------------------------------
272 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
274 if (parse_args (argc, argv) != 0)
275 return 1;
277 ST_AMH_Server amh_server;
278 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
280 amh_server.start_orb_and_poa(orb);
282 ST_AMH_Servant servant(orb.in());
284 amh_server.register_servant(&servant);
286 CORBA::Object_var object = orb->string_to_object(ior);
288 Test::Roundtrip_var roundtrip = Test::Roundtrip::_narrow(object.in ());
290 if (CORBA::is_nil(roundtrip.in()))
292 ACE_ERROR_RETURN ((LM_ERROR,
293 "Nil Test::Roundtrip reference <%s>\n",
294 ior),
298 ACE_thread_t serverThr;
299 ACE_thread_t clientThr;
301 ACE_Thread_Manager::instance()->spawn(start_server,
302 (void*)&amh_server,
303 THR_NEW_LWP | THR_JOINABLE,
304 &serverThr);
306 ACE_Thread_Manager::instance()->spawn(start_client,
307 (void*)roundtrip.in (),
308 THR_NEW_LWP | THR_JOINABLE,
309 &clientThr);
311 ACE_Thread_Manager::instance()->join(clientThr);
312 ACE_OS::printf("End client\n");
313 ACE_Thread_Manager::instance()->join(serverThr);
314 ACE_OS::printf("End server\n");
316 orb->destroy();
318 return 0;