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
,
18 : orb_ (CORBA::ORB::_duplicate (orb
)),
19 rh_ (A::AMI_TestHandler::_duplicate (rh
)),
22 time_t _tm
= ACE_OS::time ();
23 this->seed_
= (unsigned int)_tm
;
27 Test_i::set_opponent (A::Test_ptr opp
)
29 this->opponent_
= A::Test::_duplicate (opp
);
34 ::A::RequestMode reqmode
,
35 ::CORBA::ULong
& counter
,
36 ::A::FollowUp_out follow_up
)
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"),
46 const char* follow_up_str
;
47 switch (ACE_OS::rand_r (&this->seed_
) % 2)
50 follow_up
= (reqmode
== A::RQM_SYNCH
? A::FU_TIMER
: A::FU_NOTIFICATION
);
51 follow_up_str
= (reqmode
== A::RQM_SYNCH
? "TIMER" : "NOTIFICATION");
55 follow_up
= (reqmode
== A::RQM_ASYNCH
? A::FU_TIMER
: A::FU_NOTIFICATION
);
56 follow_up_str
= (reqmode
== A::RQM_ASYNCH
? "TIMER" : "NOTIFICATION");
60 if (!CORBA::is_nil(this->opponent_
.in ()))
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
);
72 this->opponent_
->report (buf
);
82 "%N:%l:(%P:%t):Test_i::report: %C - %C\n",
83 (this->mode_
== A::RM_MASTER
? "master" : "slave"),
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
,
100 : evh_ (orb
, max_count
, mode
)
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);
129 Test_Reply_i::report (
132 if (TAO_debug_level
> 0)
133 ACE_DEBUG ((LM_DEBUG
,
134 "%N:%l:(%P:%t):Test_Reply_i::report\n"));
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
,
153 : orb_ (CORBA::ORB::_duplicate (orb
)),
154 max_count_ (max_count
),
158 time_t _tm
= ACE_OS::time ();
159 this->seed_
= (unsigned int)_tm
;
163 TestHandler::set_counter (CORBA::ULong counter
)
165 this->counter_
= counter
;
169 TestHandler::set_opponent (A::Test_ptr opp
)
171 this->opponent_
= A::Test::_duplicate (opp
);
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
&,
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_
);
196 if (!CORBA::is_nil (this->opponent_
.in ()))
198 this->opponent_
->sendc_request(this->rh_
.in (), A::RQM_ASYNCH
, this->counter_
);
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_
);
218 if (!CORBA::is_nil (this->opponent_
.in ()))
220 this->opponent_
->sendc_request(this->rh_
.in (), A::RQM_ASYNCH
, this->counter_
);
226 void TestHandler::start ()
228 this->orb_
->orb_core ()->reactor ()->notify (this);
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 ();
243 if (this->mode_
== A::RM_MASTER
&& !CORBA::is_nil (this->opponent_
.in ()))
246 ACE_OS::snprintf (buf
, sizeof(buf
), "request #%d followup [%s]",
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));
267 this->orb_
->orb_core ()->reactor ()->notify (this);