Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / RTCORBA / RT_Protocols_Hooks.cpp
blob84a8a961326186b2307557f6765f95f72301fcb2
1 #include "tao/RTCORBA/RT_Protocols_Hooks.h"
3 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
5 #include "tao/RTCORBA/RT_Policy_i.h"
6 #include "tao/RTCORBA/Priority_Mapping_Manager.h"
7 #include "tao/RTCORBA/RT_Stub.h"
9 #include "tao/Stub.h"
10 #include "tao/ORB_Core.h"
11 #include "tao/MProfile.h"
12 #include "tao/Service_Context.h"
13 #include "tao/Acceptor_Registry.h"
14 #include "tao/Thread_Lane_Resources.h"
15 #include "tao/Protocol_Factory.h"
16 #include "tao/Transport_Acceptor.h"
17 #include "tao/Transport_Connector.h"
18 #include "tao/TSS_Resources.h"
19 #include "tao/Policy_Set.h"
20 #include "tao/debug.h"
21 #include "tao/CDR.h"
22 #include "tao/SystemException.h"
24 #include "ace/Dynamic_Service.h"
25 #include "ace/OS_NS_string.h"
27 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
29 TAO_RT_Protocols_Hooks::TAO_RT_Protocols_Hooks ()
30 : orb_core_ (0)
31 , current_ ()
36 TAO_RT_Protocols_Hooks::~TAO_RT_Protocols_Hooks ()
40 void
41 TAO_RT_Protocols_Hooks::init_hooks (TAO_ORB_Core *orb_core)
43 this->orb_core_ = orb_core;
45 // Save a reference to the priority mapping manager.
46 CORBA::Object_var obj =
47 orb_core->object_ref_table ().resolve_initial_reference (
48 TAO_OBJID_PRIORITYMAPPINGMANAGER);
50 this->mapping_manager_ =
51 TAO_Priority_Mapping_Manager::_narrow (obj.in ());
53 // Save a reference to the priority mapping manager.
54 CORBA::Object_var object =
55 orb_core->object_ref_table ().resolve_initial_reference (
56 TAO_OBJID_NETWORKPRIORITYMAPPINGMANAGER);
58 this->network_mapping_manager_ =
59 TAO_Network_Priority_Mapping_Manager::_narrow (object.in ());
61 object =
62 this->orb_core_->object_ref_table ().resolve_initial_reference (
63 TAO_OBJID_RTCURRENT);
65 this->current_ = RTCORBA::Current::_narrow (object.in ());
68 RTCORBA::ProtocolProperties_ptr
69 TAO_RT_Protocols_Hooks::server_protocol_properties (IOP::ProfileId protocol_tag,
70 CORBA::Policy_ptr policy)
72 if (CORBA::is_nil (policy))
73 return RTCORBA::ProtocolProperties::_nil ();
75 RTCORBA::ServerProtocolPolicy_var server_protocol_policy =
76 RTCORBA::ServerProtocolPolicy::_narrow (policy);
78 if (CORBA::is_nil (server_protocol_policy.in ()))
79 return RTCORBA::ProtocolProperties::_nil ();
81 TAO_ServerProtocolPolicy *server_protocols =
82 dynamic_cast<TAO_ServerProtocolPolicy *> (server_protocol_policy.in ());
84 if (server_protocols == 0)
85 return RTCORBA::ProtocolProperties::_nil ();
87 // TAO_ServerProtocolPolicy
88 RTCORBA::ProtocolList &protocols =
89 server_protocols->protocols_rep ();
91 for (CORBA::ULong j = 0; j < protocols.length (); ++j)
93 if (protocols[j].protocol_type == protocol_tag)
95 return RTCORBA::ProtocolProperties::_duplicate (
96 protocols[j].transport_protocol_properties.in ());
100 return RTCORBA::ProtocolProperties::_nil ();
103 RTCORBA::ProtocolProperties_ptr
104 TAO_RT_Protocols_Hooks::client_protocol_properties (IOP::ProfileId protocol_tag,
105 CORBA::Policy_ptr policy)
107 if (CORBA::is_nil (policy))
108 return RTCORBA::ProtocolProperties::_nil ();
110 RTCORBA::ClientProtocolPolicy_var client_protocol_policy =
111 RTCORBA::ClientProtocolPolicy::_narrow (policy);
113 if (CORBA::is_nil (client_protocol_policy.in ()))
114 return RTCORBA::ProtocolProperties::_nil ();
116 TAO_ClientProtocolPolicy *client_protocols =
117 dynamic_cast<TAO_ClientProtocolPolicy *> (client_protocol_policy.in ());
119 if (client_protocols == 0)
120 return RTCORBA::ProtocolProperties::_nil ();
122 // TAO_ClientProtocolPolicy
123 RTCORBA::ProtocolList &protocols =
124 client_protocols->protocols_rep ();
126 for (CORBA::ULong j = 0; j < protocols.length (); ++j)
128 if (protocols[j].protocol_type == protocol_tag)
130 return RTCORBA::ProtocolProperties::_duplicate (
131 protocols[j].transport_protocol_properties.in ());
135 return RTCORBA::ProtocolProperties::_nil ();
138 RTCORBA::ProtocolProperties_ptr
139 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (
140 IOP::ProfileId protocol_tag)
142 CORBA::Policy_var policy =
143 this->orb_core_->get_cached_policy (TAO_CACHED_POLICY_RT_SERVER_PROTOCOL);
145 return this->server_protocol_properties (protocol_tag, policy.in ());
148 RTCORBA::ProtocolProperties_ptr
149 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (
150 IOP::ProfileId protocol_tag)
152 CORBA::Policy_var policy =
153 this->orb_core_->get_cached_policy (TAO_CACHED_POLICY_RT_CLIENT_PROTOCOL);
155 return this->client_protocol_properties (protocol_tag, policy.in ());
158 RTCORBA::ProtocolProperties_ptr
159 TAO_RT_Protocols_Hooks::client_protocol_properties_at_object_level (
160 IOP::ProfileId protocol_tag,
161 TAO_Stub *stub)
163 CORBA::Policy_var policy =
164 stub->get_cached_policy (TAO_CACHED_POLICY_RT_CLIENT_PROTOCOL);
166 return this->client_protocol_properties (protocol_tag, policy.in ());
169 void
170 TAO_RT_Protocols_Hooks::extract_protocol_properties (TAO_IIOP_Protocol_Properties &to,
171 RTCORBA::ProtocolProperties_ptr from)
173 RTCORBA::TCPProtocolProperties_var protocol_properties =
174 RTCORBA::TCPProtocolProperties::_narrow (from);
176 to.send_buffer_size_ = protocol_properties->send_buffer_size ();
177 to.recv_buffer_size_ = protocol_properties->recv_buffer_size ();
178 to.keep_alive_ = protocol_properties->keep_alive ();
179 to.dont_route_ = protocol_properties->dont_route ();
180 to.no_delay_ = protocol_properties->no_delay ();
181 to.enable_network_priority_ = protocol_properties->enable_network_priority ();
184 void
185 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (TAO_IIOP_Protocol_Properties &to)
187 RTCORBA::ProtocolProperties_var from =
188 this->server_protocol_properties_at_orb_level (IOP::TAG_INTERNET_IOP);
190 if (!CORBA::is_nil (from.in ()))
191 this->extract_protocol_properties (to, from.in ());
194 void
195 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (TAO_IIOP_Protocol_Properties &to)
197 RTCORBA::ProtocolProperties_var from =
198 this->client_protocol_properties_at_orb_level (IOP::TAG_INTERNET_IOP);
200 if (!CORBA::is_nil (from.in ()))
201 this->extract_protocol_properties (to, from.in ());
204 void
205 TAO_RT_Protocols_Hooks::extract_protocol_properties (
206 TAO_UIOP_Protocol_Properties &to,
207 RTCORBA::ProtocolProperties_ptr from)
209 RTCORBA::UnixDomainProtocolProperties_var protocol_properties =
210 RTCORBA::UnixDomainProtocolProperties::_narrow (from);
212 to.send_buffer_size_ = protocol_properties->send_buffer_size ();
213 to.recv_buffer_size_ = protocol_properties->recv_buffer_size ();
216 void
217 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (
218 TAO_UIOP_Protocol_Properties &to)
220 RTCORBA::ProtocolProperties_var from =
221 this->server_protocol_properties_at_orb_level (TAO_TAG_UIOP_PROFILE);
223 if (!CORBA::is_nil (from.in ()))
224 this->extract_protocol_properties (to, from.in ());
227 void
228 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (
229 TAO_UIOP_Protocol_Properties &to)
231 RTCORBA::ProtocolProperties_var from =
232 this->client_protocol_properties_at_orb_level (TAO_TAG_UIOP_PROFILE);
234 if (!CORBA::is_nil (from.in ()))
235 this->extract_protocol_properties (to, from.in ());
238 void
239 TAO_RT_Protocols_Hooks::extract_protocol_properties (
240 TAO_SHMIOP_Protocol_Properties &to,
241 RTCORBA::ProtocolProperties_ptr from)
243 RTCORBA::SharedMemoryProtocolProperties_var protocol_properties =
244 RTCORBA::SharedMemoryProtocolProperties::_narrow (from);
246 to.preallocate_buffer_size_ = protocol_properties->preallocate_buffer_size ();
247 to.mmap_filename_ = protocol_properties->mmap_filename ();
248 to.mmap_lockname_ = protocol_properties->mmap_lockname ();
251 void
252 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (
253 TAO_SHMIOP_Protocol_Properties &to)
255 RTCORBA::ProtocolProperties_var from =
256 this->server_protocol_properties_at_orb_level (TAO_TAG_SHMEM_PROFILE);
258 if (!CORBA::is_nil (from.in ()))
259 this->extract_protocol_properties (to, from.in ());
262 void
263 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (
264 TAO_SHMIOP_Protocol_Properties &to)
266 RTCORBA::ProtocolProperties_var from =
267 this->client_protocol_properties_at_orb_level (TAO_TAG_SHMEM_PROFILE);
269 if (!CORBA::is_nil (from.in ()))
270 this->extract_protocol_properties (to, from.in ());
273 void
274 TAO_RT_Protocols_Hooks::extract_protocol_properties (
275 TAO_DIOP_Protocol_Properties &to,
276 RTCORBA::ProtocolProperties_ptr from)
278 RTCORBA::UserDatagramProtocolProperties_var protocol_properties =
279 RTCORBA::UserDatagramProtocolProperties::_narrow (from);
281 to.enable_network_priority_ = protocol_properties->enable_network_priority ();
282 to.send_buffer_size_ = protocol_properties->send_buffer_size ();
283 to.recv_buffer_size_ = protocol_properties->recv_buffer_size ();
286 void
287 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (
288 TAO_DIOP_Protocol_Properties &to)
290 RTCORBA::ProtocolProperties_var from =
291 this->server_protocol_properties_at_orb_level (TAO_TAG_DIOP_PROFILE);
293 if (!CORBA::is_nil (from.in ()))
294 this->extract_protocol_properties (to, from.in ());
297 void
298 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (
299 TAO_DIOP_Protocol_Properties &to)
301 RTCORBA::ProtocolProperties_var from =
302 this->client_protocol_properties_at_orb_level (TAO_TAG_DIOP_PROFILE);
304 if (!CORBA::is_nil (from.in ()))
305 this->extract_protocol_properties (to, from.in ());
308 void
309 TAO_RT_Protocols_Hooks::extract_protocol_properties (
310 TAO_SCIOP_Protocol_Properties &to,
311 RTCORBA::ProtocolProperties_ptr from)
313 RTCORBA::StreamControlProtocolProperties_var protocol_properties =
314 RTCORBA::StreamControlProtocolProperties::_narrow (from);
316 to.send_buffer_size_ = protocol_properties->send_buffer_size ();
317 to.recv_buffer_size_ = protocol_properties->recv_buffer_size ();
318 to.keep_alive_ = protocol_properties->keep_alive ();
319 to.dont_route_ = protocol_properties->dont_route ();
320 to.no_delay_ = protocol_properties->no_delay ();
321 to.enable_network_priority_ = protocol_properties->enable_network_priority ();
324 void
325 TAO_RT_Protocols_Hooks::server_protocol_properties_at_orb_level (
326 TAO_SCIOP_Protocol_Properties &to)
328 RTCORBA::ProtocolProperties_var from =
329 this->server_protocol_properties_at_orb_level (TAO_TAG_SCIOP_PROFILE);
331 if (!CORBA::is_nil (from.in ()))
332 this->extract_protocol_properties (to, from.in ());
335 void
336 TAO_RT_Protocols_Hooks::client_protocol_properties_at_orb_level (
337 TAO_SCIOP_Protocol_Properties &to)
339 RTCORBA::ProtocolProperties_var from =
340 this->client_protocol_properties_at_orb_level (TAO_TAG_SCIOP_PROFILE);
342 if (!CORBA::is_nil (from.in ()))
343 this->extract_protocol_properties (to, from.in ());
346 CORBA::Boolean
347 TAO_RT_Protocols_Hooks::set_network_priority (
348 IOP::ProfileId protocol_tag,
349 RTCORBA::ProtocolProperties_ptr protocol_properties)
351 if (CORBA::is_nil (protocol_properties))
352 return false;
354 if (protocol_tag == IOP::TAG_INTERNET_IOP)
356 RTCORBA::TCPProtocolProperties_var tcp_protocol_properties =
357 RTCORBA::TCPProtocolProperties::_narrow (protocol_properties);
359 return tcp_protocol_properties->enable_network_priority ();
362 if (protocol_tag == TAO_TAG_DIOP_PROFILE)
364 RTCORBA::UserDatagramProtocolProperties_var udp_protocol_properties =
365 RTCORBA::UserDatagramProtocolProperties::_narrow (protocol_properties);
367 return udp_protocol_properties->enable_network_priority ();
370 if (protocol_tag == TAO_TAG_SCIOP_PROFILE)
372 RTCORBA::StreamControlProtocolProperties_var sctp_protocol_properties =
373 RTCORBA::StreamControlProtocolProperties::_narrow (protocol_properties);
375 return sctp_protocol_properties->enable_network_priority ();
378 return false;
381 CORBA::Boolean
382 TAO_RT_Protocols_Hooks::set_client_network_priority (IOP::ProfileId protocol_tag,
383 TAO_Stub *stub)
385 if (protocol_tag != IOP::TAG_INTERNET_IOP &&
386 protocol_tag != TAO_TAG_DIOP_PROFILE &&
387 protocol_tag != TAO_TAG_SCIOP_PROFILE)
388 return false;
390 RTCORBA::ProtocolProperties_var protocol_properties =
391 this->client_protocol_properties_at_object_level (protocol_tag, stub);
393 return this->set_network_priority (protocol_tag, protocol_properties.in ());
396 CORBA::Boolean
397 TAO_RT_Protocols_Hooks::set_server_network_priority (IOP::ProfileId protocol_tag,
398 CORBA::Policy *policy)
400 if (protocol_tag != IOP::TAG_INTERNET_IOP &&
401 protocol_tag != TAO_TAG_DIOP_PROFILE &&
402 protocol_tag != TAO_TAG_SCIOP_PROFILE)
403 return false;
405 RTCORBA::ProtocolProperties_var protocol_properties =
406 this->server_protocol_properties (protocol_tag, policy);
408 return this->set_network_priority (protocol_tag, protocol_properties.in ());
411 CORBA::Long
412 TAO_RT_Protocols_Hooks::get_dscp_codepoint ()
414 if (TAO_debug_level)
415 TAOLIB_DEBUG ((LM_DEBUG,
416 "TAO_RT_Protocols_Hooks::get_dscp_codepoint\n"));
418 CORBA::Long codepoint = 0;
422 // Make several invocation, changing the priority of this thread
423 // for each.
425 RTCORBA::NetworkPriorityMapping *pm =
426 this->network_mapping_manager_->mapping ();
428 CORBA::Short const priority = this->current_->the_priority ();
430 if (pm->to_network (priority, codepoint) == 0)
432 if (TAO_debug_level > 0)
434 TAOLIB_ERROR ((LM_ERROR,
435 "Cannot convert corba priority %d "
436 "to network priority\n",
437 priority));
440 return -1;
443 catch (const ::CORBA::Exception& ex)
445 if (TAO_debug_level > 0)
447 ex._tao_print_exception (
448 "TAO_RT_Protocols_Hooks::get_dscp_codepoint");
451 return -1;
454 return codepoint;
457 void
458 TAO_RT_Protocols_Hooks::get_selector_hook (
459 CORBA::Policy *model_policy,
460 CORBA::Boolean &is_client_propagated,
461 CORBA::Short &server_priority)
463 RTCORBA::PriorityModelPolicy_var model_policy_ptr =
464 RTCORBA::PriorityModelPolicy::_narrow (model_policy);
466 TAO_PriorityModelPolicy *priority_model_policy =
467 static_cast<TAO_PriorityModelPolicy *> (model_policy_ptr.in ());
469 if (priority_model_policy->get_priority_model () == RTCORBA::CLIENT_PROPAGATED)
471 is_client_propagated = true;
474 if (!is_client_propagated)
476 server_priority = priority_model_policy->server_priority ();
479 return;
482 void
483 TAO_RT_Protocols_Hooks::get_selector_bands_policy_hook (
484 CORBA::Policy *bands_policy,
485 CORBA::Short priority,
486 CORBA::Short &min_priority,
487 CORBA::Short &max_priority,
488 bool &in_range)
490 RTCORBA::PriorityBandedConnectionPolicy_var bands_policy_ptr =
491 RTCORBA::PriorityBandedConnectionPolicy::_narrow (bands_policy);
493 TAO_PriorityBandedConnectionPolicy *priority_bands_policy =
494 static_cast<TAO_PriorityBandedConnectionPolicy *> (bands_policy_ptr.in ());
496 // Find the band with the range covering our target priority.
497 RTCORBA::PriorityBands &bands =
498 priority_bands_policy->priority_bands_rep ();
500 for (CORBA::ULong i = 0; i < bands.length (); ++i)
502 if (bands[i].low <= priority && bands[i].high >= priority)
504 min_priority = bands[i].low;
505 max_priority = bands[i].high;
507 in_range = true;
508 break;
514 TAO_RT_Protocols_Hooks::get_thread_CORBA_priority (CORBA::Short &priority)
516 priority = TAO_TSS_Resources::instance ()->rtcorba_current_priority_;
518 return (priority == -1 ? -1 : 0);
522 TAO_RT_Protocols_Hooks::get_thread_native_priority (
523 CORBA::Short &native_priority)
525 ACE_hthread_t current;
526 ACE_Thread::self (current);
528 int priority;
530 if (ACE_Thread::getprio (current, priority) == -1)
532 TAOLIB_DEBUG ((LM_DEBUG,
533 ACE_TEXT ("TAO (%P|%t) - ")
534 ACE_TEXT ("RT_Protocols_Hooks::get_thread_native_priority: ")
535 ACE_TEXT (" ACE_Thread::get_prio failed\n")));
537 return -1;
540 native_priority = CORBA::Short (priority);
541 return 0;
545 TAO_RT_Protocols_Hooks::get_thread_CORBA_and_native_priority (
546 CORBA::Short &priority,
547 CORBA::Short &native_priority)
549 if (this->get_thread_native_priority (native_priority) == -1)
551 return -1;
554 if (this->get_thread_CORBA_priority (priority) == -1)
556 // This thread had an unset CORBA priority - as this method
557 // should only be used to get and restore the state of play before and
558 // after invocations that might be OK for all I know. Or care frankly.
559 priority = -1;
562 return 0;
566 TAO_RT_Protocols_Hooks::get_thread_implicit_CORBA_priority (CORBA::Short &priority)
568 TAO_Priority_Mapping *pm =
569 this->mapping_manager_.in ()->mapping ();
571 CORBA::Short native_priority = 0;
572 if (this->get_thread_native_priority (native_priority) == 0 &&
573 pm->to_CORBA (native_priority, priority))
575 return 0;
578 return -1;
582 TAO_RT_Protocols_Hooks::restore_thread_CORBA_and_native_priority (
583 CORBA::Short priority,
584 CORBA::Short native_priority)
586 // Only used for restoration of values that were previously set /
587 // checked when so no need to re-apply the mapping.
588 int result = this->set_thread_native_priority (native_priority);
590 if (result == 0)
591 TAO_TSS_Resources::instance ()->rtcorba_current_priority_ = priority;
593 return result;
597 TAO_RT_Protocols_Hooks::set_thread_CORBA_priority (CORBA::Short priority)
599 TAO_Priority_Mapping *priority_mapping =
600 this->mapping_manager_.in ()->mapping ();
602 CORBA::Short native_priority;
604 if (priority_mapping->to_native (priority, native_priority) == 0)
606 return -1;
609 return this->restore_thread_CORBA_and_native_priority (priority,
610 native_priority);
614 TAO_RT_Protocols_Hooks::set_thread_native_priority (
615 CORBA::Short native_priority)
617 ACE_hthread_t current;
618 ACE_Thread::self (current);
620 if (ACE_Thread::setprio (current, native_priority) == -1)
622 TAOLIB_ERROR_RETURN ((LM_ERROR,
623 ACE_TEXT ("(%N,%l) Error setting thread ")
624 ACE_TEXT ("priority to %d, errno %d %m\n"),
625 native_priority,
626 ACE_ERRNO_GET),
627 -1);
630 return 0;
634 ACE_STATIC_SVC_DEFINE (TAO_RT_Protocols_Hooks,
635 ACE_TEXT ("RT_Protocols_Hooks"),
636 ACE_SVC_OBJ_T,
637 &ACE_SVC_NAME (TAO_RT_Protocols_Hooks),
638 ACE_Service_Type::DELETE_THIS
639 | ACE_Service_Type::DELETE_OBJ,
641 ACE_FACTORY_DEFINE (TAO_RTCORBA, TAO_RT_Protocols_Hooks)
643 TAO_END_VERSIONED_NAMESPACE_DECL
645 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */