summaryrefslogtreecommitdiff
path: root/ace/Cached_Connect_Strategy_T.h
blob: 4d928e57f5420a4a1957c22d7c1716755c4f1d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Cached_Connect_Strategy_T.h
 *
 *  $Id$
 *
 *  @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
 */
//=============================================================================

#ifndef CACHED_CONNECT_STRATEGY_T_H
#define CACHED_CONNECT_STRATEGY_T_H

#include /**/ "ace/pre.h"

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Strategies_T.h"
#include "ace/Hash_Cache_Map_Manager_T.h"
#include "ace/Caching_Strategies_T.h"
#include "ace/Functor_T.h"
#include "ace/Pair_T.h"

// For linkers which cant grok long names...
#define ACE_Cached_Connect_Strategy_Ex ACCSE

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

/**
 * @class ACE_Cached_Connect_Strategy_Ex
 *
 * @brief A connection strategy which caches connections to peers
 * (represented by <SVC_HANDLER> instances), thereby allowing
 * subsequent re-use of unused, but available, connections.
 *
 * <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.
 */
template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class CACHING_STRATEGY, class ATTRIBUTES, class MUTEX>
class ACE_Cached_Connect_Strategy_Ex
 : public ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX>
{
public:
  /// Constructor
  ACE_Cached_Connect_Strategy_Ex (
    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);

  /// Destructor
  virtual ~ACE_Cached_Connect_Strategy_Ex (void);

  /// Explicit purging of connection entries from the connection cache.
  virtual int purge_connections (void);

  /// Mark as closed (non-locking version). This is used during the cleanup of the
  /// connections purged.
  virtual int mark_as_closed_i (const void *recycling_act);

  /**
   * Since g++ version < 2.8 arent happy with templates, this special
   * method had to be devised to avoid memory leaks and perform
   * cleanup of the <connection_cache_>.
   */
  void cleanup (void);

  // = Typedefs for managing the map
  typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
          REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
 typedef ACE_Hash_Cache_Map_Manager<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
                                    SVC_HANDLER *,
                                    ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
                                    ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
                                    CACHING_STRATEGY,
                                    ATTRIBUTES>
          CONNECTION_CACHE;
  typedef ACE_TYPENAME CONNECTION_CACHE::CACHE_ENTRY CONNECTION_CACHE_ENTRY;
  typedef ACE_TYPENAME CONNECTION_CACHE::key_type KEY;
  typedef ACE_TYPENAME CONNECTION_CACHE::mapped_type VALUE;

  typedef ACE_Recyclable_Handler_Cleanup_Strategy<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
                                                  ACE_Pair<SVC_HANDLER *, ATTRIBUTES>,
                                                  ACE_Hash_Map_Manager_Ex<REFCOUNTED_HASH_RECYCLABLE_ADDRESS,
                                                                          ACE_Pair<SVC_HANDLER *, ATTRIBUTES>,
                                                                          ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
                                                                          ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>,
                                                                          MUTEX> >
          CLEANUP_STRATEGY;

  typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX>
          CCSBASE;

  // = Accessor.
  CACHING_STRATEGY &caching_strategy (void);

protected:

  /// Find an idle handle.
  int find (ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> &search_addr,
            ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry);

  /// Remove from cache (non-locking version).
  virtual int purge_i (const void *recycling_act);

  /// Add to cache (non-locking version).
  virtual int cache_i (const void *recycling_act);

  /// Get/Set <recycle_state> (non-locking version).
  virtual int recycle_state_i (const void *recycling_act,
                               ACE_Recyclable_State new_state);
  virtual ACE_Recyclable_State recycle_state_i (const void *recycling_act) const;

  /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
  virtual int cleanup_hint_i (const void *recycling_act,
                              void **act_holder);

  // = Helpers
  int check_hint_i (SVC_HANDLER *&sh,
                    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                    ACE_Time_Value *timeout,
                    const ACE_PEER_CONNECTOR_ADDR &local_addr,
                    int 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);

  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,
                                    int 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);

  virtual int connect_svc_handler_i (SVC_HANDLER *&sh,
                                     const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                                     ACE_Time_Value *timeout,
                                     const ACE_PEER_CONNECTOR_ADDR &local_addr,
                                     int reuse_addr,
                                     int flags,
                                     int perms,
                                     int &found);

  /**
   * Connection of the svc_handler with the remote host.  This method
   * also encapsulates the connection done with auto_purging under the
   * hood. If the connect failed due to the process running out of
   * file descriptors then, auto_purging of some connections are done
   * from the CONNECTION_CACHE. This frees the descriptors which get
   * used in the connect process and hence the connect operation can
   * succeed.
   */
  virtual int cached_connect (SVC_HANDLER *&sh,
                              const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                              ACE_Time_Value *timeout,
                              const ACE_PEER_CONNECTOR_ADDR &local_addr,
                              int reuse_addr,
                              int flags,
                              int perms);

  /// Table that maintains the cache of connected <SVC_HANDLER>s.
  CONNECTION_CACHE connection_cache_;
};

/////////////////////////////////////////////////////////////////////////////

// For linkers which cant grok long names...
#define ACE_Bounded_Cached_Connect_Strategy ABCCS

/**
 * @class ACE_Bounded_Cached_Connect_Strategy
 *
 * @brief 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.
 */
template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1,
          class CACHING_STRATEGY, class ATTRIBUTES,
          class MUTEX>
class ACE_Bounded_Cached_Connect_Strategy
  : public ACE_Cached_Connect_Strategy_Ex<SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX>
{

   typedef ACE_Cached_Connect_Strategy_Ex<SVC_HANDLER, ACE_PEER_CONNECTOR_2, CACHING_STRATEGY, ATTRIBUTES, MUTEX>
   CCSEBASE;

  // = Typedefs for managing the map
  typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
          REFCOUNTED_HASH_RECYCLABLE_ADDRESS;

public:

  /// Constructor
  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);

   /// Destructor
   virtual ~ACE_Bounded_Cached_Connect_Strategy (void);

   /// Declare the dynamic allocation hooks.
   ACE_ALLOC_HOOK_DECLARE;

protected:

  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,
                                            int 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:

  /// max items in the cache, used as a bound for the creation of svc_handlers.
  size_t  max_size_;
};

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Cached_Connect_Strategy_T.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Cached_Connect_Strategy_T.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"
#endif /* CACHED_CONNECT_STRATEGY_T_H */