Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Mixed_Sync_ASync_Events / test_i.cpp
blobccfe718ef5c3bc5689dd0aa783cfb5c37e121300
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 A::RunMode mode)
18 : orb_ (CORBA::ORB::_duplicate (orb)),
19 rh_ (A::AMI_TestHandler::_duplicate (rh)),
20 mode_(mode)
22 time_t _tm = ACE_OS::time ();
23 this->seed_ = (unsigned int)_tm;
26 void
27 Test_i::set_opponent (A::Test_ptr opp)
29 this->opponent_ = A::Test::_duplicate (opp);
32 void
33 Test_i::request (
34 ::A::RequestMode reqmode,
35 ::CORBA::ULong & counter,
36 ::A::FollowUp_out follow_up)
38 ++counter;
40 ACE_DEBUG ((LM_DEBUG,
41 "%N:%l:(%P:%t):Test_i::request: (%C) %C - %d\n",
42 (reqmode == A::RQM_SYNCH ? "SYNCH" : "ASYNCH"),
43 (this->mode_ == A::RM_MASTER ? "master" : "slave"),
44 counter));
46 const char* follow_up_str;
47 switch (ACE_OS::rand_r (&this->seed_) % 2)
49 case 0:
50 follow_up = (reqmode == A::RQM_SYNCH ? A::FU_TIMER : A::FU_NOTIFICATION);
51 follow_up_str = (reqmode == A::RQM_SYNCH ? "TIMER" : "NOTIFICATION");
52 break;
53 case 1:
54 default:
55 follow_up = (reqmode == A::RQM_ASYNCH ? A::FU_TIMER : A::FU_NOTIFICATION);
56 follow_up_str = (reqmode == A::RQM_ASYNCH ? "TIMER" : "NOTIFICATION");
57 break;
60 if (!CORBA::is_nil(this->opponent_.in ()))
62 char buf[1024];
63 ACE_OS::snprintf (buf, sizeof(buf), "request #%d followup [%s]", counter, follow_up_str);
65 if (reqmode == A::RQM_SYNCH)
67 if (!CORBA::is_nil (this->rh_.in ()))
68 this->opponent_->sendc_report(this->rh_.in (), buf);
70 else
72 this->opponent_->report (buf);
77 void
78 Test_i::report (
79 const char * msg)
81 ACE_DEBUG ((LM_DEBUG,
82 "%N:%l:(%P:%t):Test_i::report: %C - %C\n",
83 (this->mode_ == A::RM_MASTER ? "master" : "slave"),
84 msg));
87 void
88 Test_i::shutdown ()
90 if (this->mode_ == A::RM_SLAVE && !CORBA::is_nil (this->opponent_.in ()))
92 this->opponent_->shutdown ();
94 this->orb_->shutdown (false);
97 Test_Reply_i::Test_Reply_i (CORBA::ORB_ptr orb,
98 CORBA::ULong max_count,
99 A::RunMode mode)
100 : evh_ (orb, max_count, mode)
104 void
105 Test_Reply_i::request (
106 ::CORBA::ULong counter,
107 ::A::FollowUp follow_up)
109 if (TAO_debug_level > 0)
110 ACE_DEBUG ((LM_DEBUG,
111 "%N:%l:(%P:%t):Test_Reply_i::request\n"));
113 this->evh_.handle_followup (follow_up, counter);
116 void Test_Reply_i::request_excep (
117 ::Messaging::ExceptionHolder * )
119 ACE_DEBUG ((LM_ERROR,
120 "%N:%l:(%P:%t):Test_Reply_i::request_excep: %C - unexpected exception\n",
121 (this->evh_.mode () == A::RM_MASTER ? "master" : "slave")));
122 if (!CORBA::is_nil (this->evh_.opponent ()))
123 this->evh_.opponent ()->shutdown ();
124 if (this->evh_.mode () == A::RM_SLAVE)
125 this->evh_.orb ()->shutdown (0);
128 void
129 Test_Reply_i::report (
130 void)
132 if (TAO_debug_level > 0)
133 ACE_DEBUG ((LM_DEBUG,
134 "%N:%l:(%P:%t):Test_Reply_i::report\n"));
137 void
138 Test_Reply_i::report_excep (
139 ::Messaging::ExceptionHolder * )
141 ACE_DEBUG ((LM_ERROR,
142 "%N:%l:(%P:%t):Test_Reply_i::report_excep: %C - unexpected exception\n",
143 (this->evh_.mode () == A::RM_MASTER ? "master" : "slave")));
144 if (!CORBA::is_nil (this->evh_.opponent ()))
145 this->evh_.opponent ()->shutdown ();
146 if (this->evh_.mode () == A::RM_SLAVE)
147 this->evh_.orb ()->shutdown (0);
150 TestHandler::TestHandler (CORBA::ORB_ptr orb,
151 CORBA::ULong max_count,
152 A::RunMode mode)
153 : orb_ (CORBA::ORB::_duplicate (orb)),
154 max_count_ (max_count),
155 mode_ (mode),
156 counter_ (0)
158 time_t _tm = ACE_OS::time ();
159 this->seed_ = (unsigned int)_tm;
162 void
163 TestHandler::set_counter (CORBA::ULong counter)
165 this->counter_ = counter;
168 void
169 TestHandler::set_opponent (A::Test_ptr opp)
171 this->opponent_ = A::Test::_duplicate (opp);
174 void
175 TestHandler::set_reply_handler (A::AMI_TestHandler_ptr rh)
177 this->rh_ = A::AMI_TestHandler::_duplicate (rh);
181 TestHandler::handle_timeout (const ACE_Time_Value &,
182 const void *)
184 this->orb_->orb_core ()->reactor ()->cancel_timer (this);
185 if ((ACE_OS::rand_r (&this->seed_) % 2) == 0)
187 A::FollowUp followup;
188 if (!CORBA::is_nil (this->opponent_.in ()))
190 this->opponent_->request(A::RQM_SYNCH, this->counter_, followup);
191 this->handle_followup (followup, this->counter_);
194 else
196 if (!CORBA::is_nil (this->opponent_.in ()))
198 this->opponent_->sendc_request(this->rh_.in (), A::RQM_ASYNCH, this->counter_);
201 return 0;
205 TestHandler::handle_exception (ACE_HANDLE)
207 if ((ACE_OS::rand_r (&this->seed_) % 2) == 0)
209 A::FollowUp followup;
210 if (!CORBA::is_nil (this->opponent_.in ()))
212 this->opponent_->request(A::RQM_SYNCH, this->counter_, followup);
213 this->handle_followup (followup, this->counter_);
216 else
218 if (!CORBA::is_nil (this->opponent_.in ()))
220 this->opponent_->sendc_request(this->rh_.in (), A::RQM_ASYNCH, this->counter_);
223 return 0;
226 void TestHandler::start ()
228 this->orb_->orb_core ()->reactor ()->notify (this);
231 void
232 TestHandler::handle_followup (A::FollowUp fup, CORBA::ULong counter)
234 if (counter > this->max_count_)
236 if (this->mode_ == A::RM_MASTER && !CORBA::is_nil (this->opponent_.in ()))
238 this->opponent_->shutdown ();
241 else
243 if (this->mode_ == A::RM_MASTER && !CORBA::is_nil (this->opponent_.in ()))
245 char buf[1024];
246 ACE_OS::snprintf (buf, sizeof(buf), "request #%d followup [%s]",
247 counter,
248 (fup == A::FU_TIMER ? "TIMER" : "NOTIFICATION"));
249 this->opponent_->report (buf);
252 if (TAO_debug_level > 0)
254 ACE_DEBUG ((LM_DEBUG,
255 "%N:%l:(%P:%t):TestHandler::handle_followup\n"));
258 this->set_counter (counter);
259 if (fup == A::FU_TIMER)
261 this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0,
262 ACE_Time_Value (0, 100),
263 ACE_Time_Value (0, 100));
265 else
267 this->orb_->orb_core ()->reactor ()->notify (this);