Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef ACE_INET_CONNECTION_CACHE_H
00010 #define ACE_INET_CONNECTION_CACHE_H
00011
00012 #include "ace/pre.h"
00013
00014 #include "ace/Synch_Traits.h"
00015 #include "ace/Thread_Mutex.h"
00016 #include "ace/Condition_Thread_Mutex.h"
00017 #include "ace/Null_Mutex.h"
00018 #include "ace/Hash_Map_Manager_T.h"
00019 #include "ace/INet/INet_Export.h"
00020
00021 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00022
00023 namespace ACE
00024 {
00025 namespace INet
00026 {
00027
00028
00029
00030
00031
00032
00033
00034 class ACE_INET_Export ConnectionKey
00035 {
00036 public:
00037 ConnectionKey ();
00038 virtual ~ConnectionKey ();
00039
00040 bool operator ==(const ConnectionKey& key) const;
00041
00042 bool operator !=(const ConnectionKey& key) const;
00043
00044 virtual u_long hash () const = 0;
00045
00046 virtual ConnectionKey* duplicate () const = 0;
00047
00048 protected:
00049 virtual bool equal (const ConnectionKey& key) const = 0;
00050 };
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class ConnectionCacheKey
00061 {
00062 public:
00063 ConnectionCacheKey ();
00064 ConnectionCacheKey (const ConnectionKey& key);
00065 ConnectionCacheKey (const ConnectionCacheKey& cachekey);
00066
00067 ConnectionCacheKey& operator =(const ConnectionCacheKey& cachekey);
00068
00069 bool operator ==(const ConnectionCacheKey& cachekey) const;
00070
00071 bool operator !=(const ConnectionCacheKey& cachekey) const;
00072
00073 u_long hash () const;
00074
00075 const ConnectionKey& key () const;
00076
00077 private:
00078 ConnectionKey* key_;
00079 bool delete_key_;
00080 };
00081
00082
00083
00084
00085
00086
00087
00088 class ACE_INET_Export ConnectionHolder
00089 {
00090 public:
00091 virtual ~ConnectionHolder ();
00092 protected:
00093 ConnectionHolder ();
00094 };
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 class ACE_INET_Export ConnectionFactory
00107 {
00108 public:
00109 ConnectionFactory ();
00110 virtual ~ConnectionFactory ();
00111
00112 virtual ConnectionHolder* create_connection (
00113 const ConnectionKey& key) const = 0;
00114 };
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 class ConnectionCacheValue
00127 {
00128 public:
00129 enum State
00130 {
00131 CST_NONE,
00132 CST_INIT,
00133 CST_IDLE,
00134 CST_BUSY,
00135 CST_CLOSED
00136 };
00137
00138 typedef ConnectionHolder connection_type;
00139
00140 ConnectionCacheValue ();
00141 explicit ConnectionCacheValue (connection_type* connection);
00142 ConnectionCacheValue (const ConnectionCacheValue& cacheval);
00143
00144 ConnectionCacheValue& operator =(const ConnectionCacheValue& cacheval);
00145
00146 bool operator == (const ConnectionCacheValue& cacheval) const;
00147
00148 bool operator != (const ConnectionCacheValue& cacheval) const;
00149
00150 connection_type* connection ();
00151
00152 const connection_type* connection () const;
00153
00154 void connection (connection_type* conn);
00155
00156 State state () const;
00157
00158 void state (State st);
00159
00160 private:
00161 State state_;
00162 connection_type* connection_;
00163 };
00164
00165
00166
00167
00168
00169
00170
00171
00172 class ACE_INET_Export ConnectionCache
00173 {
00174 public:
00175 typedef ConnectionHolder connection_type;
00176 typedef ConnectionFactory factory_type;
00177
00178 typedef ACE_Hash_Map_Manager_Ex <ConnectionCacheKey,
00179 ConnectionCacheValue,
00180 ACE_Hash<ConnectionCacheKey>,
00181 ACE_Equal_To<ConnectionCacheKey>,
00182 ACE_SYNCH_NULL_MUTEX> map_type;
00183
00184 typedef map_type::iterator map_iter_type;
00185
00186 typedef ACE_Hash_Map_Entry <ConnectionCacheKey,
00187 ConnectionCacheValue> map_entry_type;
00188
00189
00190 ConnectionCache(size_t size = ACE_DEFAULT_MAP_SIZE);
00191
00192
00193 ~ConnectionCache ();
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 bool claim_connection(const ConnectionKey& key,
00206 connection_type*& connection,
00207 const factory_type& connection_factory,
00208 bool wait = true);
00209
00210
00211
00212
00213 bool release_connection(const ConnectionKey& key,
00214 connection_type* connection);
00215
00216
00217
00218
00219
00220 bool close_connection(const ConnectionKey& key,
00221 connection_type* connection);
00222
00223
00224
00225
00226 bool has_connection (const ConnectionKey& key);
00227
00228
00229 void close_all_connections ();
00230
00231
00232 size_t current_size () const;
00233
00234 private:
00235
00236 bool set_connection (const ConnectionKey& key,
00237 const ConnectionCacheValue& cacheval);
00238
00239
00240
00241
00242
00243 bool claim_existing_connection(const ConnectionKey& key,
00244 connection_type*& connection,
00245 ConnectionCacheValue::State& state);
00246
00247
00248
00249
00250 bool find_connection (const ConnectionKey& key,
00251 ConnectionCacheValue& cacheval);
00252
00253
00254 mutable ACE_SYNCH_MUTEX lock_;
00255 ACE_SYNCH_CONDITION condition_;
00256 map_type cache_map_;
00257 };
00258
00259
00260 }
00261 }
00262
00263 ACE_END_VERSIONED_NAMESPACE_DECL
00264
00265 #if defined (__ACE_INLINE__)
00266 #include "ace/INet/ConnectionCache.inl"
00267 #endif
00268
00269 #include "ace/post.h"
00270 #endif