Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Types

ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX > Class Template Reference

A connection strategy which caches connections to peers (represented by SVC_HANDLER instances), thereby allowing subsequent re-use of unused, but available, connections. This strategy should be used when the cache is bounded by maximum size. More...

#include <Cached_Connect_Strategy_T.h>

Inheritance diagram for ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >:
Inheritance graph
[legend]
Collaboration diagram for ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ACE_Bounded_Cached_Connect_Strategy (size_t max_size, CACHING_STRATEGY &caching_s, ACE_Creation_Strategy< SVC_HANDLER > *cre_s=0, ACE_Concurrency_Strategy< SVC_HANDLER > *con_s=0, ACE_Recycling_Strategy< SVC_HANDLER > *rec_s=0, MUTEX *lock=0, int delete_lock=0)
 Constructor.
virtual ~ACE_Bounded_Cached_Connect_Strategy (void)
 Destructor.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Protected Member Functions

virtual int find_or_create_svc_handler_i (SVC_HANDLER *&sh, const ACE_PEER_CONNECTOR_ADDR &remote_addr, ACE_Time_Value *timeout, const ACE_PEER_CONNECTOR_ADDR &local_addr, bool reuse_addr, int flags, int perms, ACE_Hash_Map_Entry< ACE_Refcounted_Hash_Recyclable< ACE_PEER_CONNECTOR_ADDR >, ACE_Pair< SVC_HANDLER *, ATTRIBUTES > > *&entry, int &found)

Protected Attributes

size_t max_size_
 Max items in the cache, used as a bound for the creation of svc_handlers.

Private Types

typedef
ACE_Cached_Connect_Strategy_Ex
< SVC_HANDLER,
ACE_PEER_CONNECTOR_2,
CACHING_STRATEGY, ATTRIBUTES,
MUTEX > 
CCSEBASE
typedef
ACE_Refcounted_Hash_Recyclable
< ACE_PEER_CONNECTOR_ADDR > 
REFCOUNTED_HASH_RECYCLABLE_ADDRESS

Detailed Description

template<class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class CACHING_STRATEGY, class ATTRIBUTES, class MUTEX>
class ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >

A connection strategy which caches connections to peers (represented by SVC_HANDLER instances), thereby allowing subsequent re-use of unused, but available, connections. This strategy should be used when the cache is bounded by maximum size.

Bounded_Cached_Connect_Strategy is intended to be used as a plug-in connection strategy for ACE_Strategy_Connector. It's added value is re-use of established connections and tweaking the role of the cache as per the caching strategy. Thanks to Edan Ayal <edana@bandwiz.com> for contributing this class and Susan Liebeskind <shl@janis.gtri.gatech.edu> for brainstorming about it.

Definition at line 205 of file Cached_Connect_Strategy_T.h.


Member Typedef Documentation

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
typedef ACE_Cached_Connect_Strategy_Ex<SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX> ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::CCSEBASE [private]

Definition at line 210 of file Cached_Connect_Strategy_T.h.

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::REFCOUNTED_HASH_RECYCLABLE_ADDRESS [private]

Constructor & Destructor Documentation

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::ACE_Bounded_Cached_Connect_Strategy ( size_t  max_size,
CACHING_STRATEGY &  caching_s,
ACE_Creation_Strategy< SVC_HANDLER > *  cre_s = 0,
ACE_Concurrency_Strategy< SVC_HANDLER > *  con_s = 0,
ACE_Recycling_Strategy< SVC_HANDLER > *  rec_s = 0,
MUTEX *  lock = 0,
int  delete_lock = 0 
)

Constructor.

Definition at line 566 of file Cached_Connect_Strategy_T.cpp.

  : CCSEBASE (caching_s, cre_s, con_s, rec_s, lock, delete_lock),
    max_size_ (max_size)
{
}

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::~ACE_Bounded_Cached_Connect_Strategy ( void   )  [virtual]

Destructor.

Definition at line 579 of file Cached_Connect_Strategy_T.cpp.

{
}


Member Function Documentation

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
int ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::find_or_create_svc_handler_i ( SVC_HANDLER *&  sh,
const ACE_PEER_CONNECTOR_ADDR &  remote_addr,
ACE_Time_Value timeout,
const ACE_PEER_CONNECTOR_ADDR &  local_addr,
bool  reuse_addr,
int  flags,
int  perms,
ACE_Hash_Map_Entry< ACE_Refcounted_Hash_Recyclable< ACE_PEER_CONNECTOR_ADDR >, ACE_Pair< SVC_HANDLER *, ATTRIBUTES > > *&  entry,
int &  found 
) [protected, virtual]

