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

Generated on Thu Nov 9 09:41:47 2006 for ACE by doxygen 1.3.6