3 //=============================================================================
5 * @file Cached_Connect_Strategy_T.h
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
11 #ifndef CACHED_CONNECT_STRATEGY_T_H
12 #define CACHED_CONNECT_STRATEGY_T_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/config-all.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Strategies_T.h"
23 #include "ace/Hash_Cache_Map_Manager_T.h"
24 #include "ace/Caching_Strategies_T.h"
25 #include "ace/Functor_T.h"
26 #include "ace/Pair_T.h"
28 // For linkers which cant grok long names...
29 #define ACE_Cached_Connect_Strategy_Ex ACCSE
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
34 * @class ACE_Cached_Connect_Strategy_Ex
36 * @brief A connection strategy which caches connections to peers
37 * (represented by SVC_HANDLER instances), thereby allowing
38 * subsequent re-use of unused, but available, connections.
40 * <Cached_Connect_Strategy> is intended to be used as a
41 * plug-in connection strategy for ACE_Strategy_Connector.
42 * It's added value is re-use of established connections and
43 * tweaking the role of the cache as per the caching strategy.
45 template <class SVC_HANDLER
, ACE_PEER_CONNECTOR_1
, class CACHING_STRATEGY
, class ATTRIBUTES
, class MUTEX
>
46 class ACE_Cached_Connect_Strategy_Ex
47 : public ACE_Cached_Connect_Strategy
<SVC_HANDLER
, ACE_PEER_CONNECTOR_2
, MUTEX
>
51 ACE_Cached_Connect_Strategy_Ex (
52 CACHING_STRATEGY
&caching_s
,
53 ACE_Creation_Strategy
<SVC_HANDLER
> *cre_s
= 0,
54 ACE_Concurrency_Strategy
<SVC_HANDLER
> *con_s
= 0,
55 ACE_Recycling_Strategy
<SVC_HANDLER
> *rec_s
= 0,
60 virtual ~ACE_Cached_Connect_Strategy_Ex ();
62 /// Explicit purging of connection entries from the connection cache.
63 virtual int purge_connections ();
65 /// Mark as closed (non-locking version). This is used during the cleanup of the
66 /// connections purged.
67 virtual int mark_as_closed_i (const void *recycling_act
);
70 * Since g++ version < 2.8 arent happy with templates, this special
71 * method had to be devised to avoid memory leaks and perform
72 * cleanup of the <connection_cache_>.
76 // = Typedefs for managing the map
77 typedef ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>
78 REFCOUNTED_HASH_RECYCLABLE_ADDRESS
;
79 typedef ACE_Hash_Cache_Map_Manager
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
,
81 ACE_Hash
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
>,
82 ACE_Equal_To
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
>,
86 typedef typename
CONNECTION_CACHE::CACHE_ENTRY CONNECTION_CACHE_ENTRY
;
87 typedef typename
CONNECTION_CACHE::key_type KEY
;
88 typedef typename
CONNECTION_CACHE::mapped_type VALUE
;
90 typedef ACE_Recyclable_Handler_Cleanup_Strategy
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
,
91 std::pair
<SVC_HANDLER
*, ATTRIBUTES
>,
92 ACE_Hash_Map_Manager_Ex
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
,
93 std::pair
<SVC_HANDLER
*, ATTRIBUTES
>,
94 ACE_Hash
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
>,
95 ACE_Equal_To
<REFCOUNTED_HASH_RECYCLABLE_ADDRESS
>,
99 typedef ACE_Cached_Connect_Strategy
<SVC_HANDLER
, ACE_PEER_CONNECTOR_2
, MUTEX
>
103 CACHING_STRATEGY
&caching_strategy ();
105 /// Declare the dynamic allocation hooks.
106 ACE_ALLOC_HOOK_DECLARE
;
109 /// Find an idle handle.
110 int find (ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
> &search_addr
,
111 ACE_Hash_Map_Entry
<ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>, std::pair
<SVC_HANDLER
*, ATTRIBUTES
> > *&entry
);
113 /// Remove from cache (non-locking version).
114 virtual int purge_i (const void *recycling_act
);
116 /// Add to cache (non-locking version).
117 virtual int cache_i (const void *recycling_act
);
119 /// Get/Set recycle_state (non-locking version).
120 virtual int recycle_state_i (const void *recycling_act
,
121 ACE_Recyclable_State new_state
);
122 virtual ACE_Recyclable_State
recycle_state_i (const void *recycling_act
) const;
124 /// Cleanup hint and reset @c *act_holder to zero if @a act_holder != 0.
125 virtual int cleanup_hint_i (const void *recycling_act
,
129 int check_hint_i (SVC_HANDLER
*&sh
,
130 const ACE_PEER_CONNECTOR_ADDR
&remote_addr
,
131 ACE_Time_Value
*timeout
,
132 const ACE_PEER_CONNECTOR_ADDR
&local_addr
,
136 ACE_Hash_Map_Entry
<ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>, std::pair
<SVC_HANDLER
*, ATTRIBUTES
> > *&entry
,
139 virtual int find_or_create_svc_handler_i (SVC_HANDLER
*&sh
,
140 const ACE_PEER_CONNECTOR_ADDR
&remote_addr
,
141 ACE_Time_Value
*timeout
,
142 const ACE_PEER_CONNECTOR_ADDR
&local_addr
,
146 ACE_Hash_Map_Entry
<ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>, std::pair
<SVC_HANDLER
*, ATTRIBUTES
> > *&entry
,
149 virtual int connect_svc_handler_i (SVC_HANDLER
*&sh
,
150 const ACE_PEER_CONNECTOR_ADDR
&remote_addr
,
151 ACE_Time_Value
*timeout
,
152 const ACE_PEER_CONNECTOR_ADDR
&local_addr
,
159 * Connection of the svc_handler with the remote host. This method
160 * also encapsulates the connection done with auto_purging under the
161 * hood. If the connect failed due to the process running out of
162 * file descriptors then, auto_purging of some connections are done
163 * from the CONNECTION_CACHE. This frees the descriptors which get
164 * used in the connect process and hence the connect operation can
167 virtual int cached_connect (SVC_HANDLER
*&sh
,
168 const ACE_PEER_CONNECTOR_ADDR
&remote_addr
,
169 ACE_Time_Value
*timeout
,
170 const ACE_PEER_CONNECTOR_ADDR
&local_addr
,
175 /// Table that maintains the cache of connected SVC_HANDLERs.
176 CONNECTION_CACHE connection_cache_
;
179 /////////////////////////////////////////////////////////////////////////////
181 // For linkers which cant grok long names...
182 #define ACE_Bounded_Cached_Connect_Strategy ABCCS
185 * @class ACE_Bounded_Cached_Connect_Strategy
188 * A connection strategy which caches connections to peers
189 * (represented by SVC_HANDLER instances), thereby allowing
190 * subsequent re-use of unused, but available, connections.
191 * This strategy should be used when the cache is bounded by
194 * Bounded_Cached_Connect_Strategy is intended to be used as a
195 * plug-in connection strategy for ACE_Strategy_Connector.
196 * It's added value is re-use of established connections and
197 * tweaking the role of the cache as per the caching strategy.
198 * Thanks to Edan Ayal <edana@bandwiz.com> for contributing this
199 * class and Susan Liebeskind <shl@janis.gtri.gatech.edu> for
200 * brainstorming about it.
202 template <class SVC_HANDLER
, ACE_PEER_CONNECTOR_1
,
203 class CACHING_STRATEGY
, class ATTRIBUTES
,
205 class ACE_Bounded_Cached_Connect_Strategy
206 : public ACE_Cached_Connect_Strategy_Ex
<SVC_HANDLER
, ACE_PEER_CONNECTOR_2
, CACHING_STRATEGY
, ATTRIBUTES
, MUTEX
>
208 typedef ACE_Cached_Connect_Strategy_Ex
<SVC_HANDLER
, ACE_PEER_CONNECTOR_2
, CACHING_STRATEGY
, ATTRIBUTES
, MUTEX
>
211 // = Typedefs for managing the map
212 typedef ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>
213 REFCOUNTED_HASH_RECYCLABLE_ADDRESS
;
217 ACE_Bounded_Cached_Connect_Strategy (size_t max_size
,
218 CACHING_STRATEGY
&caching_s
,
219 ACE_Creation_Strategy
<SVC_HANDLER
> *cre_s
= 0,
220 ACE_Concurrency_Strategy
<SVC_HANDLER
> *con_s
= 0,
221 ACE_Recycling_Strategy
<SVC_HANDLER
> *rec_s
= 0,
223 int delete_lock
= 0);
226 virtual ~ACE_Bounded_Cached_Connect_Strategy () = default;
228 /// Declare the dynamic allocation hooks.
229 ACE_ALLOC_HOOK_DECLARE
;
232 virtual int find_or_create_svc_handler_i (SVC_HANDLER
*&sh
,
233 const ACE_PEER_CONNECTOR_ADDR
&remote_addr
,
234 ACE_Time_Value
*timeout
,
235 const ACE_PEER_CONNECTOR_ADDR
&local_addr
,
239 ACE_Hash_Map_Entry
<ACE_Refcounted_Hash_Recyclable
<ACE_PEER_CONNECTOR_ADDR
>,
240 std::pair
<SVC_HANDLER
*, ATTRIBUTES
> > *&entry
,
244 /// Max items in the cache, used as a bound for the creation of svc_handlers.
248 ACE_END_VERSIONED_NAMESPACE_DECL
250 #include "ace/Cached_Connect_Strategy_T.cpp"
252 #include /**/ "ace/post.h"
253 #endif /* CACHED_CONNECT_STRATEGY_T_H */