summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-05-26 02:24:56 (EDT)
committerEike Stepper2008-05-26 02:24:56 (EDT)
commit5d5b1e471989afdece69794bdb85ca0f2b0087fb (patch)
tree620d6fd5c0e6a6dff83c289b972d4bda573a0114
parentb87a353a1c9dc61d6158a33a217ac3664f6e68a7 (diff)
downloadcdo-5d5b1e471989afdece69794bdb85ca0f2b0087fb.zip
cdo-5d5b1e471989afdece69794bdb85ca0f2b0087fb.tar.gz
cdo-5d5b1e471989afdece69794bdb85ca0f2b0087fb.tar.bz2
[232648] Provide an HTTPConnector
https://bugs.eclipse.org/bugs/show_bug.cgi?id=232648
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java4
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java110
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java9
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java39
-rw-r--r--plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java1
5 files changed, 132 insertions, 31 deletions
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
index 4244e5a..03d3771 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/http/IHTTPConnector.java
@@ -17,5 +17,9 @@ import org.eclipse.net4j.connector.IConnector;
*/
public interface IHTTPConnector extends IConnector
{
+ public static final int UNKNOWN_MAX_IDLE_TIME = -1;
+
public String getConnectorID();
+
+ public int getMaxIdleTime();
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
index 76aa123..aeb9772 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPAcceptor.java
@@ -10,12 +10,12 @@
**************************************************************************/
package org.eclipse.net4j.internal.http;
-import org.eclipse.net4j.buffer.IBuffer;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.http.IHTTPAcceptor;
import org.eclipse.net4j.http.IHTTPConnector;
import org.eclipse.net4j.http.INet4jTransportServlet;
import org.eclipse.net4j.internal.http.bundle.OM;
+import org.eclipse.net4j.internal.util.lifecycle.Worker;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.security.IRandomizer;
@@ -24,7 +24,6 @@ import org.eclipse.internal.net4j.acceptor.Acceptor;
import org.eclipse.internal.net4j.channel.InternalChannel;
import org.eclipse.internal.net4j.connector.Connector;
-import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -37,6 +36,8 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
{
public static final int DEFAULT_CONNECTOR_ID_LENGTH = 32;
+ public static final int DEFAULT_MAX_IDLE_TIME = 10000;
+
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HTTPAcceptor.class);
private IRandomizer randomizer;
@@ -45,7 +46,19 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
private int connectorIDLength = DEFAULT_CONNECTOR_ID_LENGTH;
- private Map<String, HTTPConnector> httpConnectors = new HashMap<String, HTTPConnector>();
+ private int maxIdleTime = DEFAULT_MAX_IDLE_TIME;
+
+ private Map<String, HTTPServerConnector> httpConnectors = new HashMap<String, HTTPServerConnector>();
+
+ private Worker cleaner = new Worker()
+ {
+ @Override
+ protected void work(WorkContext context) throws Exception
+ {
+ int pause = cleanIdleConnectors();
+ context.nextWork(pause);
+ }
+ };
public HTTPAcceptor()
{
@@ -87,18 +100,33 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
this.connectorIDLength = connectorIDLength;
}
+ public int getMaxIdleTime()
+ {
+ return maxIdleTime;
+ }
+
+ public void setMaxIdleTime(int maxIdleTime)
+ {
+ this.maxIdleTime = maxIdleTime;
+ }
+
+ public IHTTPConnector[] getHTTPConnectors()
+ {
+ List<IHTTPConnector> result = new ArrayList<IHTTPConnector>();
+ for (IConnector acceptedConnector : getAcceptedConnectors())
+ {
+ IHTTPConnector connector = (IHTTPConnector)acceptedConnector;
+ result.add(connector);
+ }
+
+ return result.toArray(new IHTTPConnector[result.size()]);
+ }
+
public IHTTPConnector[] handleList(String connectorID)
{
if (StringUtil.isEmpty(connectorID))
{
- List<IHTTPConnector> result = new ArrayList<IHTTPConnector>();
- for (IConnector acceptedConnector : getAcceptedConnectors())
- {
- IHTTPConnector connector = (IHTTPConnector)acceptedConnector;
- result.add(connector);
- }
-
- return result.toArray(new IHTTPConnector[result.size()]);
+ return getHTTPConnectors();
}
return new IHTTPConnector[] { httpConnectors.get(connectorID) };
@@ -109,7 +137,7 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
String connectorID = createConnectorID(userID);
System.out.println("HELLO " + userID + " (" + connectorID + ")");
- HTTPServerConnector connector = new HTTPServerConnector(this);
+ HTTPServerConnector connector = createServerConnector();
prepareConnector(connector);
connector.setConnectorID(connectorID);
connector.setUserID(userID);
@@ -137,28 +165,13 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
public void handleSendBuffer(String connectorID, short channelIndex, byte[] data)
{
- HTTPConnector connector = httpConnectors.get(connectorID);
+ HTTPServerConnector connector = httpConnectors.get(connectorID);
if (connector == null)
{
throw new IllegalArgumentException("Invalid connectorID: " + connectorID);
}
- InternalChannel channel = connector.getChannel(channelIndex);
- if (channel == null)
- {
- throw new IllegalArgumentException("Invalid channelIndex: " + channelIndex);
- }
-
- IBuffer buffer = getBufferProvider().provideBuffer();
- ByteBuffer byteBuffer = buffer.startPutting(channelIndex);
- for (int i = 0; i < data.length; i++)
- {
- System.out.println("Payload: " + data[i]);
- byteBuffer.put(data[i]);
- }
-
- buffer.flip();
- channel.handleBufferFromMultiplexer(buffer);
+ connector.handleBufferFromMultiplexer(channelIndex, data);
}
@Override
@@ -171,7 +184,7 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
public void addConnector(Connector connector)
{
super.addConnector(connector);
- HTTPConnector httpConnector = (HTTPConnector)connector;
+ HTTPServerConnector httpConnector = (HTTPServerConnector)connector;
httpConnectors.put(httpConnector.getConnectorID(), httpConnector);
}
@@ -191,8 +204,45 @@ public class HTTPAcceptor extends Acceptor implements IHTTPAcceptor, INet4jTrans
checkState(connectorIDLength > 0, "Constraint violated: connectorIDLength > 0");
}
+ @Override
+ protected void doActivate() throws Exception
+ {
+ super.doActivate();
+ cleaner.activate();
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ cleaner.deactivate();
+ super.doDeactivate();
+ }
+
protected String createConnectorID(String userID)
{
return randomizer.nextString(connectorIDLength, "0123456789ABCDEF");
}
+
+ protected HTTPServerConnector createServerConnector()
+ {
+ return new HTTPServerConnector(this);
+ }
+
+ protected int cleanIdleConnectors()
+ {
+ long now = System.currentTimeMillis();
+ for (IConnector connector : getAcceptedConnectors())
+ {
+ HTTPServerConnector serverConnector = (HTTPServerConnector)connector;
+ long lastTraffic = serverConnector.getLastTraffic();
+ long idleTime = now - lastTraffic;
+ if (idleTime > maxIdleTime)
+ {
+ serverConnector.deactivate();
+ OM.LOG.info("Deactivated idle HTTP server connector: " + serverConnector);
+ }
+ }
+
+ return maxIdleTime;
+ }
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
index 299969d..cdcc796 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPClientConnector.java
@@ -49,6 +49,8 @@ public class HTTPClientConnector extends HTTPConnector
private HttpClient httpClient;
+ private int maxIdleTime = UNKNOWN_MAX_IDLE_TIME;
+
public HTTPClientConnector()
{
}
@@ -68,6 +70,11 @@ public class HTTPClientConnector extends HTTPConnector
this.url = url;
}
+ public int getMaxIdleTime()
+ {
+ return maxIdleTime;
+ }
+
@Override
public void multiplexChannel(final IChannel channel)
{
@@ -216,6 +223,8 @@ public class HTTPClientConnector extends HTTPConnector
public void handleIn(ExtendedDataInputStream in) throws IOException
{
String connectorID = in.readString();
+ maxIdleTime = in.readInt();
+
setConnectorID(connectorID);
leaveConnecting();
}
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
index 98b4e22..1e3e3a0 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/HTTPServerConnector.java
@@ -10,11 +10,15 @@
**************************************************************************/
package org.eclipse.net4j.internal.http;
+import org.eclipse.net4j.buffer.IBuffer;
import org.eclipse.net4j.channel.IChannel;
import org.eclipse.net4j.connector.ConnectorException;
import org.eclipse.net4j.connector.ConnectorLocation;
import org.eclipse.net4j.protocol.IProtocol;
+import org.eclipse.internal.net4j.channel.InternalChannel;
+
+import java.nio.ByteBuffer;
import java.text.MessageFormat;
/**
@@ -24,11 +28,12 @@ public class HTTPServerConnector extends HTTPConnector
{
private HTTPAcceptor acceptor;
- private long lastTraffic = System.currentTimeMillis();
+ private long lastTraffic;
public HTTPServerConnector(HTTPAcceptor acceptor)
{
this.acceptor = acceptor;
+ markLastTraffic();
}
public HTTPAcceptor getAcceptor()
@@ -46,16 +51,48 @@ public class HTTPServerConnector extends HTTPConnector
return "agent://connector:" + getConnectorID();
}
+ public int getMaxIdleTime()
+ {
+ return acceptor.getMaxIdleTime();
+ }
+
public long getLastTraffic()
{
return lastTraffic;
}
+ private void markLastTraffic()
+ {
+ lastTraffic = System.currentTimeMillis();
+ }
+
+ @Override
public void multiplexChannel(IChannel channel)
{
throw new UnsupportedOperationException();
}
+ public void handleBufferFromMultiplexer(short channelIndex, byte[] data)
+ {
+ InternalChannel channel = getChannel(channelIndex);
+ if (channel == null)
+ {
+ throw new IllegalArgumentException("Invalid channelIndex: " + channelIndex);
+ }
+
+ IBuffer buffer = getBufferProvider().provideBuffer();
+ ByteBuffer byteBuffer = buffer.startPutting(channelIndex);
+ for (int i = 0; i < data.length; i++)
+ {
+ System.out.println("Payload: " + data[i]);
+ byteBuffer.put(data[i]);
+ }
+
+ buffer.flip();
+ channel.handleBufferFromMultiplexer(buffer);
+ markLastTraffic();
+ }
+
@Override
public String toString()
{
diff --git a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
index a2d31dc..d60526b 100644
--- a/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
+++ b/plugins/org.eclipse.net4j.http/src/org/eclipse/net4j/internal/http/tests/HTTPTest.java
@@ -120,6 +120,7 @@ public class HTTPTest extends AbstractTransportTest
IntRequest request = new IntRequest(channel, 305419896);
int result = request.send();
assertEquals(305419896, result);
+ channel.close();
}
private IHTTPConnector getHTTPConnector()