Fixed typo and layout issues
[ACE_TAO.git] / TAO / tests / AMH_Exceptions / server.cpp
blob14dce3d4eb642feadeba147d1204878e47f18553
1 // -*- C++ -*-
2 #include "ace/OS_NS_stdio.h"
3 #include "ace/OS_NS_time.h"
4 #include "ace/Get_Opt.h"
5 #include "TestS.h"
6 #include "tao/ORB_Core.h"
8 /***************************/
9 /*** Servant Declaration ***/
11 class ST_AMH_Servant
12 : public virtual POA_Test::AMH_Roundtrip
14 public:
15 ST_AMH_Servant (CORBA::ORB_ptr orb);
17 void test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
18 Test::Timestamp send_time);
20 //FUZZ: disable check_for_lack_ACE_OS
21 void shutdown (Test::AMH_RoundtripResponseHandler_ptr _tao_rh);
22 //FUZZ: disable check_for_lack_ACE_OS
24 protected:
25 CORBA::ORB_var orb_;
29 /***************************/
30 /*** Servant Definition ***/
32 ST_AMH_Servant::ST_AMH_Servant (CORBA::ORB_ptr orb)
33 : orb_ (CORBA::ORB::_duplicate (orb))
37 void
38 ST_AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
39 Test::Timestamp)
41 // Throw an overload exception
42 Test::ServerOverload *ts = new Test::ServerOverload;
44 // Caller owns the memory now. Need not delete 'ts'
45 Test::AMH_RoundtripExceptionHolder holder (ts);
47 try
49 _tao_rh->test_method_excep (&holder);
51 catch (...)
56 void
57 ST_AMH_Servant::shutdown (Test::AMH_RoundtripResponseHandler_ptr /*_tao_rh*/)
59 this->orb_->shutdown (0);
62 /*** Server Declaration ***/
64 /**
65 Class that performs all 'dirty' initialisation work that is common to
66 all the AMH servers and 'hides' all the common ORB functions.
68 class ST_AMH_Server
70 public:
71 ST_AMH_Server (int argc, ACE_TCHAR **argv);
72 virtual ~ST_AMH_Server ();
74 /// ORB initialisation stuff
75 int start_orb_and_poa (void);
77 /// register the servant with the poa
78 virtual void register_servant (ST_AMH_Servant *servant);
80 /// orb-perform_work () abstraction
81 virtual void run_event_loop ();
83 public:
84 /// Accesor method (for servants) to the initialized ORB
85 CORBA::ORB_ptr orb () { return this->orb_.in (); }
87 protected:
88 int argc_;
89 ACE_TCHAR **argv_;
90 const ACE_TCHAR *ior_output_file_;
91 CORBA::ORB_var orb_;
92 PortableServer::POA_var root_poa_;
94 private:
95 /// Write servant IOR to file specified with the '-o' option
96 int write_ior_to_file (CORBA::String_var ior);
99 /*** Server Declaration ***/
101 ST_AMH_Server::ST_AMH_Server (int argc, ACE_TCHAR **argv)
102 : argc_ (argc)
103 , argv_ (argv)
105 this->ior_output_file_ = ACE_TEXT ("test.ior");
106 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
107 int c;
109 while ((c = get_opts ()) != -1)
110 switch (c)
112 case 'o':
113 this->ior_output_file_ = get_opts.opt_arg ();
114 break;
118 ST_AMH_Server::~ST_AMH_Server ()
122 this->root_poa_->destroy (1, 1);
124 this->orb_->destroy ();
126 catch (const CORBA::Exception& ex)
128 ex._tao_print_exception ("Exception caught:");
134 ST_AMH_Server::start_orb_and_poa (void)
138 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
140 CORBA::Object_var poa_object =
141 this->orb_->resolve_initial_references("RootPOA");
143 if (CORBA::is_nil (poa_object.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR,
145 " (%P|%t) Unable to initialize the POA.\n"),
148 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
150 PortableServer::POAManager_var poa_manager =
151 this->root_poa_->the_POAManager ();
153 poa_manager->activate ();
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Exception caught:");
158 return -1;
161 return 0;
164 void
165 ST_AMH_Server::register_servant (ST_AMH_Servant *servant)
169 CORBA::Object_var poa_object =
170 this->orb_->resolve_initial_references("RootPOA");
172 PortableServer::POA_var root_poa =
173 PortableServer::POA::_narrow (poa_object.in ());
175 PortableServer::ObjectId_var id =
176 root_poa->activate_object (servant);
178 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
180 Test::Roundtrip_var roundtrip =
181 Test::Roundtrip::_narrow (object.in ());
183 CORBA::String_var ior =
184 this->orb_->object_to_string (roundtrip.in ());
186 (void) this->write_ior_to_file (ior);
188 catch (const CORBA::Exception& ex)
190 ex._tao_print_exception ("Exception caught:");
194 void
195 ST_AMH_Server::run_event_loop ()
199 ACE_Time_Value period (0, 11000);
200 while (!this->orb_->orb_core ()->has_shutdown ())
202 this->orb_->perform_work (&period);
205 catch (const CORBA::Exception&)
210 ST_AMH_Server::write_ior_to_file (CORBA::String_var ior)
212 // If the ior_output_file exists, output the ior to it
213 FILE *output_file= ACE_OS::fopen (ST_AMH_Server::ior_output_file_,
214 "w");
215 if (output_file == 0)
217 ACE_ERROR ((LM_ERROR,
218 "Cannot open output file for writing IOR: %s",
219 ST_AMH_Server::ior_output_file_));
220 return -1;
223 ACE_OS::fprintf (output_file, "%s", ior.in ());
224 ACE_OS::fclose (output_file);
225 return 0;
230 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
232 ST_AMH_Server amh_server (argc, argv);
234 amh_server.start_orb_and_poa ();
236 ST_AMH_Servant servant (amh_server.orb ());
238 amh_server.register_servant (&servant);
240 amh_server.run_event_loop ();
242 return 0;