Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / UNKNOWN_Exception / server.cpp
blob0bdedee34a81e4e0e4b7a592a5a39fd57a88011a
1 #include "ace/Get_Opt.h"
2 #include "testS.h"
3 #include "tao/PortableServer/Root_POA.h"
4 #include "ace/OS_NS_stdio.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
7 static int done = 0;
9 void
10 throw_exception ()
12 throw 1;
15 class test_i :
16 public POA_test
18 public:
19 test_i (CORBA::ORB_ptr orb);
21 void normal_method ();
23 void unknown_exception_in_method ();
25 void unknown_exception_during_deactivation ();
27 void _add_ref ();
28 void _remove_ref ();
30 CORBA::ORB_var orb_;
32 int reference_count_;
35 test_i::test_i (CORBA::ORB_ptr orb)
36 : orb_ (CORBA::ORB::_duplicate (orb)),
37 reference_count_ (1)
41 void
42 test_i::normal_method ()
44 ACE_DEBUG ((LM_DEBUG,
45 "test_i::normal_method() called\n"));
48 void
49 test_i::unknown_exception_in_method ()
51 ACE_DEBUG ((LM_DEBUG,
52 "test_i::unknown_exception_in_method() called\n"));
54 ACE_DEBUG ((LM_DEBUG,
55 "Unknown exception being generated: should be propagated to the client\n"));
57 throw_exception ();
60 void
61 test_i::unknown_exception_during_deactivation ()
63 ACE_DEBUG ((LM_DEBUG,
64 "test_i::unknown_exception_during_deactivation() called\n"));
66 PortableServer::POA_var poa =
67 this->_default_POA ();
69 PortableServer::ObjectId_var id =
70 poa->servant_to_id (this);
72 poa->deactivate_object (id.in ());
75 void
76 test_i::_add_ref ()
78 ACE_DEBUG ((LM_DEBUG,
79 "test_i::_add_ref() called; current refcount = %d\n",
80 this->reference_count_++));
83 void
84 test_i::_remove_ref ()
86 ACE_DEBUG ((LM_DEBUG,
87 "test_i::_remove_ref() called; current refcount = %d\n",
88 this->reference_count_--));
90 if (this->reference_count_ == 0)
92 delete this;
94 ACE_DEBUG ((LM_DEBUG,
95 "Unknown exception being generated: should be gobbled up by the POA\n"));
97 throw_exception ();
101 //FUZZ: disable check_for_lack_ACE_OS
102 class test_factory_i :
103 public POA_test_factory
105 public:
106 test_factory_i (CORBA::ORB_ptr orb);
108 test_ptr create_test ();
110 void shutdown ();
112 CORBA::ORB_var orb_;
114 //FUZZ: enable check_for_lack_ACE_OS
116 test_factory_i::test_factory_i (CORBA::ORB_ptr orb)
117 : orb_ (CORBA::ORB::_duplicate (orb))
121 test_ptr
122 test_factory_i::create_test ()
124 test_i *servant =
125 new test_i (this->orb_.in ());
127 PortableServer::ServantBase_var safe_servant (servant);
128 ACE_UNUSED_ARG (safe_servant);
130 CORBA::Object_var poa_object =
131 this->orb_->resolve_initial_references("RootPOA");
133 PortableServer::POA_var root_poa =
134 PortableServer::POA::_narrow (poa_object.in ());
136 PortableServer::ObjectId_var id_act =
137 root_poa->activate_object (servant);
139 CORBA::Object_var object = root_poa->id_to_reference (id_act.in ());
141 test_var test =
142 test::_narrow (object.in ());
144 return test._retn ();
147 void
148 test_factory_i::shutdown ()
150 ACE_DEBUG ((LM_DEBUG,
151 "factory_i::shutdown() called\n"));
153 done = 1;
154 this->orb_->shutdown (false);
158 parse_args (int argc, ACE_TCHAR *argv[])
160 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
161 int c;
163 while ((c = get_opts ()) != -1)
164 switch (c)
166 case 'o':
167 ior_output_file = get_opts.opt_arg ();
168 break;
169 case '?':
170 default:
171 ACE_ERROR_RETURN ((LM_ERROR,
172 "\nusage %s:\n"
173 "\t-o <ior output file> [defaults to %s]\n"
174 "\n",
175 argv [0],
176 ior_output_file),
177 -1);
180 return 0;
184 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
188 CORBA::ORB_var orb =
189 CORBA::ORB_init (argc,argv);
191 CORBA::Object_var poa_object =
192 orb->resolve_initial_references ("RootPOA");
194 PortableServer::POA_var root_poa =
195 PortableServer::POA::_narrow (poa_object.in ());
197 PortableServer::POAManager_var poa_manager =
198 root_poa->the_POAManager ();
200 if (parse_args (argc, argv) != 0)
201 return -1;
204 test_factory_i *servant =
205 new test_factory_i (orb.in ());
207 PortableServer::ServantBase_var safe_servant (servant);
208 ACE_UNUSED_ARG (safe_servant);
210 PortableServer::ObjectId_var id_act =
211 root_poa->activate_object (servant);
213 CORBA::Object_var object = root_poa->id_to_reference (id_act.in ());
215 test_factory_var test_factory =
216 test_factory::_narrow (object.in ());
218 CORBA::String_var ior =
219 orb->object_to_string (test_factory.in ());
221 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
222 if (output_file == 0)
223 ACE_ERROR_RETURN ((LM_ERROR,
224 "Cannot open output file for writing IOR: %s",
225 ior_output_file),
226 -1);
227 ACE_OS::fprintf (output_file, "%s", ior.in ());
228 ACE_OS::fclose (output_file);
231 poa_manager->activate ();
233 TAO_Root_POA *tao_poa = dynamic_cast <TAO_Root_POA*> (root_poa.in ());
235 while (!done)
237 CORBA::ULong outstanding_requests =
238 tao_poa->outstanding_requests ();
240 ACE_DEBUG ((LM_DEBUG,
241 "Number of outstanding requests before ORB::perform_work(): %d\n",
242 outstanding_requests));
244 ACE_ASSERT (outstanding_requests == 0);
246 orb->perform_work ();
248 // On some systems this loop must yield or else the other threads
249 // will not get a chance to run.
250 ACE_OS::thr_yield ();
253 catch (...)
255 ACE_ERROR_RETURN ((LM_ERROR,
256 "Failure: Unexpected exception caught\n"),
257 -1);
260 return 0;