1 //=============================================================================
5 * Implementation of the Test interface.
7 //=============================================================================
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
,
19 : orb_ (CORBA::ORB::_duplicate (orb
)),
20 rh_ (A::AMI_TestHandler::_duplicate (rh
)),
21 max_count_ (max_count
),
24 time_t _tm
= ACE_OS::time ();
25 this->seed_
= (unsigned int)_tm
;
29 Test_i::set_opponent (A::Test_ptr opp
)
31 this->opponent_
= A::Test::_duplicate (opp
);
36 ::A::RequestMode reqmode
,
37 ::CORBA::ULong
& counter
,
38 ::A::FollowUp_out follow_up
)
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"),
48 const char* follow_up_str
;
49 switch (ACE_OS::rand_r (&this->seed_
) % 2)
52 follow_up
= (reqmode
== A::RQM_SYNCH
? A::FU_TIMER
: A::FU_NOTIFICATION
);
53 follow_up_str
= (reqmode
== A::RQM_SYNCH
? "TIMER" : "NOTIFICATION");
57 follow_up
= (reqmode
== A::RQM_ASYNCH
? A::FU_TIMER
: A::FU_NOTIFICATION
);
58 follow_up_str
= (reqmode
== A::RQM_ASYNCH
? "TIMER" : "NOTIFICATION");
62 if (!CORBA::is_nil(this->opponent_
.in ()))
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
);
74 this->opponent_
->report (buf
);
84 "%N:%l:(%P:%t):Test_i::report: %C - %C\n",
85 (this->mode_
== A::RM_MASTER
? "master" : "slave"),
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
,
102 : evh_ (orb
, max_count
, mode
)
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);
131 Test_Reply_i::report (
134 if (TAO_debug_level
> 0)
135 ACE_DEBUG ((LM_DEBUG
,
136 "%N:%l:(%P:%t):Test_Reply_i::report\n"));
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
,
155 : orb_ (CORBA::ORB::_duplicate (orb
)),
156 max_count_ (max_count
),
160 time_t _tm
= ACE_OS::time ();
161 this->seed_
= (unsigned int)_tm
;
165 TestHandler::set_counter (CORBA::ULong counter
)
167 this->counter_
= counter
;
171 TestHandler::set_opponent (A::Test_ptr opp
)
173 this->opponent_
= A::Test::_duplicate (opp
);
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
&,
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_
);
198 if (!CORBA::is_nil (this->opponent_
.in ()))
200 this->opponent_
->sendc_request(this->rh_
.in (), A::RQM_ASYNCH
, this->counter_
);
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_
);
220 if (!CORBA::is_nil (this->opponent_
.in ()))
222 this->opponent_
->sendc_request(this->rh_
.in (), A::RQM_ASYNCH
, this->counter_
);
228 void TestHandler::start ()
230 this->orb_
->orb_core ()->reactor ()->notify (this);
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 ();
245 if (this->mode_
== A::RM_MASTER
&& !CORBA::is_nil (this->opponent_
.in ()))
248 ACE_OS::snprintf (buf
, sizeof(buf
), "request #%d followup [%s]",
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));
269 this->orb_
->orb_core ()->reactor ()->notify (this);