Line data Source code
1 : /* 2 : * 3 : * 4 : * Distributed under the OpenDDS License. 5 : * See: http://www.opendds.org/license.html 6 : */ 7 : 8 : #ifndef OPENDDS_DCPS_OWNERSHIP_MANAGER_H 9 : #define OPENDDS_DCPS_OWNERSHIP_MANAGER_H 10 : 11 : #ifndef OPENDDS_NO_OWNERSHIP_KIND_EXCLUSIVE 12 : 13 : #include "Definitions.h" 14 : #include "GuidUtils.h" 15 : #include "InstanceState.h" 16 : #include "dds/DdsDcpsInfrastructureC.h" 17 : #include "PoolAllocator.h" 18 : #include "RcObject.h" 19 : 20 : #if !defined (ACE_LACKS_PRAGMA_ONCE) 21 : #pragma once 22 : #endif /* ACE_LACKS_PRAGMA_ONCE */ 23 : 24 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 25 : 26 : namespace OpenDDS { 27 : namespace DCPS { 28 : 29 : class DataReaderImpl; 30 : 31 : class OpenDDS_Dcps_Export OwnershipManager { 32 : public: 33 : typedef OPENDDS_SET(DataReaderImpl*) ReaderSet; 34 : 35 : // TypeInstanceMap is only used for EXCLUSIVE ownership. 36 : struct InstanceMap { 37 0 : InstanceMap() {} 38 0 : InstanceMap(const RcHandle<RcObject>& map, DataReaderImpl* reader) 39 0 : : map_(map) 40 : { 41 0 : readers_.insert(reader); 42 0 : } 43 : 44 : RcHandle<RcObject> map_; 45 : ReaderSet readers_; 46 : }; 47 : 48 : typedef OPENDDS_MAP(OPENDDS_STRING, InstanceMap) TypeInstanceMap; 49 : 50 : struct WriterInfo { 51 0 : WriterInfo(const GUID_t& pub_id, 52 : const CORBA::Long& ownership_strength) 53 0 : : pub_id_(pub_id) 54 0 : , ownership_strength_(ownership_strength) 55 0 : {} 56 : 57 0 : WriterInfo() 58 0 : : pub_id_(GUID_UNKNOWN) 59 0 : , ownership_strength_(0) 60 0 : {} 61 : 62 : GUID_t pub_id_; 63 : CORBA::Long ownership_strength_; 64 : }; 65 : 66 : typedef OPENDDS_VECTOR(WriterInfo) WriterInfos; 67 : typedef OPENDDS_VECTOR(InstanceState_rch) InstanceStateVec; 68 : 69 : struct OwnershipWriterInfos { 70 : WriterInfo owner_; 71 : WriterInfos candidates_; 72 : InstanceStateVec instance_states_; 73 : }; 74 : 75 : typedef OPENDDS_MAP(DDS::InstanceHandle_t, OwnershipWriterInfos) 76 : InstanceOwnershipWriterInfos; 77 : 78 : OwnershipManager(); 79 : ~OwnershipManager(); 80 : 81 : /** 82 : * Acquire/release lock for type instance map. 83 : * The following functions are synchronized by instance_lock_. 84 : */ 85 : int instance_lock_acquire(); 86 : int instance_lock_release(); 87 : 88 : /** 89 : * The instance map per type is created by the concrete datareader 90 : * when first sample with the type is received. 91 : */ 92 : void set_instance_map(const char* type_name, 93 : const RcHandle<RcObject>& instance_map, 94 : DataReaderImpl* reader); 95 : 96 : /** 97 : * Accesor of the instance map for provided type. It is called once 98 : * for each new instance in a datareader. 99 : */ 100 : RcHandle<RcObject> get_instance_map(const char* type_name, DataReaderImpl* reader); 101 : 102 : /** 103 : * The readers that access the instance map are keep tracked as ref 104 : * counting to the instance map. The readers need unregister itself 105 : * with the instance map upon unregistering instance.The instance map 106 : * is deleted upon the last reader unregistering an instance of the 107 : * type. 108 : */ 109 : void unregister_reader(const char* type_name, 110 : DataReaderImpl* reader); 111 : 112 : /** 113 : * Remove a writer from all instances ownership collection. 114 : */ 115 : void remove_writer(const GUID_t& pub_id); 116 : 117 : /** 118 : * Remove all writers that write to the specified instance. 119 : */ 120 : void remove_writers(const DDS::InstanceHandle_t& instance_handle); 121 : 122 : /** 123 : * Remove a writer that write to the specified instance. 124 : * Return true if it's the owner writer removed. 125 : */ 126 : bool remove_writer(const DDS::InstanceHandle_t& instance_handle, 127 : const GUID_t& pub_id); 128 : 129 : /** 130 : * Return true if the provide writer is the owner of the instance. 131 : */ 132 : bool is_owner(const DDS::InstanceHandle_t& instance_handle, 133 : const GUID_t& pub_id); 134 : 135 : /** 136 : * Determine if the provided publication can be the owner. 137 : */ 138 : bool select_owner(const DDS::InstanceHandle_t& instance_handle, 139 : const GUID_t& pub_id, 140 : const CORBA::Long& ownership_strength, 141 : InstanceState_rch instance_state); 142 : 143 : /** 144 : * Remove an owner of the specified instance. 145 : */ 146 : void remove_owner(const DDS::InstanceHandle_t& instance_handle); 147 : 148 : void remove_instance(InstanceState* instance_state); 149 : 150 : /** 151 : * Update the ownership strength of a publication. 152 : */ 153 : void update_ownership_strength(const GUID_t& pub_id, 154 : const CORBA::Long& ownership_strength); 155 : 156 : private: 157 : 158 : bool remove_writer(const DDS::InstanceHandle_t& instance_handle, 159 : OwnershipWriterInfos& infos, 160 : const GUID_t& pub_id); 161 : 162 : void remove_owner(const DDS::InstanceHandle_t& instance_handle, 163 : OwnershipWriterInfos& infos, 164 : bool sort); 165 : 166 : void remove_candidate(OwnershipWriterInfos& infos, 167 : const GUID_t& pub_id); 168 : 169 : void broadcast_new_owner(const DDS::InstanceHandle_t& instance_handle, 170 : OwnershipWriterInfos& infos, 171 : const GUID_t& owner); 172 : 173 : ACE_Thread_Mutex instance_lock_; 174 : TypeInstanceMap type_instance_map_; 175 : InstanceOwnershipWriterInfos instance_ownership_infos_; 176 : 177 : }; 178 : 179 : } // namespace DCPS 180 : } // namespace OpenDDS 181 : 182 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 183 : 184 : #endif /* OPENDDS_NO_OWNERSHIP_KIND_EXCLUSIVE */ 185 : 186 : #endif /* OPENDDS_DCPS_OWNERSHIP_MANAGER_H */