2 * @file ConnectionCache.h
4 * @author Martin Corino <mcorino@remedy.nl>
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
26 * @class ACE_INet_ConnectionKey
28 * @brief Base class for connection keys.
31 class ACE_INET_Export 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;
46 virtual bool equal (const ConnectionKey
& key
) const = 0;
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
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;
73 const ConnectionKey
& key () const;
81 * @class ACE_INet_ConnectionHolder
83 * @brief Generic base for connection wrappers.
86 class ACE_INET_Export ConnectionHolder
89 virtual ~ConnectionHolder ();
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
104 class ACE_INET_Export ConnectionFactory
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
124 class ConnectionCacheValue
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
);
160 connection_type
* connection_
;
165 * @class ACE_INet_ConnectionCache
167 * @brief Implements a cache for INet connection objects.
170 class ACE_INET_Export ConnectionCache
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
;
188 ConnectionCache(size_t size
= ACE_DEFAULT_MAP_SIZE
);
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
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
,
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
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;
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_
;
259 ACE_END_VERSIONED_NAMESPACE_DECL
261 #if defined (__ACE_INLINE__)
262 #include "ace/INet/ConnectionCache.inl"
265 #include /**/ "ace/post.h"
266 #endif /* ACE_INET_CONNECTION_CACHE_H */