Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Oneway_Timeouts / server.cpp
blob19b7f52659fc730809d66e272cea999e18567ed3
1 #include "TestS.h"
3 #include "tao/Strategies/advanced_resource.h"
4 #include "tao/Messaging/Messaging.h"
5 #include "tao/AnyTypeCode/TAOA.h"
6 #include "tao/AnyTypeCode/Any.h"
8 #include "ace/streams.h"
9 #include "ace/High_Res_Timer.h"
10 #include "ace/Reactor.h"
12 const int TIME_THRESHOLD = 50; //ms
14 int activate_delay = 0000;
15 int run_delay = 00;
16 int request_delay = 00;
17 int abort_after = 0;
18 int num_expected = 0;
19 int elapsed_max = 0;
20 int elapsed_min = 0;
21 int first_min = 0;
22 int first_max = 0;
23 const ACE_TCHAR *server_ior = ACE_TEXT ("test.ior");
25 class Tester_i
26 : public virtual POA_Tester
27 , public virtual ACE_Event_Handler
29 public:
30 Tester_i (CORBA::ORB_ptr orb)
31 : orb_ (orb)
32 , id1_ (0)
33 , id2_ (0)
34 , count_ (0)
35 , failed_ (false)
37 this->start_ = ACE_High_Res_Timer::gettimeofday_hr ();
40 virtual ~Tester_i ()
44 virtual void test (CORBA::Long id)
46 // Ignore the extra message that were used for making messages
47 // queued for SYNC_DELAYED_BUFFERING test cases.
48 if (id == -3)
49 return;
51 testShared (id);
54 virtual CORBA::Long test2 (CORBA::Long id)
56 if (id == -2)
58 // Special id used to force a connect. Ignore.
59 this->start_ = ACE_High_Res_Timer::gettimeofday_hr ();
60 return id;
62 return testShared (id);
65 int testShared (CORBA::Long id)
67 ACE_Time_Value now = ACE_High_Res_Timer::gettimeofday_hr ();
68 if (id == -1)
70 // Signals the end of a test run
71 if (num_expected > 0 && count_ != num_expected)
73 cerr << "Error: Expected " << num_expected
74 << ", but received " << count_ << endl;
75 this->failed_ = true;
77 long ms = (last_ - first_).msec ();
78 if (elapsed_max > 0 && ms > elapsed_max)
80 cerr << "Error: Expected < " << elapsed_max
81 << "ms, but was " << ms << "ms" << endl;
82 this->failed_ = true;
84 if (elapsed_min > 0 && ms < elapsed_min)
86 cerr << "Error: Expected > " << elapsed_min
87 << "ms, but was " << ms << "ms" << endl;
88 this->failed_ = true;
90 ms = (first_ - start_).msec ();
91 if (first_max > 0 && ms > first_max)
93 cerr << "Error: Expected first < " << first_max
94 << "ms, but was " << ms << "ms" << endl;
95 this->failed_ = true;
97 if (first_min > 0 && ms < first_min)
99 cerr << "Error: Expected first > " << first_min
100 << "ms, but was " << ms << "ms" << endl;
101 this->failed_ = true;
103 ACE_Time_Value timeout (0, 50 * 1000);
104 this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0, timeout);
105 return id;
109 this->last_ = now;
110 if (id == 0)
112 this->first_ = now;
114 ++this->count_;
115 cout << 's' << id << endl;
116 if (abort_after > 0 && this->count_ >= abort_after)
118 cout << "\nAborting..." << endl;
119 ACE_OS::abort ();
121 if (request_delay > 0 && id == 0)
123 ACE_OS::sleep (ACE_Time_Value (0, 1000 * request_delay));
125 return id;
128 int handle_timeout (const ACE_Time_Value &, const void *)
130 this->orb_->shutdown (0);
131 return 0;
134 bool failed () const {
135 return this->failed_;
138 private:
139 CORBA::ORB_ptr orb_;
140 CORBA::Long id1_;
141 CORBA::Long id2_;
142 int count_;
143 bool failed_;
144 ACE_Time_Value start_;
145 ACE_Time_Value first_;
146 ACE_Time_Value last_;
149 #include "tao/Messaging/Messaging.h"
150 #include "tao/Strategies/advanced_resource.h"
152 #include "ace/streams.h"
153 #include "ace/Log_Msg.h"
154 #include "ace/Arg_Shifter.h"
156 using namespace CORBA;
157 using namespace PortableServer;
159 namespace {
161 void print_usage ()
163 //FUZZ: disable check_for_lack_ACE_OS
164 cout << "server [-activate_delay ms] [-run_delay ms] [-request_delay ms] "
165 "[-abort_after n] [-server_ior <ior>]\n"
166 "\t[-expected n=0] [-elapsed_max ms=0] [-elapsed_min ms=0] "
167 "[-first_min ms=0]\n"
168 "\t[-first_max ms=0]\n"
169 "\tactivate_delay Millisecond delay before POAManager::activate.\n"
170 "\trun_delay Millisecond delay before ORB::run ().\n"
171 "\trequest_delay Millisecond delay within each servant request.\n"
172 "\tabort_after abort () after N requests.\n"
173 "\tior Server ior.\n" << endl;
174 //FUZZ: enable check_for_lack_ACE_OS
177 bool parse_command_line (int ac, ACE_TCHAR *av[])
179 ACE_Arg_Shifter args (ac, av);
180 args.consume_arg ();
181 while (args.is_anything_left ())
183 if (args.cur_arg_strncasecmp (ACE_TEXT ("-activate_delay")) == 0)
185 args.consume_arg ();
186 activate_delay = ACE_OS::atoi (args.get_current ());
187 args.consume_arg ();
189 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-run_delay")) == 0)
191 args.consume_arg ();
192 run_delay = ACE_OS::atoi (args.get_current ());
193 args.consume_arg ();
195 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-request_delay")) == 0)
197 args.consume_arg ();
198 request_delay = ACE_OS::atoi (args.get_current ());
199 args.consume_arg ();
201 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-expected")) == 0)
203 args.consume_arg ();
204 num_expected = ACE_OS::atoi (args.get_current ());
205 args.consume_arg ();
207 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-elapsed_max")) == 0)
209 args.consume_arg ();
210 elapsed_max = ACE_OS::atoi (args.get_current ());
211 args.consume_arg ();
213 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-elapsed_min")) == 0)
215 args.consume_arg ();
216 elapsed_min = ACE_OS::atoi (args.get_current ());
217 args.consume_arg ();
219 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-first_min")) == 0)
221 args.consume_arg ();
222 first_min = ACE_OS::atoi (args.get_current ());
223 args.consume_arg ();
225 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-first_max")) == 0)
227 args.consume_arg ();
228 first_max = ACE_OS::atoi (args.get_current ());
229 args.consume_arg ();
231 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-abort_after")) == 0)
233 args.consume_arg ();
234 abort_after = ACE_OS::atoi (args.get_current ());
235 args.consume_arg ();
237 else if (args.cur_arg_strncasecmp (ACE_TEXT ("-server_ior")) == 0)
239 args.consume_arg ();
240 server_ior = args.get_current ();
241 args.consume_arg ();
243 else
245 cerr << "Error: Unknown argument \""
246 << args.get_current () << "\"" << endl;
247 print_usage ();
248 return false;
251 return true;
254 void WriteIOR (const char *ior)
256 ofstream out (ACE_TEXT_ALWAYS_CHAR (server_ior));
257 out << ior;
260 POA_ptr create_poa (ORB_ptr orb)
262 PolicyList pols (2);
263 Object_var obj = orb->resolve_initial_references ("RootPOA");
264 POA_var root = POA::_narrow (obj.in ());
265 ACE_ASSERT (! is_nil (root.in ()));
266 pols.length (2);
267 pols[0] = root->create_id_assignment_policy (PortableServer::USER_ID);
268 pols[1] = root->create_lifespan_policy (PortableServer::PERSISTENT);
269 POAManager_var man = root->the_POAManager ();
270 POA_var poa = root->create_POA ("X", man.in (), pols);
271 return poa._retn ();
275 int ACE_TMAIN (int ac, ACE_TCHAR *av[])
279 ORB_var orb = ORB_init (ac, av);
281 if (!parse_command_line (ac, av))
283 return 1;
286 POA_var poa = create_poa (orb.in ());
287 ACE_ASSERT (! is_nil (poa.in ()));
289 Tester_i svt (orb.in ());
291 ObjectId_var id = string_to_ObjectId ("tester");
293 poa->activate_object_with_id (id.in (), &svt);
294 Object_var obj = poa->id_to_reference (id.in ());
295 String_var ior = orb->object_to_string (obj.in ());
296 WriteIOR (ior.in ());
298 cout << "Servants registered and activated." << endl;
300 if (activate_delay > 0)
302 ACE_OS::sleep (ACE_Time_Value (0, activate_delay * 1000));
304 POAManager_var man = poa->the_POAManager ();
305 man->activate ();
307 cout << "POAManager activated." << endl;
309 if (run_delay > 0)
311 ACE_OS::sleep (ACE_Time_Value (0, run_delay * 1000));
313 cout << "Running orb..." << endl;
315 orb->run ();
317 if (svt.failed ())
319 return 1;
322 return 0;
324 catch (const CORBA::Exception &ex)
326 ex._tao_print_exception ("server:");
329 return 1;