summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/impl/DomainParticipantManager.h
blob: c92bf467098e338d77a35c0e85506c98fbc43135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * @author William R. Otte <wotte@dre.vanderbilt.edu>
 * @author Johnny Willemsen (jwillemsen@remedy.nl)
 *
 */

#ifndef DOMAINPARTICIPANTMANAGER_H_
#define DOMAINPARTICIPANTMANAGER_H_

#include "dds4ccm/impl/dds4ccm_dds_impl_export.h"

#include "ace/Singleton.h"
#include "tao/orbconf.h"

#include "dds4ccm/idl/dds_rtf2_dcpsC.h"

#include <map>
#include <string>

namespace CIAO
{
  namespace DDS4CCM
  {
    /**
     *
     * @class DomainParticipantManager
     *
     * @brief
     *
     * Stores a DDSParticipantTopic per domain ID and QOS profile string.
     * When several connectors are joining the same domain ID, using the
     * same QOS settings (ie have the same QOS profile string),
     * the DomainParticipant for that combination is shared. This is done
     * to save resources (running threads/memory usage).
     *
     * The DomainParticipantManager is a singleton.
     *
     */
    class DDS4CCM_DDS_IMPL_Export DomainParticipantManager : private ACE_Copy_Disabled
    {
    friend class ACE_Singleton<DomainParticipantManager, TAO_SYNCH_MUTEX>;

    /**
     * @class DDSParticipantTopic
     *
     * Stores a list of topics for a specific domain. If
     * several connectors run in the same process and those
     * connectors are making use of the same topic, the topics
     * are shared amongst the connectors.
     *
     * This class maintains a reference count. It's save to remove
     * a topic once the reference count becomes one.
     *
     */
    class DDSParticipantTopic
    {
      public:
        DDSParticipantTopic (DDS::DomainParticipant_ptr dp);
        ~DDSParticipantTopic (void);

        DDS::DomainParticipant_ptr get_participant ();

        /**
         * Returns the reference count of this class
         */
        int _ref_count ();
        /**
         * Increments the reference count of this class
         */
        void _inc_ref ();
        /**
         * Decrements the reference count of this class
         */
        void _dec_ref ();

      private:
        int ref_count_;
        DDS::DomainParticipant_var dp_;
    };

    private:
      /// Constructor
      DomainParticipantManager (void);

    public:
      /// Destructor
      ~DomainParticipantManager (void);

      /**
       * Searches for the DomainParticipant_ptr in the internal map.
       * Search is based on the given domain ID and the given QOS
       * (QOS profile string). If found, it'll increment the
       * reference count of the DDSParticipantTopic instance.
       */
      DDS::DomainParticipant_ptr get_participant (const DDS::DomainId_t domain_id,
                                                  const char * qos_profile);

      /**
       * Adding a DDSParticipantTopic instance when the
       * internal maps doesn't contain a reference. Returns
       * false if there's already an DDSParticipantTopic
       * available (base on domain ID and QOS)
       */
      bool register_participant (DDS::DomainId_t domain_id,
                                 const char * qos_profile,
                                 DDS::DomainParticipant_ptr dp);

      /**
       * Removes the DDSParticipantTopic instance when the
       * reference count is one.
       * Returns false if the reference count of the corresponding
       * DDSParticipantTopic was not nil
       */
      bool unregister_participant (DDS::DomainId_t domain_id,
                                 const char * qos_profile,
                                 DDS::DomainParticipant_ptr dp);

      /**
       * Try to close the DPM, at the moment no domain participants are
       * registered anymore we are going to shutdown DDS interaction
       */
      bool close();

    private:
      TAO_SYNCH_MUTEX dps_mutex_;

      typedef std::pair <std::string, DDS::DomainId_t> IdQosProfile;
      typedef std::map < IdQosProfile, DDSParticipantTopic *> DomainParticipants;
      DomainParticipants dps_;

      typedef DomainParticipants::iterator DomainParticipants_iterator;
    };

    typedef ACE_Singleton<DomainParticipantManager,
              TAO_SYNCH_MUTEX> Domain_Participant_Manager;
  }
}

#define DPMANAGER ::CIAO::DDS4CCM::Domain_Participant_Manager::instance ()

/// Declare a process wide singleton
DDS4CCM_DDS_IMPL_SINGLETON_DECLARE (ACE_Singleton,
                                    ::CIAO::DDS4CCM::DomainParticipantManager,
                                    TAO_SYNCH_MUTEX)

#endif