]> git.saurik.com Git - apple/security.git/blob - Network/connectionpool.cpp
bea735d4cf32464daa87e3866824176c4288380a
[apple/security.git] / Network / connectionpool.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // connectionpool - manage pool of active, unused Connection objects
21 //
22 #include "connectionpool.h"
23 #include "netconnection.h"
24
25
26 namespace Security {
27 namespace Network {
28
29
30 //
31 // Try to locate a Connection with a suitable HostTarget from the pool.
32 // If found, remove it from the pool and return it. Otherwise, return NULL (no error).
33 //
34 Connection *ConnectionPool::get(const HostTarget &host)
35 {
36 //@@@ locking, of course :-)
37 ConnectionMap::iterator it = mConnections.find(host);
38 if (it != mConnections.end()) {
39 // take it and use it
40 Connection *connection = it->second;
41 mConnections.erase(it);
42 secdebug("connpool", "Connection %p retrieved from pool", connection);
43 return connection;
44 }
45 // none available
46 return NULL;
47 }
48
49
50 //
51 // Retain a Connection in the pool
52 //
53 void ConnectionPool::retain(Connection *connection)
54 {
55 //@@@ threading, of course :-)
56 secdebug("connpool", "Connection %p retained in connection pool", connection);
57 mConnections.insert(ConnectionMap::value_type(connection->hostTarget, connection));
58 //mConnections[connection->hostTarget] = connection;
59 }
60
61
62 //
63 // Remove a retained Connection from the pool.
64 // Returns true if found (and removed); false otherwise.
65 //
66 bool ConnectionPool::remove(Connection *connection)
67 {
68 // this search is two-stage to deal with potentially large multimaps
69 typedef ConnectionMap::iterator Iter;
70 pair<Iter, Iter> range = mConnections.equal_range(connection->hostTarget);
71 for (Iter it = range.first; it != range.second; it++)
72 if (it->second == connection) {
73 mConnections.erase(it);
74 secdebug("connpool", "Connection %p removed from connection pool", connection);
75 return true;
76 }
77 return false;
78 }
79
80
81 //
82 // Clear the connection pool
83 //
84 void ConnectionPool::purge()
85 {
86 secdebug("connpool", "Connection pool purging %ld connections", mConnections.size());
87 for (ConnectionMap::iterator it = mConnections.begin(); it != mConnections.end(); it++)
88 delete it->second;
89 mConnections.erase(mConnections.begin(), mConnections.end());
90 }
91
92
93 } // end namespace Network
94 } // end namespace Security