Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Cached_Connect_Strategy_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Cached_Connect_Strategy_T.h
00006  *
00007  *  $Id: Cached_Connect_Strategy_T.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef CACHED_CONNECT_STRATEGY_T_H
00014 #define CACHED_CONNECT_STRATEGY_T_H
00015 
00016 #include "ace/pre.h"
00017 
00018 #include "ace/config-all.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Strategies_T.h"
00025 #include "ace/Hash_Cache_Map_Manager_T.h"
00026 #include "ace/Caching_Strategies_T.h"
00027 #include "ace/Functor_T.h"
00028 #include "ace/Pair_T.h"
00029 #include "ace/Synch.h"
00030 
00031 // For linkers which cant grok long names...
00032 #define ACE_Cached_Connect_Strategy_Ex ACCSE
00033 
00034 /**
00035  * @class ACE_Cached_Connect_Strategy_Ex
00036  *
00037  * @brief A connection strategy which caches connections to peers
00038  * (represented by <SVC_HANDLER> instances), thereby allowing
00039  * subsequent re-use of unused, but available, connections.
00040  *
00041  * <Cached_Connect_Strategy> is intended to be used as a
00042  * plug-in connection strategy for <ACE_Strategy_Connector>.
00043  * It's added value is re-use of established connections and
00044  * tweaking the role of the cache as per the caching strategy.
00045  */
00046 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class CACHING_STRATEGY, class ATTRIBUTES, class MUTEX>
00047 class ACE_Cached_Connect_Strategy_Ex
00048  : public ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX>
00049 {
00050 public:
00051   /// Constructor
00052   ACE_Cached_Connect_Strategy_Ex (
00053     CACHING_STRATEGY &caching_s,
00054     ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
00055     ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
00056     ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
00057     MUTEX *lock = 0,
00058     int delete_lock = 0);
00059 
00060   /// Destructor
00061   virtual ~ACE_Cached_Connect_Strategy_Ex (void);
00062 
00063   /// Explicit purging of connection entries from the connection cache.
00064   virtual int purge_connections (void);
00065 
00066   /// Mark as closed (non-locking version). This is used during the cleanup of the
00067   /// connections purged.
00068   virtual int mark_as_closed_i (const void *recycling_act);
00069 
00070   /**
00071    * Since g++ version < 2.8 arent happy with templates, this special
00072    * method had to be devised to avoid memory leaks and perform
00073    * cleanup of the <connection_cache_>.
00074    */
00075   void cleanup (void);
00076 
00077   // = Typedefs for managing the map
00078   typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
00079           REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
00080  typedef ACE_Hash_Cache_Map_Manager<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
00081                                     SVC_HANDLER *,
00082                                     ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
00083                                     ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
00084                                     CACHING_STRATEGY,
00085                                     ATTRIBUTES>
00086           CONNECTION_CACHE;
00087   typedef ACE_TYPENAME CONNECTION_CACHE::CACHE_ENTRY CONNECTION_CACHE_ENTRY;
00088   typedef ACE_TYPENAME CONNECTION_CACHE::key_type KEY;
00089   typedef ACE_TYPENAME CONNECTION_CACHE::mapped_type VALUE;
00090 
00091   typedef ACE_Recyclable_Handler_Cleanup_Strategy<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
00092                                                   ACE_Pair<SVC_HANDLER *, ATTRIBUTES>,
00093                                                   ACE_Hash_Map_Manager_Ex<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
00094                                                                           ACE_Pair<SVC_HANDLER *, ATTRIBUTES>,
00095                                                                           ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
00096                                                                           ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
00097                                                                           MUTEX> >
00098           CLEANUP_STRATEGY;
00099 
00100   typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX>
00101           CCSBASE;
00102 
00103   // = Accessor.
00104   CACHING_STRATEGY &caching_strategy (void);
00105 
00106 protected:
00107 
00108   /// Find an idle handle.
00109   int find (ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> &search_addr,
00110             ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry);
00111 
00112   /// Remove from cache (non-locking version).
00113   virtual int purge_i (const void *recycling_act);
00114 
00115   /// Add to cache (non-locking version).
00116   virtual int cache_i (const void *recycling_act);
00117 
00118   /// Get/Set <recycle_state> (non-locking version).
00119   virtual int recycle_state_i (const void *recycling_act,
00120                                ACE_Recyclable_State new_state);
00121   virtual ACE_Recyclable_State recycle_state_i (const void *recycling_act) const;
00122 
00123   /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
00124   virtual int cleanup_hint_i (const void *recycling_act,
00125                               void **act_holder);
00126 
00127   // = Helpers
00128   int check_hint_i (SVC_HANDLER *&sh,
00129                     const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00130                     ACE_Time_Value *timeout,
00131                     const ACE_PEER_CONNECTOR_ADDR &local_addr,
00132                     int reuse_addr,
00133                     int flags,
00134                     int perms,
00135                     ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
00136                     int &found);
00137 
00138   virtual int find_or_create_svc_handler_i (SVC_HANDLER *&sh,
00139                                     const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00140                                     ACE_Time_Value *timeout,
00141                                     const ACE_PEER_CONNECTOR_ADDR &local_addr,
00142                                     int reuse_addr,
00143                                     int flags,
00144                                     int perms,
00145                                     ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
00146                                     int &found);
00147 
00148   virtual int connect_svc_handler_i (SVC_HANDLER *&sh,
00149                                      const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00150                                      ACE_Time_Value *timeout,
00151                                      const ACE_PEER_CONNECTOR_ADDR &local_addr,
00152                                      int reuse_addr,
00153                                      int flags,
00154                                      int perms,
00155                                      int &found);
00156 
00157   /**
00158    * Connection of the svc_handler with the remote host.  This method
00159    * also encapsulates the connection done with auto_purging under the
00160    * hood. If the connect failed due to the process running out of
00161    * file descriptors then, auto_purging of some connections are done
00162    * from the CONNECTION_CACHE. This frees the descriptors which get
00163    * used in the connect process and hence the connect operation can
00164    * succeed.
00165    */
00166   virtual int cached_connect (SVC_HANDLER *&sh,
00167                               const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00168                               ACE_Time_Value *timeout,
00169                               const ACE_PEER_CONNECTOR_ADDR &local_addr,
00170                               int reuse_addr,
00171                               int flags,
00172                               int perms);
00173 
00174   /// Table that maintains the cache of connected <SVC_HANDLER>s.
00175   CONNECTION_CACHE connection_cache_;
00176 };
00177 
00178 /////////////////////////////////////////////////////////////////////////////
00179 
00180 // For linkers which cant grok long names...
00181 #define ACE_Bounded_Cached_Connect_Strategy ABCCS
00182 
00183 /**
00184  * @class ACE_Bounded_Cached_Connect_Strategy
00185  *
00186  * @brief A connection strategy which caches connections to peers
00187  *     (represented by <SVC_HANDLER> instances), thereby allowing
00188  *     subsequent re-use of unused, but available, connections.
00189  *     This strategy should be used when the cache is bounded by
00190  *     maximum size.
00191  *
00192  *       <Bounded_Cached_Connect_Strategy> is intended to be used as a
00193  *     plug-in connection strategy for <ACE_Strategy_Connector>.
00194  *     It's added value is re-use of established connections and
00195  *     tweaking the role of the cache as per the caching strategy.
00196  *     Thanks to Edan Ayal  <edana@bandwiz.com> for contributing this
00197  *     class and Susan Liebeskind  <shl@janis.gtri.gatech.edu> for
00198  *     brainstorming about it.
00199  */
00200 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1,
00201           class CACHING_STRATEGY, class ATTRIBUTES,
00202           class MUTEX>
00203 class ACE_Bounded_Cached_Connect_Strategy
00204   : public ACE_Cached_Connect_Strategy_Ex<SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX>
00205 {
00206 
00207    typedef ACE_Cached_Connect_Strategy_Ex<SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX>
00208    CCSEBASE;
00209 
00210   // = Typedefs for managing the map
00211   typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
00212           REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
00213 
00214 public:
00215 
00216   /// Constructor
00217   ACE_Bounded_Cached_Connect_Strategy (size_t  max_size,
00218                                        CACHING_STRATEGY &caching_s,
00219                                        ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
00220                                        ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
00221                                        ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
00222                                        MUTEX *lock = 0,
00223                                        int delete_lock = 0);
00224 
00225    /// Destructor
00226    virtual ~ACE_Bounded_Cached_Connect_Strategy (void);
00227 
00228    /// Declare the dynamic allocation hooks.
00229    ACE_ALLOC_HOOK_DECLARE;
00230 
00231 protected:
00232 
00233   virtual int find_or_create_svc_handler_i (SVC_HANDLER *&sh,
00234                                             const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00235                                             ACE_Time_Value *timeout,
00236                                             const ACE_PEER_CONNECTOR_ADDR &local_addr,
00237                                             int reuse_addr,
00238                                             int flags,
00239                                             int perms,
00240                                             ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>,
00241                                             ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
00242                                             int &found);
00243 
00244 protected:
00245 
00246   /// max items in the cache, used as a bound for the creation of svc_handlers.
00247   size_t  max_size_;
00248 };
00249 
00250 
00251 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00252 #include "ace/Cached_Connect_Strategy_T.cpp"
00253 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00254 
00255 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00256 #pragma implementation ("Cached_Connect_Strategy_T.cpp")
00257 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00258 
00259 #include "ace/post.h"
00260 #endif /* CACHED_CONNECT_STRATEGY_T_H */

Generated on Mon Jun 16 11:19:16 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002