Author: sjlee
Date: Tue Jun 23 20:15:38 2009
New Revision: 787819
URL: http://svn.apache.org/viewvc?rev=787819&view=rev
Log:
ASYNCWEB-30
Use InetSocketAddress objects instead of string as keys for the session cache.
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/SessionCache.java
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java?rev=787819&r1=787818&r2=787819&view=diff
==============================================================================
---
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
(original)
+++
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
Tue Jun 23 20:15:38 2009
@@ -504,9 +504,10 @@
// *IF* connection reuse is enabled, we should see if we have a cached
// connection first; if not, always open a new one
+ InetSocketAddress remote = getAddress(message);
ConnectFuture future = null;
if (getSessionCache() != null) {
- future = getCachedConnection(message);
+ future = getCachedConnection(message, remote);
} else {
// add the Connection close header explicitly
message.setHeader(HttpDecoder.CONNECTION, HttpDecoder.CLOSE);
@@ -521,7 +522,7 @@
// having a connection retry result in both a CONNECTION_ATTEMPTED
and
// CONNECTION_RETRIED event getting dispatched.
notifyMonitoringListeners(MonitoringEvent.CONNECTION_ATTEMPTED,
message);
- future = openConnection(message);
+ future = openConnection(remote);
}
ResponseFuture response = message.getResponseFuture();
FutureListener listener =
@@ -546,25 +547,30 @@
// set the connect start time again
message.setConnectStartTime();
notifyMonitoringListeners(MonitoringEvent.CONNECTION_RETRIED,
message);
- ConnectFuture future = openConnection(message);
+ ConnectFuture future = openConnection(getAddress(message));
future.addListener(listener);
}
/**
+ * Creates an InetSocketAddress object appropriate for the message, taking
+ * into account a possible proxy configuration.
+ */
+ private InetSocketAddress getAddress(HttpRequestMessage message) {
+ return message.isProxyEnabled() ?
+
message.getProxyConfiguration().getProxyAddress(message.getUrl()) :
+ new InetSocketAddress(message.getHost(),
message.getPort());
+ }
+
+ /**
* Open the appropriate connection for this message.
* This will either open a direct connection or connect
* to the configured proxy server.
*
- * @param message The message getting sent. This defines the target
- * location and also holds the proxy configuration.
+ * @param remote the remote address.
*
* @return A ConnectFuture instance for managing the connection.
*/
- private ConnectFuture openConnection(HttpRequestMessage message) {
- InetSocketAddress remote =
- message.isProxyEnabled() ?
-
message.getProxyConfiguration().getProxyAddress(message.getUrl()) :
- new InetSocketAddress(message.getHost(),
message.getPort());
+ private ConnectFuture openConnection(InetSocketAddress remote) {
return connector.connect(remote, handler);
}
@@ -572,12 +578,13 @@
* Attempt to get a connection from the session cache.
*
* @param message The message we're sending.
+ * @param remote the remote address.
*
* @return A cached connection. This returns null if there's
* no available connection for the target location.
*/
- private ConnectFuture getCachedConnection(HttpRequestMessage message) {
- IoSession cached = sessionCache.getActiveSession(message);
+ private ConnectFuture getCachedConnection(HttpRequestMessage message,
InetSocketAddress remote) {
+ IoSession cached = sessionCache.getActiveSession(remote);
if (cached == null) {
return null;
}
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/SessionCache.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/SessionCache.java?rev=787819&r1=787818&r2=787819&view=diff
==============================================================================
---
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/SessionCache.java
(original)
+++
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/SessionCache.java
Tue Jun 23 20:15:38 2009
@@ -25,7 +25,6 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
-import org.apache.asyncweb.client.proxy.ProxyConfiguration;
import org.apache.mina.common.IoSession;
/**
@@ -33,8 +32,8 @@
* the host and the port. This class is thread safe.
*/
public final class SessionCache {
- private final ConcurrentMap<String,Queue<IoSession>> cachedSessions =
- new ConcurrentHashMap<String,Queue<IoSession>>();
+ private final ConcurrentMap<InetSocketAddress,Queue<IoSession>>
cachedSessions =
+ new ConcurrentHashMap<InetSocketAddress,Queue<IoSession>>();
public SessionCache() {}
@@ -44,16 +43,16 @@
* connection can be used without errors, although it should be usually
* safe to use it.
*
- * @param msg the message for which to look up an active session.
- * @throws IllegalArgumentException if a null request message was passed
in.
+ * @param addr the remote address with which to look up an active session.
+ * @throws IllegalArgumentException if a null address was passed in.
* @return an active IoSession, or null if none are found.
*/
- public IoSession getActiveSession(HttpRequestMessage msg) {
- if (msg == null) {
- throw new IllegalArgumentException("null request was passed in");
+ public IoSession getActiveSession(InetSocketAddress addr) {
+ if (addr == null) {
+ throw new IllegalArgumentException("null address was passed in");
}
- Queue<IoSession> queue = cachedSessions.get(getKey(msg));
+ Queue<IoSession> queue = cachedSessions.get(addr);
if (queue == null) {
return null;
}
@@ -69,7 +68,7 @@
}
/**
- * Caches the given session using its remote host and port information.
+ * Caches the given session using its remote address.
*
* @param session IoSession to cache
* @throws IllegalArgumentException if a null session was passed in.
@@ -79,12 +78,15 @@
throw new IllegalArgumentException("null session was passed in");
}
- String key = getKey((InetSocketAddress)session.getRemoteAddress());
- Queue<IoSession> newQueue = new ConcurrentLinkedQueue<IoSession>();
- Queue<IoSession> queue = cachedSessions.putIfAbsent(key, newQueue);
- if (queue == null) {
- // the value was previously empty
- queue = newQueue;
+ InetSocketAddress addr = (InetSocketAddress)session.getRemoteAddress();
+ Queue<IoSession> queue = cachedSessions.get(addr);
+ if (queue == null) {
+ queue = new ConcurrentLinkedQueue<IoSession>();
+ Queue<IoSession> existing = cachedSessions.putIfAbsent(addr,
queue);
+ if (existing != null) {
+ // the value exists
+ queue = existing;
+ }
}
// add it to the queue
queue.offer(session);
@@ -101,46 +103,10 @@
throw new IllegalArgumentException("null session was passed in");
}
- String key = getKey((InetSocketAddress)session.getRemoteAddress());
- Queue<IoSession> queue = cachedSessions.get(key);
+ InetSocketAddress addr = (InetSocketAddress)session.getRemoteAddress();
+ Queue<IoSession> queue = cachedSessions.get(addr);
if (queue != null) {
queue.remove(session);
}
}
-
- /**
- * Generate a request key from an HTTP request message.
- *
- * @param msg The request message we need a key from.
- *
- * @return A String key instance for this request.
- */
- private String getKey(HttpRequestMessage msg) {
- if (msg.isProxyEnabled()) {
- ProxyConfiguration proxyCfg = msg.getProxyConfiguration();
- return (msg.getProtocol().equalsIgnoreCase("https")) ?
- getKey(proxyCfg.getHttpsProxyHost(),
proxyCfg.getHttpsProxyPort()) :
- getKey(proxyCfg.getHttpProxyHost(),
proxyCfg.getHttpProxyPort());
- } else {
- return getKey(msg.getHost(), msg.getPort());
- }
- }
-
- /**
- * Generate a session key from an InetSocketAddress
- *
- * @param remote The endpoint address of the connection.
- *
- * @return A string key for this endpoint.
- */
- private String getKey(InetSocketAddress remote) {
- return getKey(remote.getHostName(), remote.getPort());
- }
-
- /**
- * The key is of the form "host:port".
- */
- private String getKey(String host, int port) {
- return new StringBuilder(host).append(':').append(port).toString();
- }
}