1 #include "tao/AnyTypeCode/Any.h"
2 #include "tao/AnyTypeCode/TypeCode.h"
4 #include "tao/RTScheduling/Request_Interceptor.h"
5 #include "tao/RTScheduling/Current.h"
6 #include "tao/RTScheduling/Distributable_Thread.h"
8 #include "tao/TSS_Resources.h"
10 #include "tao/ORB_Constants.h"
11 #include "ace/OS_NS_string.h"
13 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
16 Client_Interceptor::SchedulingInfo
= 30;
19 Client_Interceptor::send_request (PortableInterceptor::ClientRequestInfo_ptr ri
)
21 if (TAO_debug_level
> 0)
22 TAOLIB_DEBUG ((LM_DEBUG
,
23 "Client_Interceptor::send_request\n"));
26 TAO_RTScheduler_Current_i
*new_current
= 0;
27 TAO_RTScheduler_Current_i
*current
= 0;
29 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
31 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
35 // If this is a one way request
36 if (!ri
->response_expected ())
39 RTScheduling::Current::IdType guid
;
40 guid
.length (sizeof(size_t));
42 size_t temp
= ++TAO_RTScheduler_Current::guid_counter
;
43 ACE_OS::memcpy (guid
.get_buffer (),
52 if (TAO_debug_level
> 0)
53 TAOLIB_DEBUG ((LM_DEBUG
,
54 "The Guid is %d %d\n",
56 TAO_RTScheduler_Current::guid_counter
.load ()));
59 RTScheduling::DistributableThread_var dt
=
60 TAO_DistributableThread_Factory::create_DT ();
63 int result
= current
->dt_hash ()->bind (guid
, dt
);
66 TAOLIB_DEBUG ((LM_DEBUG
,
67 "No Scheduling Segment Context\n"));
68 throw ::CORBA::INTERNAL ();
71 // @@ Store implicit_sched_param in a var
73 // Create new temporary current. Note that
74 // the new <sched_param> is the current
75 // <implicit_sched_param> and there is no
77 CORBA::Policy_var implicit_sched_param
=
78 current
->implicit_scheduling_parameter ();
80 TAO_RTScheduler_Current_i (current
->orb (),
84 implicit_sched_param
.in (),
89 // Install new current in the ORB.
90 //current->implementation (new_current);
91 tss
->rtscheduler_current_impl_
= new_current
;
94 // Scheduler populates the service context with
95 // scheduling parameters.
96 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
97 scheduler
->send_request (ri
);
99 // If this is a one way request
100 if (!ri
->response_expected ())
102 // Cleanup temporary DT.
103 new_current
->cleanup_DT ();
105 //Restore old current
106 new_current
->cleanup_current ();
112 Client_Interceptor::send_poll (PortableInterceptor::ClientRequestInfo_ptr ri
)
114 if (TAO_debug_level
> 0)
116 TAOLIB_DEBUG ((LM_DEBUG
,
117 "Client_Interceptor::send_poll\n"));
120 TAO_RTScheduler_Current_i
*current
= 0;
122 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
124 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
127 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
128 scheduler
->send_poll (ri
);
133 Client_Interceptor::receive_reply (PortableInterceptor::ClientRequestInfo_ptr ri
)
135 if (TAO_debug_level
> 0)
137 TAOLIB_DEBUG ((LM_DEBUG
,
138 "Client_Interceptor::receive_reply\n"));
141 TAO_RTScheduler_Current_i
*current
= 0;
143 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
145 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
148 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
149 scheduler
->receive_reply (ri
);
154 Client_Interceptor::receive_exception (PortableInterceptor::ClientRequestInfo_ptr ri
)
156 if (TAO_debug_level
> 0)
158 TAOLIB_DEBUG ((LM_DEBUG
,
159 "Client_Interceptor::receive_exception\n"));
162 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
164 TAO_RTScheduler_Current_i
*current
=
165 static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
171 TAOLIB_ERROR ((LM_ERROR
,
172 "Client_Interceptor::receive_exception ri = 0\n"));
176 CORBA::Any_var ex
= ri
->received_exception ();
177 CORBA::TypeCode_var type
= ex
->type ();
179 if (CORBA::is_nil (type
.in ()))
181 TAOLIB_ERROR ((LM_ERROR
,
185 const char * id
= type
->id ();
187 if (TAO_debug_level
> 0)
188 TAOLIB_DEBUG ((LM_DEBUG
,
189 "Received Exception %C\n",
192 // If the remote host threw a THREAD_CANCELLED
193 // exception, make sure to take the appropriate
195 if (ACE_OS::strstr (id
, "CORBA::THREAD_CANCELLED") == 0)
197 // Perform the necessary cleanup as the
198 // thread was cancelled.
199 current
->cancel_thread ();
203 // Inform scheduler that exception was
205 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
206 scheduler
->receive_exception (ri
);
212 Client_Interceptor::receive_other (PortableInterceptor::ClientRequestInfo_ptr ri
)
214 if (TAO_debug_level
> 0)
216 TAOLIB_DEBUG ((LM_DEBUG
,
217 "Client_Interceptor::receive_other\n"));
220 TAO_RTScheduler_Current_i
*current
= 0;
222 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
224 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
227 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
228 scheduler
->receive_other (ri
);
233 Client_Interceptor::name ()
235 return CORBA::string_dup ("RTSchdeuler_Client_Interceptor");
239 Client_Interceptor::destroy ()
244 Server_Interceptor::SchedulingInfo
= 30;
246 Server_Interceptor::Server_Interceptor (TAO_RTScheduler_Current_ptr current
)
248 this->current_
= TAO_RTScheduler_Current::_duplicate (current
);
252 Server_Interceptor::receive_request_service_contexts (
253 PortableInterceptor::ServerRequestInfo_ptr
)
255 if (TAO_debug_level
> 0)
257 TAOLIB_DEBUG ((LM_DEBUG
,
258 "Server_Interceptor::receive_request_service_contexts\n"));
263 Server_Interceptor::receive_request (PortableInterceptor::ServerRequestInfo_ptr ri
)
265 if (TAO_debug_level
> 0)
267 TAOLIB_DEBUG ((LM_DEBUG
,
268 "Server_Interceptor::receive_request\n"));
271 IOP::ServiceContext_var serv_cxt
;
276 ri
->get_request_service_context (Server_Interceptor::SchedulingInfo
);
278 catch (const ::CORBA::Exception
&)
280 if (TAO_debug_level
> 0)
281 TAOLIB_DEBUG ((LM_DEBUG
,
282 "Invalid Service Request\n"));
286 if (TAO_debug_level
> 0)
287 TAOLIB_DEBUG ((LM_DEBUG
,
288 "Request from Distributable Thread\n"));
290 RTScheduling::Current::IdType_var guid_var
;
292 CORBA::Policy_var sched_param
= 0;
293 CORBA::Policy_var implicit_sched_param
= 0;
295 TAO_RTScheduler_Current_i
* new_current
= 0;
296 ACE_NEW_THROW_EX (new_current
,
297 TAO_RTScheduler_Current_i (this->current_
->orb (),
298 this->current_
->dt_hash ()),
300 CORBA::SystemException::_tao_minor_code (
303 CORBA::COMPLETED_NO
));
305 // Scheduler retrieves scheduling parameters
306 // from request and populates the out
308 RTScheduling::Scheduler_var scheduler
= new_current
->scheduler();
309 scheduler
->receive_request (ri
,
313 implicit_sched_param
.out ());
315 if (guid_var
->length () == 0)
317 TAOLIB_ERROR ((LM_ERROR
,
318 "The scheduler MUST retrieve and return the "
319 "GUID from the service context\n"));
322 RTScheduling::Current::IdType guid
;
323 guid
.length (sizeof (size_t));
324 ACE_OS::memcpy (guid
.get_buffer (),
325 guid_var
->get_buffer (),
333 if (TAO_debug_level
> 0)
334 TAOLIB_DEBUG ((LM_DEBUG
,
339 RTScheduling::DistributableThread_var dt
= TAO_DistributableThread_Factory::create_DT ();
341 // Add new DT to map.
342 int result
= new_current
->dt_hash ()->bind (guid
, dt
);
346 throw ::CORBA::INTERNAL ();
349 // Create new temporary current. Note that the new <sched_param> is
350 // the current <implicit_sched_param> and there is no segment name.
351 new_current
->id (guid
);
352 new_current
->name (name
);
353 new_current
->scheduling_parameter (sched_param
.in ());
354 new_current
->implicit_scheduling_parameter (implicit_sched_param
.in ());
355 new_current
->DT (dt
.in ());
357 // Install new current in the ORB and store the previous current
359 // current->implementation (new_current)
360 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
362 tss
->rtscheduler_previous_current_impl_
= this->current_
->implementation (new_current
);
366 Server_Interceptor::send_reply (PortableInterceptor::ServerRequestInfo_ptr ri
)
368 if (TAO_debug_level
> 0)
370 TAOLIB_DEBUG ((LM_DEBUG
,
371 "Server_Interceptor::send_reply\n"));
374 TAO_RTScheduler_Current_i
*current
= 0;
375 TAO_RTScheduler_Current_i
*prev_current
= 0;
377 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
379 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
382 RTScheduling::DistributableThread_var dt
= current
->DT ();
383 if (dt
->state () == RTScheduling::DistributableThread::CANCELLED
)
385 current
->cancel_thread ();
389 else TAOLIB_DEBUG ((LM_DEBUG
,
390 "Thread Not Cancelled\n"));
393 // Inform scheduler that upcall is complete.
394 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
395 scheduler
->send_reply (ri
);
397 current
->cleanup_DT ();
398 current
->cleanup_current ();
400 // Get the previous current if any.
401 prev_current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_previous_current_impl_
);
403 // Restore the previous current.
404 tss
->rtscheduler_current_impl_
= prev_current
;
406 // Reset the previous current pointer.
407 tss
->rtscheduler_previous_current_impl_
= 0;
409 else TAOLIB_DEBUG ((LM_DEBUG
,
410 "Send Reply Current is 0\n"));
414 Server_Interceptor::send_exception (PortableInterceptor::ServerRequestInfo_ptr ri
)
416 if (TAO_debug_level
> 0)
418 TAOLIB_DEBUG ((LM_DEBUG
,
419 "Server_Interceptor::send_exception\n"));
422 TAO_RTScheduler_Current_i
*current
= 0;
424 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
426 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
429 // Inform scheduler that upcall is complete.
430 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
431 scheduler
->send_exception (ri
);
433 current
->cleanup_DT ();
434 current
->cleanup_current ();
439 Server_Interceptor::send_other (PortableInterceptor::ServerRequestInfo_ptr ri
)
441 if (TAO_debug_level
> 0)
443 TAOLIB_DEBUG ((LM_DEBUG
,
444 "Server_Interceptor::send_other\n"));
447 TAO_RTScheduler_Current_i
*current
= 0;
449 TAO_TSS_Resources
*tss
= TAO_TSS_Resources::instance ();
451 current
= static_cast<TAO_RTScheduler_Current_i
*> (tss
->rtscheduler_current_impl_
);
454 // Inform scheduler that upcall is complete.
455 RTScheduling::Scheduler_var scheduler
= current
->scheduler ();
456 scheduler
->send_other (ri
);
458 current
->cleanup_DT ();
459 current
->cleanup_current ();
464 Server_Interceptor::name ()
466 return CORBA::string_dup ("RTSchdeuler_Server_Interceptor");
470 Server_Interceptor::destroy ()
474 TAO_END_VERSIONED_NAMESPACE_DECL