OwnershipManager.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef OPENDDS_DCPS_OWNERSHIP_MANAGER_H
00009 #define OPENDDS_DCPS_OWNERSHIP_MANAGER_H
00010 
00011 #ifndef OPENDDS_NO_OWNERSHIP_KIND_EXCLUSIVE
00012 
00013 #include "Definitions.h"
00014 #include "GuidUtils.h"
00015 #include "dds/DdsDcpsInfrastructureC.h"
00016 #include "PoolAllocator.h"
00017 #include "RepoIdTypes.h"
00018 #include "RcObject.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 #pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00025 
00026 namespace OpenDDS {
00027 namespace DCPS {
00028 
00029 class InstanceState;
00030 class DataReaderImpl;
00031 
00032 class OpenDDS_Dcps_Export OwnershipManager {
00033 public:
00034   typedef OPENDDS_SET(DataReaderImpl*) ReaderSet;
00035 
00036   // TypeInstanceMap is only used for EXCLUSIVE ownership.
00037   struct InstanceMap {
00038     InstanceMap()  {}
00039     InstanceMap(const RcHandle<RcObject>& map, DataReaderImpl* reader)
00040       : map_(map)
00041     {
00042       readers_.insert(reader);
00043     }
00044 
00045     RcHandle<RcObject> map_;
00046     ReaderSet readers_;
00047   };
00048 
00049   typedef OPENDDS_MAP(OPENDDS_STRING, InstanceMap) TypeInstanceMap;
00050 
00051   struct WriterInfo {
00052     WriterInfo(const PublicationId& pub_id,
00053                const CORBA::Long& ownership_strength)
00054       : pub_id_(pub_id)
00055       , ownership_strength_(ownership_strength)
00056     {}
00057 
00058     WriterInfo()
00059       : pub_id_(GUID_UNKNOWN)
00060       , ownership_strength_(0)
00061     {}
00062 
00063     PublicationId pub_id_;
00064     CORBA::Long ownership_strength_;
00065   };
00066 
00067   typedef OPENDDS_VECTOR(WriterInfo) WriterInfos;
00068   typedef OPENDDS_VECTOR(InstanceState*) InstanceStateVec;
00069 
00070   struct OwnershipWriterInfos {
00071     WriterInfo owner_;
00072     WriterInfos candidates_;
00073     InstanceStateVec instance_states_;
00074   };
00075 
00076   typedef OPENDDS_MAP(DDS::InstanceHandle_t, OwnershipWriterInfos)
00077     InstanceOwnershipWriterInfos;
00078 
00079   OwnershipManager();
00080   ~OwnershipManager();
00081 
00082   /**
00083   * Acquire/release lock for type instance map.
00084   * The following functions are synchronized by instance_lock_.
00085   */
00086   int instance_lock_acquire();
00087   int instance_lock_release();
00088 
00089   /**
00090   * The instance map per type is created by the concrete datareader
00091   * when first sample with the type is received.
00092   */
00093   void set_instance_map(const char* type_name,
00094                         const RcHandle<RcObject>& instance_map,
00095                         DataReaderImpl* reader);
00096 
00097   /**
00098   * Accesor of the instance map for provided type. It is called once
00099   * for each new instance in a datareader.
00100   */
00101   RcHandle<RcObject> get_instance_map(const char* type_name, DataReaderImpl* reader);
00102 
00103   /**
00104   * The readers that access the instance map are keep tracked as ref
00105   * counting to the instance map. The readers need unregister itself
00106   * with the instance map upon unregistering instance.The instance map
00107   * is deleted upon the last reader unregistering an instance of the
00108   * type.
00109   */
00110   void unregister_reader(const char* type_name,
00111                          DataReaderImpl* reader);
00112 
00113   /**
00114   * Remove a writer from all instances ownership collection.
00115   */
00116   void remove_writer(const PublicationId& pub_id);
00117 
00118   /**
00119   * Remove all writers that write to the specified instance.
00120   */
00121   void remove_writers(const DDS::InstanceHandle_t& instance_handle);
00122 
00123   /**
00124   * Remove a writer that write to the specified instance.
00125   * Return true if it's the owner writer removed.
00126   */
00127   bool remove_writer(const DDS::InstanceHandle_t& instance_handle,
00128                      const PublicationId& pub_id);
00129 
00130   /**
00131   * Return true if the provide writer is the owner of the instance.
00132   */
00133   bool is_owner(const DDS::InstanceHandle_t& instance_handle,
00134                 const PublicationId& pub_id);
00135 
00136   /**
00137   * Determine if the provided publication can be the owner.
00138   */
00139   bool select_owner(const DDS::InstanceHandle_t& instance_handle,
00140                     const PublicationId& pub_id,
00141                     const CORBA::Long& ownership_strength,
00142                     InstanceState* instance_state);
00143 
00144   /**
00145   * Remove an owner of the specified instance.
00146   */
00147   void remove_owner(const DDS::InstanceHandle_t& instance_handle);
00148 
00149   void remove_instance(InstanceState* instance_state);
00150 
00151   /**
00152   * Update the ownership strength of a publication.
00153   */
00154   void update_ownership_strength(const PublicationId& pub_id,
00155                                  const CORBA::Long& ownership_strength);
00156 
00157 private:
00158 
00159   bool remove_writer(const DDS::InstanceHandle_t& instance_handle,
00160                      OwnershipWriterInfos& infos,
00161                      const PublicationId& pub_id);
00162 
00163   void remove_owner(const DDS::InstanceHandle_t& instance_handle,
00164                     OwnershipWriterInfos& infos,
00165                     bool sort);
00166 
00167   void remove_candidate(OwnershipWriterInfos& infos,
00168                         const PublicationId& pub_id);
00169 
00170   void broadcast_new_owner(const DDS::InstanceHandle_t& instance_handle,
00171                            OwnershipWriterInfos& infos,
00172                            const PublicationId& owner);
00173 
00174   ACE_Thread_Mutex instance_lock_;
00175   TypeInstanceMap type_instance_map_;
00176   InstanceOwnershipWriterInfos instance_ownership_infos_;
00177 
00178 };
00179 
00180 } // namespace DCPS
00181 } // namespace OpenDDS
00182 
00183 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00184 
00185 #endif /* OPENDDS_NO_OWNERSHIP_KIND_EXCLUSIVE */
00186 
00187 #endif /* OPENDDS_DCPS_OWNERSHIP_MANAGER_H  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1