Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / AMH_Oneway / server.cpp
blob38fc4db9cca977ad9c9c0c09d2c84855185a843c
1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_unistd.h"
3 #include "ace/Get_Opt.h"
4 #include "TestS.h"
6 int num_calls = 10; // total calls client is going to make
8 // This should equal num_calls within 'sleep * num_calls' seconds
9 int calls_received = 0;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'n':
21 num_calls = ACE_OS::atoi (get_opts.opt_arg ());
22 break;
23 default:
24 break;
26 return 0;
30 /***************************/
31 /*** Servant Declaration ***/
33 class ST_AMH_Servant
34 : public virtual POA_Test::AMH_Roundtrip
36 public:
37 ST_AMH_Servant (CORBA::ORB_ptr orb);
39 void test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
40 Test::Timestamp send_time);
42 protected:
43 CORBA::ORB_var orb_;
46 /***************************/
47 /*** Servant Definition ***/
49 ST_AMH_Servant::ST_AMH_Servant (CORBA::ORB_ptr orb)
50 : orb_ (CORBA::ORB::_duplicate (orb))
54 void
55 ST_AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr,
56 Test::Timestamp)
58 ACE_OS::sleep (1);
59 ACE_DEBUG ((LM_DEBUG, "Received Timestamp # %d on %T\n", calls_received));
60 ++calls_received;
62 // When _tao_rh destructor is called, it shouldn't send anything to
63 // the client as well
66 /*** Server Declaration ***/
68 /**
69 Class that performs all 'dirty' initialisation work that is common to
70 all the AMH servers and 'hides' all the common ORB functions.
72 class ST_AMH_Server
74 public:
75 ST_AMH_Server (int *argc, ACE_TCHAR **argv);
76 virtual ~ST_AMH_Server () = default;
78 /// ORB initialization stuff
79 int start_orb_and_poa ();
81 /// register the servant with the poa
82 virtual void register_servant (ST_AMH_Servant *servant);
84 /// orb-perform_work () abstraction
85 virtual void run_event_loop ();
87 /// do post-run cleanup. This is necessary here because the servant
88 /// supplied to register_servant happens to be allocated on the
89 /// stack, and done after the instance of ST_AMH_Server is
90 /// created. This leads to the servant being destroyed before this
91 /// class's destructor. And alternative solution would be allocate
92 /// the servant on the stack and rely on the reference count to
93 /// delete it.
94 virtual void cleanup ();
96 public:
97 /// Accesor method (for servants) to the initialized ORB
98 CORBA::ORB_ptr orb () { return this->orb_.in (); }
100 protected:
101 int *argc_;
102 ACE_TCHAR **argv_;
103 char *ior_output_file_;
104 CORBA::ORB_var orb_;
105 PortableServer::POA_var root_poa_;
107 private:
108 /// Write servant IOR to file specified with the '-o' option
109 int write_ior_to_file (CORBA::String_var ior);
112 /*** Server Declaration ***/
114 ST_AMH_Server::ST_AMH_Server (int* argc, ACE_TCHAR **argv)
115 : argc_ (argc)
116 , argv_ (argv)
118 this->ior_output_file_ = const_cast<char*> ("test.ior");
121 void
122 ST_AMH_Server::cleanup ()
126 this->root_poa_->destroy (1, 1);
128 this->orb_->destroy ();
130 catch (const CORBA::Exception& ex)
132 ex._tao_print_exception ("Exception caught:");
138 ST_AMH_Server::start_orb_and_poa ()
142 this->orb_ = CORBA::ORB_init (*(this->argc_),
143 this->argv_);
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 void
170 ST_AMH_Server::register_servant (ST_AMH_Servant *servant)
174 CORBA::Object_var poa_object =
175 this->orb_->resolve_initial_references("RootPOA");
177 PortableServer::POA_var root_poa =
178 PortableServer::POA::_narrow (poa_object.in ());
180 PortableServer::ObjectId_var id =
181 root_poa->activate_object (servant);
183 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
185 Test::Roundtrip_var roundtrip =
186 Test::Roundtrip::_narrow (object.in ());
188 CORBA::String_var ior =
189 this->orb_->object_to_string (roundtrip.in ());
191 (void) this->write_ior_to_file (ior);
193 catch (const CORBA::Exception& ex)
195 ex._tao_print_exception ("Exception caught:");
199 void
200 ST_AMH_Server::run_event_loop ()
204 while (1)
206 ACE_Time_Value period (0, 11000);
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 ST_AMH_Server::write_ior_to_file (CORBA::String_var ior)
221 // If the ior_output_file exists, output the ior to it
222 FILE *output_file= ACE_OS::fopen (ST_AMH_Server::ior_output_file_,
223 "w");
224 if (output_file == 0)
226 ACE_ERROR ((LM_ERROR,
227 "Cannot open output file for writing IOR: %s",
228 ST_AMH_Server::ior_output_file_));
229 return -1;
232 ACE_OS::fprintf (output_file, "%s", ior.in ());
233 ACE_OS::fclose (output_file);
234 return 0;
239 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
241 ST_AMH_Server amh_server (&argc, argv);
243 amh_server.start_orb_and_poa ();
245 if (parse_args (argc, argv) != 0)
246 return 1;
248 ST_AMH_Servant servant (amh_server.orb ());
250 amh_server.register_servant (&servant);
252 amh_server.run_event_loop ();
254 amh_server.cleanup ();
256 return 0;