summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaspar De Groot2011-02-23 00:47:43 (EST)
committerCaspar De Groot2011-02-23 00:47:43 (EST)
commit40b806681aa48dcbcf5c94c3d01fee49a8b81315 (patch)
treee0204761d78411e36f1ab2cc3f01b76b0bccffb2
parentbaad70b607cd76bcbbd5dcb884eb697d6e37042f (diff)
downloadcdo-40b806681aa48dcbcf5c94c3d01fee49a8b81315.zip
cdo-40b806681aa48dcbcf5c94c3d01fee49a8b81315.tar.gz
cdo-40b806681aa48dcbcf5c94c3d01fee49a8b81315.tar.bz2
[Bug 337095] Improve reusability of TCP classes
https://bugs.eclipse.org/bugs/show_bug.cgi?id=337095
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java5
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java16
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPClientConnector.java13
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnector.java61
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java22
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/buffer/Buffer.java21
6 files changed, 112 insertions, 26 deletions
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java
index eadd284..ee9f671 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.net4j.internal.tcp;
@@ -183,7 +184,7 @@ public class TCPAcceptor extends Acceptor implements ITCPAcceptor, ITCPPassiveSe
// socketChannel.socket().setKeepAlive(true);
socketChannel.configureBlocking(false);
- TCPServerConnector connector = createConnector();
+ TCPConnector connector = createConnector();
prepareConnector(connector);
connector.setSocketChannel(socketChannel);
connector.setSelector(selector);
@@ -211,7 +212,7 @@ public class TCPAcceptor extends Acceptor implements ITCPAcceptor, ITCPPassiveSe
return MessageFormat.format("TCPAcceptor[{0}:{1}]", address, port); //$NON-NLS-1$
}
- protected TCPServerConnector createConnector()
+ protected TCPConnector createConnector()
{
return new TCPServerConnector(this);
}
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java
index 5c96798..bd8d472 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.net4j.internal.tcp;
@@ -29,6 +30,14 @@ public class TCPAcceptorFactory extends AcceptorFactory
super(TYPE);
}
+ /**
+ * Allows derived classes to override the TYPE identifier
+ */
+ protected TCPAcceptorFactory(String type)
+ {
+ super(type);
+ }
+
public TCPAcceptor create(String description)
{
String address = ITCPAcceptor.DEFAULT_ADDRESS;
@@ -48,12 +57,17 @@ public class TCPAcceptorFactory extends AcceptorFactory
}
}
- TCPAcceptor acceptor = new TCPAcceptor();
+ TCPAcceptor acceptor = createAcceptor();
acceptor.setAddress(address);
acceptor.setPort(port);
return acceptor;
}
+ protected TCPAcceptor createAcceptor()
+ {
+ return new TCPAcceptor();
+ }
+
@Override
public String getDescriptionFor(Object object)
{
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPClientConnector.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPClientConnector.java
index 5fe6fff..7711c70 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPClientConnector.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPClientConnector.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.net4j.internal.tcp;
@@ -47,18 +48,6 @@ public class TCPClientConnector extends TCPConnector
}
@Override
- public void setHost(String host)
- {
- super.setHost(host);
- }
-
- @Override
- public void setPort(int port)
- {
- super.setPort(port);
- }
-
- @Override
public String toString()
{
if (getUserID() == null)
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnector.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnector.java
index 1e578f3..78dbbb4 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnector.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnector.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.net4j.internal.tcp;
@@ -75,7 +76,7 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
return host;
}
- void setHost(String host)
+ public void setHost(String host)
{
this.host = host;
}
@@ -85,7 +86,7 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
return port;
}
- void setPort(int port)
+ public void setPort(int port)
{
this.port = port;
}
@@ -113,11 +114,51 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
this.socketChannel = socketChannel;
}
+ public SelectionKey getSelectionKey()
+ {
+ return selectionKey;
+ }
+
+ public void setSelectionKey(SelectionKey selectionKey)
+ {
+ this.selectionKey = selectionKey;
+ }
+
+ public BlockingQueue<InternalChannel> getWriteQueue()
+ {
+ return writeQueue;
+ }
+
+ public void setWriteQueue(BlockingQueue<InternalChannel> writeQueue)
+ {
+ this.writeQueue = writeQueue;
+ }
+
+ public IBuffer getInputBuffer()
+ {
+ return inputBuffer;
+ }
+
+ public void setInputBuffer(IBuffer inputBuffer)
+ {
+ this.inputBuffer = inputBuffer;
+ }
+
+ public ControlChannel getControlChannel()
+ {
+ return controlChannel;
+ }
+
+ public void setControlChannel(ControlChannel controlChannel)
+ {
+ this.controlChannel = controlChannel;
+ }
+
@Override
public String getURL()
{
StringBuilder builder = new StringBuilder();
- builder.append("tcp://");
+ builder.append(getProtocolString());
builder.append(host);
if (port != DEFAULT_PORT)
{
@@ -128,6 +169,11 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
return builder.toString();
}
+ public String getProtocolString()
+ {
+ return "tcp://";
+ }
+
public void handleRegistration(ITCPSelector selector, SocketChannel socketChannel)
{
try
@@ -242,7 +288,7 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
{
if (selectionKey != null)
{
- selector.orderWriteInterest(selectionKey, isClient(), true);
+ doOrderWriteInterest(true);
}
}
}
@@ -277,7 +323,7 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
{
if (selectionKey != null)
{
- selector.orderWriteInterest(selectionKey, isClient(), false);
+ doOrderWriteInterest(false);
}
}
}
@@ -304,6 +350,11 @@ public abstract class TCPConnector extends Connector implements ITCPConnector, I
}
}
+ protected void doOrderWriteInterest(boolean on)
+ {
+ selector.orderWriteInterest(selectionKey, isClient(), on);
+ }
+
@Override
protected void registerChannelWithPeer(short channelID, long timeout, IProtocol<?> protocol) throws ChannelException
{
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java
index 49b7870..0f818dd 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.net4j.internal.tcp;
@@ -33,7 +34,15 @@ public class TCPConnectorFactory extends ConnectorFactory
super(TYPE);
}
- public TCPClientConnector create(String description)
+ /**
+ * Allows derived classes to override the TYPE identifier
+ */
+ protected TCPConnectorFactory(String type)
+ {
+ super(type);
+ }
+
+ public TCPConnector create(String description)
{
try
{
@@ -48,7 +57,7 @@ public class TCPConnectorFactory extends ConnectorFactory
port = ITCPConnector.DEFAULT_PORT;
}
- TCPClientConnector connector = new TCPClientConnector();
+ TCPConnector connector = createConnector();
connector.setUserID(userID);
connector.setHost(host);
connector.setPort(port);
@@ -60,12 +69,17 @@ public class TCPConnectorFactory extends ConnectorFactory
}
}
+ protected TCPConnector createConnector()
+ {
+ return new TCPClientConnector();
+ }
+
@Override
public String getDescriptionFor(Object object)
{
- if (object instanceof TCPClientConnector)
+ if (object instanceof TCPConnector)
{
- TCPClientConnector connector = (TCPClientConnector)object;
+ TCPConnector connector = (TCPConnector)object;
String description = connector.getHost();
String userID = connector.getUserID();
if (!StringUtil.isEmpty(userID))
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/buffer/Buffer.java b/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/buffer/Buffer.java
index f58ba8e..cef0924 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/buffer/Buffer.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/buffer/Buffer.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
*/
package org.eclipse.internal.net4j.buffer;
@@ -31,10 +32,11 @@ import java.text.MessageFormat;
/**
* @author Eike Stepper
+ * @since 4.0
*/
public class Buffer implements InternalBuffer
{
- private static final int EOS_OFFSET = 1;
+ public static final int EOS_OFFSET = 1;
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_BUFFER, Buffer.class);
@@ -86,6 +88,11 @@ public class Buffer implements InternalBuffer
return channelID;
}
+ public void setChannelID(short channelID)
+ {
+ this.channelID = channelID;
+ }
+
public short getCapacity()
{
return (short)byteBuffer.capacity();
@@ -96,6 +103,11 @@ public class Buffer implements InternalBuffer
return state;
}
+ public void setState(BufferState state)
+ {
+ this.state = state;
+ }
+
public ByteBuffer getByteBuffer()
{
if (state != BufferState.GETTING && state != BufferState.PUTTING)
@@ -106,6 +118,11 @@ public class Buffer implements InternalBuffer
return byteBuffer;
}
+ public void setByteBuffer(ByteBuffer buffer)
+ {
+ byteBuffer = buffer;
+ }
+
public void clear()
{
state = BufferState.INITIAL;
@@ -389,7 +406,7 @@ public class Buffer implements InternalBuffer
this.errorHandler = errorHandler;
}
- private void handleError(Throwable t)
+ public void handleError(Throwable t)
{
if (errorHandler != null)
{