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

Generated on Fri Feb 12 20:05:24 2016 for OpenDDS by  doxygen 1.4.7