Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Mixed_Sync_ASync_Events / test_i.cpp
blobff21e3f096530c994e589582a329aa674148544e
1 //=============================================================================
2 /**
3 * @file test_i.cpp
5 * Implementation of the Test interface.
6 */
7 //=============================================================================
9 #include "test_i.h"
10 #include "tao/debug.h"
11 #include "tao/ORB_Core.h"
12 #include "ace/Reactor.h"
13 #include "ace/OS_NS_time.h"
15 Test_i::Test_i (CORBA::ORB_ptr orb,
16 A::AMI_TestHandler_ptr rh,
17 CORBA::ULong max_count,
18 A::RunMode mode)
19 : orb_ (CORBA::ORB::_duplicate (orb)),
20 rh_ (A::AMI_TestHandler::_duplicate (rh)),
21 max_count_ (max_count),
22 mode_(mode)
24 time_t _tm = ACE_OS::time ();
25 this->seed_ = (unsigned int)_tm;
28 void
29 Test_i::set_opponent (A::Test_ptr opp)
31 this->opponent_ = A::Test::_duplicate (opp);
34 void
35 Test_i::request (
36 ::A::RequestMode reqmode,
37 ::CORBA::ULong & counter,
38 ::A::FollowUp_out follow_up)
40 ++counter;
42 ACE_DEBUG ((LM_DEBUG,
43 "%N:%l:(%P:%t):Test_i::request: (%C) %C - %d\n",
44 (reqmode == A::RQM_SYNCH ? "SYNCH" : "ASYNCH"),
45 (this->mode_ == A::RM_MASTER ? "master" : "slave"),
46 counter));
48 const char* follow_up_str;
49 switch (ACE_OS::rand_r (&this->seed_) % 2)
51 case 0:
52 follow_up = (reqmode == A::RQM_SYNCH ? A::FU_TIMER : A::FU_NOTIFICATION);
53 follow_up_str = (reqmode == A::RQM_SYNCH ? "TIMER" : "NOTIFICATION");
54 break;
55 case 1:
56 default:
57 follow_up = (reqmode == A::RQM_ASYNCH ? A::FU_TIMER : A::FU_NOTIFICATION);
58 follow_up_str = (reqmode == A::RQM_ASYNCH ? "TIMER" : "NOTIFICATION");
59 break;
62 if (!CORBA::is_nil(this->opponent_.in ()))
64 char buf[1024];
65 ACE_OS::snprintf (buf, sizeof(buf), "request #%d followup [%s]", counter, follow_up_str);
67 if (reqmode == A::RQM_SYNCH)
69 if (!CORBA::is_nil (this->rh_.in ()))
70 this->opponent_->sendc_report(this->rh_.in (), buf);
72 else
74 this->opponent_->report (buf);
79 void
80 Test_i::report (
81 const char * msg)
83 ACE_DEBUG ((LM_DEBUG,
84 "%N:%l:(%P:%t):Test_i::report: %C - %C\n",
85 (this->mode_ == A::RM_MASTER ? "master" : "slave"),
86 msg));
89 void
90 Test_i::shutdown (void)
92 if (this->mode_ == A::RM_SLAVE && !CORBA::is_nil (this->opponent_.in ()))
94 this->opponent_->shutdown ();
96 this->orb_->shutdown (0);
99 Test_Reply_i::Test_Reply_i (CORBA::ORB_ptr orb,
100 CORBA::ULong max_count,
101 A::RunMode mode)
102 : evh_ (orb, max_count, mode)
106 void
107 Test_Reply_i::request (
108 ::CORBA::ULong counter,
109 ::A::FollowUp follow_up)
111 if (TAO_debug_level > 0)
112 ACE_DEBUG ((LM_DEBUG,
113 "%N:%l:(%P:%t):Test_Reply_i::request\n"));
115 this->evh_.handle_followup (follow_up, counter);
118 void Test_Reply_i::request_excep (
119 ::Messaging::ExceptionHolder * )
121 ACE_DEBUG ((LM_ERROR,
122 "%N:%l:(%P:%t):Test_Reply_i::request_excep: %C - unexpected exception\n",
123 (this->evh_.mode () == A::RM_MASTER ? "master" : "slave")));
124 if (!CORBA::is_nil (this->evh_.opponent ()))
125 this->evh_.opponent ()->shutdown ();
126 if (this->evh_.mode () == A::RM_SLAVE)
127 this->evh_.orb ()->shutdown (0);
130 void
131 Test_Reply_i::report (
132 void)
134 if (TAO_debug_level > 0)
135 ACE_DEBUG ((LM_DEBUG,
136 "%N:%l:(%P:%t):Test_Reply_i::report\n"));
139 void
140 Test_Reply_i::report_excep (
141 ::Messaging::ExceptionHolder * )
143 ACE_DEBUG ((LM_ERROR,
144 "%N:%l:(%P:%t):Test_Reply_i::report_excep: %C - unexpected exception\n",
145 (this->evh_.mode () == A::RM_MASTER ? "master" : "slave")));
146 if (!CORBA::is_nil (this->evh_.opponent ()))
147 this->evh_.opponent ()->shutdown ();
148 if (this->evh_.mode () == A::RM_SLAVE)
149 this->evh_.orb ()->shutdown (0);
152 TestHandler::TestHandler (CORBA::ORB_ptr orb,
153 CORBA::ULong max_count,
154 A::RunMode mode)
155 : orb_ (CORBA::ORB::_duplicate (orb)),
156 max_count_ (max_count),
157 mode_ (mode),
158 counter_ (0)
160 time_t _tm = ACE_OS::time ();
161 this->seed_ = (unsigned int)_tm;
164 void
165 TestHandler::set_counter (CORBA::ULong counter)
167 this->counter_ = counter;
170 void
171 TestHandler::set_opponent (A::Test_ptr opp)
173 this->opponent_ = A::Test::_duplicate (opp);
176 void
177 TestHandler::set_reply_handler (A::AMI_TestHandler_ptr rh)
179 this->rh_ = A::AMI_TestHandler::_duplicate (rh);
183 TestHandler::handle_timeout (const ACE_Time_Value &,
184 const void *)
186 this->orb_->orb_core ()->reactor ()->cancel_timer (this);
187 if ((ACE_OS::rand_r (&this->seed_) % 2) == 0)
189 A::FollowUp followup;
190 if (!CORBA::is_nil (this->opponent_.in ()))
192 this->opponent_->request(A::RQM_SYNCH, this->counter_, followup);
193 this->handle_followup (followup, this->counter_);
196 else
198 if (!CORBA::is_nil (this->opponent_.in ()))
200 this->opponent_->sendc_request(this->rh_.in (), A::RQM_ASYNCH, this->counter_);
203 return 0;
207 TestHandler::handle_exception (ACE_HANDLE)
209 if ((ACE_OS::rand_r (&this->seed_) % 2) == 0)
211 A::FollowUp followup;
212 if (!CORBA::is_nil (this->opponent_.in ()))
214 this->opponent_->request(A::RQM_SYNCH, this->counter_, followup);
215 this->handle_followup (followup, this->counter_);
218 else
220 if (!CORBA::is_nil (this->opponent_.in ()))
222 this->opponent_->sendc_request(this->rh_.in (), A::RQM_ASYNCH, this->counter_);
225 return 0;
228 void TestHandler::start ()
230 this->orb_->orb_core ()->reactor ()->notify (this);
233 void
234 TestHandler::handle_followup (A::FollowUp fup, CORBA::ULong counter)
236 if (counter > this->max_count_)
238 if (this->mode_ == A::RM_MASTER && !CORBA::is_nil (this->opponent_.in ()))
240 this->opponent_->shutdown ();
243 else
245 if (this->mode_ == A::RM_MASTER && !CORBA::is_nil (this->opponent_.in ()))
247 char buf[1024];
248 ACE_OS::snprintf (buf, sizeof(buf), "request #%d followup [%s]",
249 counter,
250 (fup == A::FU_TIMER ? "TIMER" : "NOTIFICATION"));
251 this->opponent_->report (buf);
254 if (TAO_debug_level > 0)
256 ACE_DEBUG ((LM_DEBUG,
257 "%N:%l:(%P:%t):TestHandler::handle_followup\n"));
260 this->set_counter (counter);
261 if (fup == A::FU_TIMER)
263 this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0,
264 ACE_Time_Value (0, 100),
265 ACE_Time_Value (0, 100));
267 else
269 this->orb_->orb_core ()->reactor ()->notify (this);