Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2319_Regression / server.cpp
blob6cad968abdb8dbfc1b0aa95af64cb31ba40495c8
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
11 const int sleep_time = 1; // sleep for 1 sec on each call
13 // This should equal num_calls within 'sleep * num_calls' seconds
14 int calls_received = 0;
16 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
17 const ACE_TCHAR *ior_file = ACE_TEXT("test.ior");
19 /***************************/
20 /*** Servant Declaration ***/
21 /***************************/
23 class ST_AMH_Servant : public virtual POA_Test::AMH_Roundtrip
25 public:
26 ST_AMH_Servant (CORBA::ORB_ptr orb);
28 void test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
29 Test::Timestamp send_time);
31 protected:
32 CORBA::ORB_ptr orb_;
35 /****************************/
36 /**** Server Declaration ****/
37 /****************************/
39 /**
40 Class that performs all 'dirty' initialisation work that is common to
41 all the AMH servers and 'hides' all the common ORB functions.
43 class ST_AMH_Server
45 public:
46 ST_AMH_Server (void);
47 virtual ~ST_AMH_Server ();
49 /// ORB inititalisation stuff
50 int start_orb_and_poa (const CORBA::ORB_var &_orb);
52 /// register the servant with the poa
53 virtual void register_servant (ST_AMH_Servant *servant);
55 /// orb-perform_work () abstraction
56 virtual void run_event_loop ();
58 public:
60 protected:
61 CORBA::ORB_ptr orb_;
62 PortableServer::POA_var root_poa_;
64 private:
65 /// Write servant IOR to file specified with the '-o' option
66 int write_ior_to_file (CORBA::String_var ior);
69 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 int parse_args (int argc, ACE_TCHAR *argv[])
77 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:o:k:"));
78 int c;
80 while ((c = get_opts ()) != -1)
81 switch (c)
83 case 'n':
84 num_calls = ACE_OS::atoi (get_opts.opt_arg ());
85 break;
86 case 'o':
87 ior_file = get_opts.opt_arg ();
88 break;
89 case 'k':
90 ior = get_opts.opt_arg ();
91 break;
92 default:
93 break;
95 return 0;
98 /***************************/
99 /*** Servant Definition ***/
100 /***************************/
102 // ------------------------------------------------------------------------
104 ST_AMH_Servant::ST_AMH_Servant (CORBA::ORB_ptr orb)
105 : orb_(CORBA::ORB::_duplicate(orb))
109 // ------------------------------------------------------------------------
111 void
112 ST_AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr,
113 Test::Timestamp)
115 ACE_OS::printf("Received Timestamp # %d\n", calls_received);
116 ACE_OS::sleep(1);
117 ++calls_received;
119 // When _tao_rh destructor is called, it shouldn't send anything to
120 // the client as well
123 /*** Server Declaration ***/
125 // ------------------------------------------------------------------------
127 ST_AMH_Server::ST_AMH_Server (void)
131 // ------------------------------------------------------------------------
133 ST_AMH_Server::~ST_AMH_Server ()
137 this->root_poa_->destroy (1, 1);
139 catch (const CORBA::Exception& ex)
141 ex._tao_print_exception ("Exception caught:");
145 // ------------------------------------------------------------------------
147 int ST_AMH_Server::start_orb_and_poa (const CORBA::ORB_var &_orb)
151 this->orb_ = CORBA::ORB::_duplicate(_orb.in ());
153 CORBA::Object_var poa_object =
154 this->orb_->resolve_initial_references("RootPOA");
156 if (CORBA::is_nil (poa_object.in ()))
157 ACE_ERROR_RETURN ((LM_ERROR,
158 " (%P|%t) Unable to initialize the POA.\n"),
161 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
163 PortableServer::POAManager_var poa_manager =
164 this->root_poa_->the_POAManager ();
166 poa_manager->activate ();
168 catch (const CORBA::Exception& ex)
170 ex._tao_print_exception ("Exception caught:");
171 return -1;
174 return 0;
177 // ------------------------------------------------------------------------
179 void ST_AMH_Server::register_servant (ST_AMH_Servant *servant)
183 CORBA::Object_var poa_object =
184 this->orb_->resolve_initial_references("RootPOA");
186 PortableServer::POA_var root_poa =
187 PortableServer::POA::_narrow (poa_object.in ());
189 PortableServer::ObjectId_var id =
190 root_poa->activate_object (servant);
192 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
194 Test::Roundtrip_var roundtrip = Test::Roundtrip::_narrow (object.in ());
196 CORBA::String_var iorstr = this->orb_->object_to_string(roundtrip.in ());
198 (void) this->write_ior_to_file(iorstr);
200 catch (const CORBA::Exception& ex)
202 ex._tao_print_exception ("Exception caught:");
206 // ------------------------------------------------------------------------
208 void ST_AMH_Server::run_event_loop ()
212 ACE_Time_Value period (1, 0);
213 while (1)
215 this->orb_->perform_work (&period);
217 // when all calls from client have been received, exit
218 if (calls_received == num_calls )
219 return;
222 catch (const CORBA::Exception&)
227 // ------------------------------------------------------------------------
230 ST_AMH_Server::write_ior_to_file (CORBA::String_var iorstr)
232 // If the ior_output_file exists, output the ior to it
233 FILE *output_file= ACE_OS::fopen (ior_file, "w");
234 if (output_file == 0)
236 ACE_ERROR ((LM_ERROR,
237 "Cannot open output file for writing IOR: %s",
238 ior_file));
239 return -1;
242 ACE_OS::fprintf (output_file, "%s", iorstr.in ());
243 ACE_OS::fclose (output_file);
244 return 0;
248 // ------------------------------------------------------------------------
250 static ACE_THR_FUNC_RETURN start_server(void* _arg)
252 ST_AMH_Server *amh_server = static_cast<ST_AMH_Server*>(_arg);
253 amh_server->run_event_loop();
254 return 0;
257 // ------------------------------------------------------------------------
259 static ACE_THR_FUNC_RETURN start_client(void* _arg)
261 Test::Roundtrip_var roundtrip(static_cast<Test::Roundtrip_ptr>(_arg));
263 // Do a couple of calls on the server. If the sever is trying to
264 // do something stupid like sending an exception to us, then it
265 // won't be able to handle more than 1 request from us.
266 Test::Timestamp time = 10;
268 for (int i = 0; i < num_calls; i++)
270 roundtrip->test_method(time);
271 ACE_DEBUG ((LM_DEBUG, "Sent call # %d\n", i));
274 return 0;
278 // ------------------------------------------------------------------------
280 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
282 if (parse_args (argc, argv) != 0)
283 return 1;
285 ST_AMH_Server amh_server;
286 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
288 amh_server.start_orb_and_poa(orb);
290 ST_AMH_Servant servant(orb.in());
292 amh_server.register_servant(&servant);
294 CORBA::Object_var object = orb->string_to_object(ior);
296 Test::Roundtrip_var roundtrip = Test::Roundtrip::_narrow(object.in ());
298 if (CORBA::is_nil(roundtrip.in()))
300 ACE_ERROR_RETURN ((LM_ERROR,
301 "Nil Test::Roundtrip reference <%s>\n",
302 ior),
306 ACE_thread_t serverThr;
307 ACE_thread_t clientThr;
309 ACE_Thread_Manager::instance()->spawn(start_server,
310 (void*)&amh_server,
311 THR_NEW_LWP | THR_JOINABLE,
312 &serverThr);
314 ACE_Thread_Manager::instance()->spawn(start_client,
315 (void*)roundtrip.in (),
316 THR_NEW_LWP | THR_JOINABLE,
317 &clientThr);
319 ACE_Thread_Manager::instance()->join(clientThr);
320 ACE_OS::printf("End client\n");
321 ACE_Thread_Manager::instance()->join(serverThr);
322 ACE_OS::printf("End server\n");
324 orb->destroy();
326 return 0;