More tests update
[ACE_TAO.git] / TAO / tests / UNKNOWN_Exception / server.cpp
blob6e6a3f0066a167bef4afbdf30080a31391bfb756
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 (void)
12 throw 1;
15 class test_i :
16 public POA_test
18 public:
20 test_i (CORBA::ORB_ptr orb);
22 void normal_method (void);
24 void unknown_exception_in_method (void);
26 void unknown_exception_during_deactivation (void);
28 void _add_ref (void);
29 void _remove_ref (void);
31 CORBA::ORB_var orb_;
33 int reference_count_;
36 test_i::test_i (CORBA::ORB_ptr orb)
37 : orb_ (CORBA::ORB::_duplicate (orb)),
38 reference_count_ (1)
42 void
43 test_i::normal_method (void)
45 ACE_DEBUG ((LM_DEBUG,
46 "test_i::normal_method() called\n"));
49 void
50 test_i::unknown_exception_in_method (void)
52 ACE_DEBUG ((LM_DEBUG,
53 "test_i::unknown_exception_in_method() called\n"));
55 ACE_DEBUG ((LM_DEBUG,
56 "Unknown exception being generated: should be propagated to the client\n"));
58 throw_exception ();
61 void
62 test_i::unknown_exception_during_deactivation (void)
64 ACE_DEBUG ((LM_DEBUG,
65 "test_i::unknown_exception_during_deactivation() called\n"));
67 PortableServer::POA_var poa =
68 this->_default_POA ();
70 PortableServer::ObjectId_var id =
71 poa->servant_to_id (this);
73 poa->deactivate_object (id.in ());
76 void
77 test_i::_add_ref (void)
79 ACE_DEBUG ((LM_DEBUG,
80 "test_i::_add_ref() called; current refcount = %d\n",
81 this->reference_count_++));
84 void
85 test_i::_remove_ref (void)
87 ACE_DEBUG ((LM_DEBUG,
88 "test_i::_remove_ref() called; current refcount = %d\n",
89 this->reference_count_--));
91 if (this->reference_count_ == 0)
93 delete this;
95 ACE_DEBUG ((LM_DEBUG,
96 "Unknown exception being generated: should be gobbled up by the POA\n"));
98 throw_exception ();
102 //FUZZ: disable check_for_lack_ACE_OS
103 class test_factory_i :
104 public POA_test_factory
106 public:
108 test_factory_i (CORBA::ORB_ptr orb);
110 test_ptr create_test (void);
112 void shutdown (void);
114 CORBA::ORB_var orb_;
116 //FUZZ: enable check_for_lack_ACE_OS
118 test_factory_i::test_factory_i (CORBA::ORB_ptr orb)
119 : orb_ (CORBA::ORB::_duplicate (orb))
123 test_ptr
124 test_factory_i::create_test (void)
126 test_i *servant =
127 new test_i (this->orb_.in ());
129 PortableServer::ServantBase_var safe_servant (servant);
130 ACE_UNUSED_ARG (safe_servant);
132 CORBA::Object_var poa_object =
133 this->orb_->resolve_initial_references("RootPOA");
135 PortableServer::POA_var root_poa =
136 PortableServer::POA::_narrow (poa_object.in ());
138 PortableServer::ObjectId_var id_act =
139 root_poa->activate_object (servant);
141 CORBA::Object_var object = root_poa->id_to_reference (id_act.in ());
143 test_var test =
144 test::_narrow (object.in ());
146 return test._retn ();
149 void
150 test_factory_i::shutdown (void)
152 ACE_DEBUG ((LM_DEBUG,
153 "factory_i::shutdown() called\n"));
155 done = 1;
156 this->orb_->shutdown (0);
160 parse_args (int argc, ACE_TCHAR *argv[])
162 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
163 int c;
165 while ((c = get_opts ()) != -1)
166 switch (c)
168 case 'o':
169 ior_output_file = get_opts.opt_arg ();
170 break;
171 case '?':
172 default:
173 ACE_ERROR_RETURN ((LM_ERROR,
174 "\nusage %s:\n"
175 "\t-o <ior output file> [defaults to %s]\n"
176 "\n",
177 argv [0],
178 ior_output_file),
179 -1);
182 return 0;
186 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
190 CORBA::ORB_var orb =
191 CORBA::ORB_init (argc,argv);
193 CORBA::Object_var poa_object =
194 orb->resolve_initial_references ("RootPOA");
196 PortableServer::POA_var root_poa =
197 PortableServer::POA::_narrow (poa_object.in ());
199 PortableServer::POAManager_var poa_manager =
200 root_poa->the_POAManager ();
202 if (parse_args (argc, argv) != 0)
203 return -1;
206 test_factory_i *servant =
207 new test_factory_i (orb.in ());
209 PortableServer::ServantBase_var safe_servant (servant);
210 ACE_UNUSED_ARG (safe_servant);
212 PortableServer::ObjectId_var id_act =
213 root_poa->activate_object (servant);
215 CORBA::Object_var object = root_poa->id_to_reference (id_act.in ());
217 test_factory_var test_factory =
218 test_factory::_narrow (object.in ());
220 CORBA::String_var ior =
221 orb->object_to_string (test_factory.in ());
223 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
224 if (output_file == 0)
225 ACE_ERROR_RETURN ((LM_ERROR,
226 "Cannot open output file for writing IOR: %s",
227 ior_output_file),
228 -1);
229 ACE_OS::fprintf (output_file, "%s", ior.in ());
230 ACE_OS::fclose (output_file);
233 poa_manager->activate ();
235 TAO_Root_POA *tao_poa = dynamic_cast <TAO_Root_POA*> (root_poa.in ());
237 while (!done)
239 CORBA::ULong outstanding_requests =
240 tao_poa->outstanding_requests ();
242 ACE_DEBUG ((LM_DEBUG,
243 "Number of outstanding requests before ORB::perform_work(): %d\n",
244 outstanding_requests));
246 ACE_ASSERT (outstanding_requests == 0);
248 orb->perform_work ();
250 // On some systems this loop must yield or else the other threads
251 // will not get a chance to run.
252 ACE_OS::thr_yield ();
255 catch (...)
257 ACE_ERROR_RETURN ((LM_ERROR,
258 "Failure: Unexpected exception caught\n"),
259 -1);
262 return 0;