Reimplemented from ACE_Cached_Connect_Strategy_Ex< SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX >.

Definition at line 586 of file Cached_Connect_Strategy_T.cpp.

{

  REFCOUNTED_HASH_RECYCLABLE_ADDRESS search_addr (remote_addr);

  // Try to find the address in the cache.  Only if we don't find it
  // do we create a new <SVC_HANDLER> and connect it with the server.
  while (this->find (search_addr, entry) != -1)
    {
      // We found a cached svc_handler.
      // Get the cached <svc_handler>
      sh = entry->int_id_.first ();

      // Is the connection clean?
      int state_result= ACE::handle_ready (sh->peer ().get_handle (),
                                           &ACE_Time_Value::zero,
                                           1, // read ready
                                           0, // write ready
                                           1);// exception ready

      if (state_result == 1)
        {
          // The connection was disconnected during idle.
          // close the svc_handler down.
          if (sh->close () == -1)
            {
              ACE_ASSERT (0);
              return -1;
            }
          sh = 0;
          // and rotate once more...
        }
      else if ((state_result == -1) && (errno == ETIME))
        {
          // Found!!!
          // Set the flag
          found = 1;

          // Tell the <svc_handler> that it should prepare itself for
          // being recycled.
          if (this->prepare_for_recycling (sh) == -1)
            {
              ACE_ASSERT (0);
              return -1;
            }

          return 0;
        }
      else  // some other return value or error...
        {
          ACE_ASSERT (0); // just to see it coming

          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%t)ACE_Bounded_Cached_Connect_Strategy<>::")
                      ACE_TEXT ("find_or_create_svc_handler_i - ")
                      ACE_TEXT ("error polling server socket state.\n")));

          return -1;
        }
    }

  // Not found...

  // Set the flag
  found = 0;

  // Check the limit of handlers...
  if ((this->max_size_ > 0) &&
      (this->connection_cache_.current_size () >= this->max_size_))
    {
      // Try to purge idle connections
      if (this->purge_connections () == -1)
        return -1;

      // Check limit again.
      if (this->connection_cache_.current_size () >= this->max_size_)
        // still too much!
        return -1;

      // OK, we have room now...
    }

  // We need to use a temporary variable here since we are not
  // allowed to change <sh> because other threads may use this
  // when we let go of the lock during the OS level connect.
  //
  // Note that making a new svc_handler, connecting remotely,
  // binding to the map, and assigning of the hint and recycler
  // should be atomic to the outside world.
  SVC_HANDLER *potential_handler = 0;

  // Create a new svc_handler
  if (this->make_svc_handler (potential_handler) == -1)
    return -1;

  // Connect using the svc_handler.
  if (this->cached_connect (potential_handler,
                            remote_addr,
                            timeout,
                            local_addr,
                            reuse_addr,
                            flags,
                            perms) == -1)
    {
      // Close the svc handler.
      potential_handler->close (0);
      return -1;
    }
  else
    {
      // Insert the new SVC_HANDLER instance into the cache.
      if (this->connection_cache_.bind (search_addr,
                                        potential_handler,
                                        entry) == -1)
        {
          // Close the svc handler and reset <sh>.
          potential_handler->close (0);

          return -1;
        }

      // Everything succeeded as planned. Assign <sh> to
      // <potential_handler>.
      sh = potential_handler;

      // Set the recycler and the recycling act
      this->assign_recycler (sh, this, entry);
    }

  return 0;
}


Member Data Documentation

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Reimplemented from ACE_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_2 >.

Definition at line 231 of file Cached_Connect_Strategy_T.h.

template<class SVC_HANDLER , ACE_PEER_CONNECTOR_1 , class CACHING_STRATEGY , class ATTRIBUTES , class MUTEX >
size_t ACE_Bounded_Cached_Connect_Strategy< SVC_HANDLER, ACE_PEER_CONNECTOR_1, CACHING_STRATEGY, ATTRIBUTES, MUTEX >::max_size_ [protected]

Max items in the cache, used as a bound for the creation of svc_handlers.

Definition at line 249 of file Cached_Connect_Strategy_T.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines