#include <ConnectionCache.h>
Public Types | |
typedef ConnectionHolder | connection_type |
typedef ConnectionFactory | factory_type |
typedef ACE_Hash_Map_Manager_Ex < ConnectionCacheKey, ConnectionCacheValue, ACE_Hash < ConnectionCacheKey > , ACE_Equal_To < ConnectionCacheKey > , ACE_SYNCH_NULL_MUTEX > | map_type |
typedef map_type::iterator | map_iter_type |
typedef ACE_Hash_Map_Entry < ConnectionCacheKey, ConnectionCacheValue > | map_entry_type |
Public Member Functions | |
ConnectionCache (size_t size=ACE_DEFAULT_MAP_SIZE) | |
Constructor. | |
~ConnectionCache () | |
Destructor. | |
bool | claim_connection (const ConnectionKey &key, connection_type *&connection, const factory_type &connection_factory, bool wait=true) |
bool | release_connection (const ConnectionKey &key, connection_type *connection) |
bool | close_connection (const ConnectionKey &key, connection_type *connection) |
bool | has_connection (const ConnectionKey &key) |
void | close_all_connections () |
Unconditionally closes all active connections. | |
size_t | current_size () const |
Returns the number of registered cache entries (including CLOSED). | |
Private Member Functions | |
bool | set_connection (const ConnectionKey &key, const ConnectionCacheValue &cacheval) |
Updates cache entry state. | |
bool | claim_existing_connection (const ConnectionKey &key, connection_type *&connection, ConnectionCacheValue::State &state) |
bool | find_connection (const ConnectionKey &key, ConnectionCacheValue &cacheval) |
Private Attributes | |
ACE_SYNCH_MUTEX | lock_ |
ACE_SYNCH_CONDITION | condition_ |
map_type | cache_map_ |
Definition at line 165 of file ConnectionCache.h.
Definition at line 168 of file ConnectionCache.h.
Definition at line 169 of file ConnectionCache.h.
typedef ACE_Hash_Map_Entry<ConnectionCacheKey, ConnectionCacheValue> ACE::INet::ConnectionCache::map_entry_type |
Definition at line 180 of file ConnectionCache.h.
Definition at line 177 of file ConnectionCache.h.
typedef ACE_Hash_Map_Manager_Ex<ConnectionCacheKey, ConnectionCacheValue, ACE_Hash<ConnectionCacheKey>, ACE_Equal_To<ConnectionCacheKey>, ACE_SYNCH_NULL_MUTEX> ACE::INet::ConnectionCache::map_type |
Definition at line 175 of file ConnectionCache.h.
ACE::INet::ConnectionCache::ConnectionCache | ( | size_t | size = ACE_DEFAULT_MAP_SIZE |
) |
Constructor.
Definition at line 107 of file ConnectionCache.cpp.
: condition_ (lock_), cache_map_ (size) { }
ACE::INet::ConnectionCache::~ConnectionCache | ( | ) |
bool ACE::INet::ConnectionCache::claim_connection | ( | const ConnectionKey & | key, | |
connection_type *& | connection, | |||
const factory_type & | connection_factory, | |||
bool | wait = true | |||
) |
Claim a connection from the cache. Creates a new connection using <connection_factory> if the cache does not contain a matching entry for <key>. If <wait> is true and the state of the matching connection is BUSY the method will block waiting for connection to become available. Returns true if a connection could be successfully claimed and sets <connection> to the claimed connection. Returns false otherwise.
Definition at line 164 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::claim_connection"); while (1) { bool create_connection = false; ConnectionCacheValue::State state = ConnectionCacheValue::CST_NONE; do { ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); if (this->claim_existing_connection (key, connection, state)) { INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("%P|%t) ConnectionCache::claim_connection - ") ACE_TEXT ("successfully claimed existing connection\n"))); return true; } if ((state == ConnectionCacheValue::CST_BUSY || state == ConnectionCacheValue::CST_INIT) && !wait) return false; if (state == ConnectionCacheValue::CST_CLOSED || state == ConnectionCacheValue::CST_NONE) { if (!this->set_connection (key, ConnectionCacheValue ())) { INET_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("ConnectionCache::claim_connection - ") ACE_TEXT ("failed to initialize connection entry"))); return false; } create_connection = true; } else { INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("ConnectionCache::claim_connection - ") ACE_TEXT ("waiting for connection to become available\n"))); // wait for connection to become ready/free if (this->condition_.wait () != 0) { INET_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("(%P|%t) ConnectionCache::claim_connection - ") ACE_TEXT ("error waiting for connection condition (%p)\n"))); return false; } INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("ConnectionCache::claim_connection - ") ACE_TEXT ("awoken and retrying to claim connection\n"))); } } while (0); if (create_connection) { connection = connection_factory.create_connection (key); if (connection) { INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("ConnectionCache::claim_connection - ") ACE_TEXT ("successfully created new connection\n"))); ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); ConnectionCacheValue cacheval (connection); cacheval.state (ConnectionCacheValue::CST_BUSY); return this->set_connection (key, cacheval); } else return false; } } }
bool ACE::INet::ConnectionCache::claim_existing_connection | ( | const ConnectionKey & | key, | |
connection_type *& | connection, | |||
ConnectionCacheValue::State & | state | |||
) | [private] |
Attempts to claim an existing connection. Returns true and sets <connection> if successful. Returns false otherwise. Does not wait when no connection available.
Definition at line 136 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::claim_existing_connection"); ConnectionCacheValue cacheval; if (this->find_connection (key, cacheval)) { state = cacheval.state (); if (state == ConnectionCacheValue::CST_IDLE) { cacheval.state (ConnectionCacheValue::CST_BUSY); if (this->set_connection (key, cacheval)) { connection = cacheval.connection (); return true; } else { INET_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("ConnectionCache::claim_existing_connection - ") ACE_TEXT ("failed to claim connection entry"))); } } } return false; }
void ACE::INet::ConnectionCache::close_all_connections | ( | ) |
Unconditionally closes all active connections.
Definition at line 334 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::close_all_connections"); ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, guard_, this->lock_)); map_iter_type iter = this->cache_map_.end (); for (iter = this->cache_map_.begin (); iter != this->cache_map_.end (); ++iter) { if ((*iter).int_id_.state () == ConnectionCacheValue::CST_CLOSED) { connection_type* conn = (*iter).int_id_.connection (); (*iter).int_id_.connection (0); (*iter).int_id_.state (ConnectionCacheValue::CST_CLOSED); delete conn; } } this->cache_map_.unbind_all (); }
bool ACE::INet::ConnectionCache::close_connection | ( | const ConnectionKey & | key, | |
connection_type * | connection | |||
) |
Close a previously claimed connection. Deletes the actual connection object and marks the cache entry as CLOSED. Returns true is the connection was successfully closed.
Definition at line 281 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::close_connection"); INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("ConnectionCache::close_connection - ") ACE_TEXT ("closing connection\n"))); ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); ConnectionCacheValue cacheval; if (this->find_connection (key, cacheval) && cacheval.connection () == connection && cacheval.state () == ConnectionCacheValue::CST_BUSY) { connection_type* conn = cacheval.connection (); cacheval.connection (0); cacheval.state (ConnectionCacheValue::CST_CLOSED); if (this->set_connection (key, cacheval)) { // signal other threads about closed connection this->condition_.broadcast (); delete conn; // clean up return true; } else { INET_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("ConnectionCache::close_connection - ") ACE_TEXT ("failed to close connection entry"))); return false; } } else return false; }
size_t ACE::INet::ConnectionCache::current_size | ( | void | ) | const [inline] |
Returns the number of registered cache entries (including CLOSED).
Definition at line 86 of file ConnectionCache.inl.
{ ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); return this->cache_map_.current_size (); }
bool ACE::INet::ConnectionCache::find_connection | ( | const ConnectionKey & | key, | |
ConnectionCacheValue & | cacheval | |||
) | [private] |
Looks up a matching cache entry for <key> and updates <cacheval> with the entry state if found. Returns true if found, false otherwise.
Definition at line 118 of file ConnectionCache.cpp.
{ if (this->cache_map_.find (ConnectionCacheKey (key), cacheval) == 0) { return true; } return false; }
bool ACE::INet::ConnectionCache::has_connection | ( | const ConnectionKey & | key | ) |
Returns true if the cache contains a connection matching <key>. Cache entries with state CLOSED are not considered. Returns false otherwise.
Definition at line 320 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::has_connection"); ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); ConnectionCacheValue cacheval; return (this->find_connection (key, cacheval) && cacheval.state () != ConnectionCacheValue::CST_CLOSED); }
bool ACE::INet::ConnectionCache::release_connection | ( | const ConnectionKey & | key, | |
connection_type * | connection | |||
) |
Release a previously claimed connection making it available for renewed claiming. Returns true if the connection was successfully released.
Definition at line 245 of file ConnectionCache.cpp.
{ INET_TRACE ("ConnectionCache::release_connection"); INET_DEBUG (9, (LM_INFO, DLINFO ACE_TEXT ("ConnectionCache::release_connection - ") ACE_TEXT ("releasing connection\n"))); ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, this->lock_, false)); ConnectionCacheValue cacheval; if (this->find_connection (key, cacheval) && cacheval.connection () == connection && cacheval.state () == ConnectionCacheValue::CST_BUSY) { cacheval.state (ConnectionCacheValue::CST_IDLE); if (this->set_connection (key, cacheval)) { // signal other threads about free connection this->condition_.broadcast (); return true; } else { INET_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("ConnectionCache::release_connection - ") ACE_TEXT ("failed to release connection entry"))); return false; } } else return false; }
bool ACE::INet::ConnectionCache::set_connection | ( | const ConnectionKey & | key, | |
const ConnectionCacheValue & | cacheval | |||
) | [private] |
Updates cache entry state.
Definition at line 129 of file ConnectionCache.cpp.
{ return this->cache_map_.rebind (ConnectionCacheKey (key), cacheval) != -1; }
Definition at line 249 of file ConnectionCache.h.
ACE_SYNCH_CONDITION ACE::INet::ConnectionCache::condition_ [private] |
Definition at line 248 of file ConnectionCache.h.
ACE_SYNCH_MUTEX ACE::INet::ConnectionCache::lock_ [mutable, private] |
Definition at line 247 of file ConnectionCache.h.