Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / ace / INet / ConnectionCache.h
blob809503fd0d45415d5036f4981b58cba804abb7d0
1 /**
2 * @file ConnectionCache.h
4 * @author Martin Corino <mcorino@remedy.nl>
5 */
7 #ifndef ACE_INET_CONNECTION_CACHE_H
8 #define ACE_INET_CONNECTION_CACHE_H
10 #include /**/ "ace/pre.h"
12 #include "ace/Synch_Traits.h"
13 #include "ace/Thread_Mutex.h"
14 #include "ace/Condition_Thread_Mutex.h"
15 #include "ace/Null_Mutex.h"
16 #include "ace/Hash_Map_Manager_T.h"
17 #include "ace/INet/INet_Export.h"
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
21 namespace ACE
23 namespace INet
25 /**
26 * @class ACE_INet_ConnectionKey
28 * @brief Base class for connection keys.
31 class ACE_INET_Export ConnectionKey
33 public:
34 ConnectionKey ();
35 virtual ~ConnectionKey ();
37 bool operator ==(const ConnectionKey& key) const;
39 bool operator !=(const ConnectionKey& key) const;
41 virtual u_long hash () const = 0;
43 virtual ConnectionKey* duplicate () const = 0;
45 protected:
46 virtual bool equal (const ConnectionKey& key) const = 0;
49 /**
50 * @class ACE_INet_ConnectionCacheKey
52 * @brief Holder class for connection keys.
54 * Implements non-copying holder object used as ext_id in
55 * connection cache map.
57 class ConnectionCacheKey
59 public:
60 ConnectionCacheKey ();
61 ConnectionCacheKey (const ConnectionKey& key);
62 ConnectionCacheKey (const ConnectionCacheKey& cachekey);
63 virtual ~ConnectionCacheKey ();
65 ConnectionCacheKey& operator =(const ConnectionCacheKey& cachekey);
67 bool operator ==(const ConnectionCacheKey& cachekey) const;
69 bool operator !=(const ConnectionCacheKey& cachekey) const;
71 u_long hash () const;
73 const ConnectionKey& key () const;
75 private:
76 ConnectionKey* key_;
77 bool delete_key_;
80 /**
81 * @class ACE_INet_ConnectionHolder
83 * @brief Generic base for connection wrappers.
86 class ACE_INET_Export ConnectionHolder
88 public:
89 virtual ~ConnectionHolder ();
90 protected:
91 ConnectionHolder ();
95 /**
96 * @class ACE_INet_ConnectionFactory
98 * @brief Base class for connection factories.
100 * Derived classes create specific connections and
101 * return those for caching wrapped in a connection
102 * holder.
104 class ACE_INET_Export ConnectionFactory
106 public:
107 ConnectionFactory ();
108 virtual ~ConnectionFactory ();
110 virtual ConnectionHolder* create_connection (
111 const ConnectionKey& key) const = 0;
116 * @class ACE_INet_ConnectionCacheValue
118 * @brief Holder class for connections.
120 * Implements non-copying holder object maintaining
121 * connection state used as int_id in connection
122 * cache map.
124 class ConnectionCacheValue
126 public:
127 enum State
129 CST_NONE,
130 CST_INIT,
131 CST_IDLE,
132 CST_BUSY,
133 CST_CLOSED
136 typedef ConnectionHolder connection_type;
138 ConnectionCacheValue ();
139 explicit ConnectionCacheValue (connection_type* connection);
140 ConnectionCacheValue (const ConnectionCacheValue& cacheval);
142 ConnectionCacheValue& operator =(const ConnectionCacheValue& cacheval);
144 bool operator == (const ConnectionCacheValue& cacheval) const;
146 bool operator != (const ConnectionCacheValue& cacheval) const;
148 connection_type* connection ();
150 const connection_type* connection () const;
152 void connection (connection_type* conn);
154 State state () const;
156 void state (State st);
158 private:
159 State state_;
160 connection_type* connection_;
165 * @class ACE_INet_ConnectionCache
167 * @brief Implements a cache for INet connection objects.
170 class ACE_INET_Export ConnectionCache
172 public:
173 typedef ConnectionHolder connection_type;
174 typedef ConnectionFactory factory_type;
176 typedef ACE_Hash_Map_Manager_Ex <ConnectionCacheKey,
177 ConnectionCacheValue,
178 ACE_Hash<ConnectionCacheKey>,
179 ACE_Equal_To<ConnectionCacheKey>,
180 ACE_SYNCH_NULL_MUTEX> map_type;
182 typedef map_type::iterator map_iter_type;
184 typedef ACE_Hash_Map_Entry <ConnectionCacheKey,
185 ConnectionCacheValue> map_entry_type;
187 /// Constructor
188 ConnectionCache(size_t size = ACE_DEFAULT_MAP_SIZE);
190 /// Destructor
191 ~ConnectionCache ();
193 /// Claim a connection from the cache.
194 /// Creates a new connection using <connection_factory>
195 /// if the cache does not contain a matching entry for
196 /// <key>.
197 /// If <wait> is true and the state of the matching
198 /// connection is BUSY the method will block waiting for
199 /// connection to become available.
200 /// Returns true if a connection could be successfully
201 /// claimed and sets <connection> to the claimed connection.
202 /// Returns false otherwise.
203 bool claim_connection(const ConnectionKey& key,
204 connection_type*& connection,
205 const factory_type& connection_factory,
206 bool wait = true);
208 /// Release a previously claimed connection making it
209 /// available for renewed claiming.
210 /// Returns true if the connection was successfully released.
211 bool release_connection(const ConnectionKey& key,
212 connection_type* connection);
214 /// Close a previously claimed connection.
215 /// Deletes the actual connection object and marks the cache entry
216 /// as CLOSED.
217 /// Returns true is the connection was successfully closed.
218 bool close_connection(const ConnectionKey& key,
219 connection_type* connection);
221 /// Returns true if the cache contains a connection matching
222 /// <key>. Cache entries with state CLOSED are not considered.
223 /// Returns false otherwise.
224 bool has_connection (const ConnectionKey& key);
226 /// Unconditionally closes all active connections.
227 void close_all_connections ();
229 /// Returns the number of registered cache entries (including CLOSED).
230 size_t current_size () const;
232 private:
233 /// Updates cache entry state
234 bool set_connection (const ConnectionKey& key,
235 const ConnectionCacheValue& cacheval);
237 /// Attempts to claim an existing connection.
238 /// Returns true and sets @a connection if successful.
239 /// Returns false otherwise.
240 /// Does not wait when no connection available.
241 bool claim_existing_connection(const ConnectionKey& key,
242 connection_type*& connection,
243 ConnectionCacheValue::State& state);
245 /// Looks up a matching cache entry for @a key and updates
246 /// <cacheval> with the entry state if found.
247 /// Returns true if found, false otherwise.
248 bool find_connection (const ConnectionKey& key,
249 ConnectionCacheValue& cacheval);
252 mutable ACE_SYNCH_MUTEX lock_;
253 ACE_SYNCH_CONDITION condition_;
254 map_type cache_map_;
259 ACE_END_VERSIONED_NAMESPACE_DECL
261 #if defined (__ACE_INLINE__)
262 #include "ace/INet/ConnectionCache.inl"
263 #endif
265 #include /**/ "ace/post.h"
266 #endif /* ACE_INET_CONNECTION_CACHE_H */