summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-03-03 03:28:33 (EST)
committerEike Stepper2007-03-03 03:28:33 (EST)
commit04a5120cb7b07cabf64a62ff3ab6dca5744c680d (patch)
treedd99b29f6bd643a7fa645cfb0161cc14ac617a56
parent0b7d5c3e7989691e4142c0989e1a2baf2d9cbab2 (diff)
downloadcdo-04a5120cb7b07cabf64a62ff3ab6dca5744c680d.zip
cdo-04a5120cb7b07cabf64a62ff3ab6dca5744c680d.tar.gz
cdo-04a5120cb7b07cabf64a62ff3ab6dca5744c680d.tar.bz2
acceptor can startup (register with selector) synchronously.
synchronous startup is default.
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorImpl.java41
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPSelectorImpl.java4
-rw-r--r--plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPSelectorListener.java8
3 files changed, 48 insertions, 5 deletions
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorImpl.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorImpl.java
index 41cb948..df3ed9b 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorImpl.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorImpl.java
@@ -25,9 +25,11 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.MessageFormat;
+import java.util.concurrent.CountDownLatch;
/**
* @author Eike Stepper
@@ -40,6 +42,12 @@ public class TCPAcceptorImpl extends AbstractAcceptor implements TCPAcceptor, TC
private TCPSelectorImpl selector;
+ private SelectionKey selectionKey;
+
+ private boolean startSynchronously = true;
+
+ private CountDownLatch startLatch;
+
private ServerSocketChannel serverSocketChannel;
private String address;
@@ -70,6 +78,30 @@ public class TCPAcceptorImpl extends AbstractAcceptor implements TCPAcceptor, TC
this.selector = selector;
}
+ public boolean isStartSynchronously()
+ {
+ return startSynchronously;
+ }
+
+ public void setStartSynchronously(boolean startSynchronously)
+ {
+ this.startSynchronously = startSynchronously;
+ }
+
+ public SelectionKey getSelectionKey()
+ {
+ return selectionKey;
+ }
+
+ public void registered(SelectionKey selectionKey)
+ {
+ this.selectionKey = selectionKey;
+ if (startSynchronously)
+ {
+ startLatch.countDown();
+ }
+ }
+
public void handleAccept(TCPSelector selector, ServerSocketChannel serverSocketChannel)
{
try
@@ -180,7 +212,16 @@ public class TCPAcceptorImpl extends AbstractAcceptor implements TCPAcceptor, TC
}
}
+ if (startSynchronously)
+ {
+ startLatch = new CountDownLatch(1);
+ }
+
selector.registerAsync(serverSocketChannel, this);
+ if (startSynchronously)
+ {
+ startLatch.await();
+ }
}
@Override
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPSelectorImpl.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPSelectorImpl.java
index 70eeb49..a17205d 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPSelectorImpl.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPSelectorImpl.java
@@ -343,7 +343,9 @@ public class TCPSelectorImpl extends LifecycleImpl implements TCPSelector, Runna
try
{
- channel.register(selector, SelectionKey.OP_ACCEPT, listener);
+ int interest = SelectionKey.OP_ACCEPT;
+ SelectionKey selectionKey = channel.register(selector, interest, listener);
+ listener.registered(selectionKey);
}
catch (ClosedChannelException ignore)
{
diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPSelectorListener.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPSelectorListener.java
index a83835f..7bf7290 100644
--- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPSelectorListener.java
+++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPSelectorListener.java
@@ -19,10 +19,12 @@ import java.nio.channels.SocketChannel;
*/
public interface TCPSelectorListener
{
+ public void registered(SelectionKey selectionKey);
+
/**
* @author Eike Stepper
*/
- public interface Passive
+ public interface Passive extends TCPSelectorListener
{
public void handleAccept(TCPSelector selector, ServerSocketChannel serverSocketChannel);
}
@@ -30,10 +32,8 @@ public interface TCPSelectorListener
/**
* @author Eike Stepper
*/
- public interface Active
+ public interface Active extends TCPSelectorListener
{
- public void registered(SelectionKey selectionKey);
-
public void handleConnect(TCPSelector selector, SocketChannel channel);
public void handleRead(TCPSelector selector, SocketChannel socketChannel);