You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/31 13:21:54 UTC
svn commit: r1129629 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/net/ native/include/acr/ native/os/unix/
test/org/apache/commons/runtime/
Author: mturk
Date: Tue May 31 11:21:54 2011
New Revision: 1129629
URL: http://svn.apache.org/viewvc?rev=1129629&view=rev
Log:
Add private abstract selector and move registration to endpoints
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java
- copied, changed from r1129030, commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOp.java
Removed:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOp.java
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/PosixSelector.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java?rev=1129629&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java Tue May 31 11:21:54 2011
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.commons.runtime.net;
+
+import java.io.IOException;
+import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.InvalidDescriptorException;
+import org.apache.commons.runtime.OverflowException;
+
+/**
+ * Wait for some event on set of descriptors.
+ */
+abstract class AbstractSelector extends Selector
+{
+ /**
+ * Creates a new object instance.
+ */
+ protected AbstractSelector(int capacity)
+ {
+ super(capacity);
+ }
+
+ /**
+ * Queue the given key to this selector.
+ */
+ protected abstract SelectionKey queue(SelectionKeyImpl key, Descriptor sd)
+ throws ClosedSelectorException,
+ OverflowException,
+ IOException;
+
+ /**
+ * Unregister the selection key from this selector.
+ *
+ * @throws ClosedSelectorException if this selector is closed.
+ * @throws IllegalSelectorException if the key was not created by the same
+ * selector as this selector.
+ */
+ protected abstract void cancel(SelectionKey key)
+ throws ClosedSelectorException,
+ IllegalSelectorException;
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java Tue May 31 11:21:54 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
import java.net.SocketException;
import org.apache.commons.runtime.io.ClosedDescriptorException;
import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.OverflowException;
/**
* This class represents a network endpoint.
@@ -87,7 +88,15 @@ public abstract class Endpoint implement
*/
public abstract boolean isBlocking()
throws IOException;
-
+
+ /**
+ * Indicates whether this channel is registered with at least one selector.
+ *
+ * @return {@code true} if this channel is registered, {@code false}
+ * otherwise.
+ */
+ public abstract boolean isRegistered();
+
/**
* Free the allocated resource by the Operating system.
* <p>
@@ -102,18 +111,85 @@ public abstract class Endpoint implement
throws IOException;
/**
- * Returns the key representing this endpoint's registration with
+ * Returns the key representing the endpoint's registration with
* the given selector.
*
* @param selector the selector.
*
* @return A key representing the registration of this endpoint
- * with the given selector.
- * @throws NullPonterException if the selector is {@code null}
- * @throws IllegalSelectorException if the endpoint is already
- * registered with a different selector.
+ * with the given selector or {@code null} if this endpoint
+ * is not currently registered with that selector.
*/
- public abstract SelectionKey key(Selector selector)
- throws NullPointerException, IllegalSelectorException;
+ public abstract SelectionKey keyFor(Selector selector);
+ /**
+ * Registers this endpoint with the specified selector for the specified
+ * selection ops and an object to attach. If the channel is already
+ * registered with the selector, the corresponding selection key is returned
+ * but its {@link SelectionOps interest set} is updated to {@code ops} and
+ * the attached object is updated to {@code att}. The returned key is
+ * canceled if the channel is closed while registering is in progress.
+ * <p>
+ * Calling this method is valid at any time. If another thread executes this
+ * method or the {@code configureBlocking(boolean)} method then this call is
+ * blocked until the other call finishes. After that, it will synchronize on
+ * the key set of the selector and thus may again block if other threads
+ * also hold locks on the key set of the same selector.
+ *
+ * @param selector the selector with which to register this endpoint.
+ * @param ops this endpoints's {@link SelectionOps interest set}.
+ * @param att the object to attach, can be {@code null} in which case the
+ * previous attachment is not modified.
+ *
+ * @return the selection key for this registration.
+ *
+ * @throws ClosedDescriptorException if the endpoint is closed.
+ * @throws ClosedSelectorException if the selector is closed.
+ * @throws IllegalSelectorException if the key was not created by the same
+ * selector as this selector.
+ * @throws OverflowException if the selector reached it's capacity.
+ */
+ public abstract SelectionKey register(Selector selector, int ops, Object att)
+ throws ClosedSelectorException,
+ IllegalSelectorException,
+ ClosedDescriptorException,
+ OverflowException,
+ IOException;
+
+ /**
+ * Registers this endpoint with the specified selector for the specified
+ * selection ops. If the channel is already
+ * registered with the selector, the corresponding selection key is returned
+ * but its {@link SelectionOps interest set} is updated to {@code ops}.
+ * The returned key is canceled if the channel is closed while registering
+ * is in progress.
+ * <p>
+ * Calling this method is valid at any time. If another thread executes this
+ * method or the {@code configureBlocking(boolean)} method then this call is
+ * blocked until the other call finishes. After that, it will synchronize on
+ * the key set of the selector and thus may again block if other threads
+ * also hold locks on the key set of the same selector.
+ *
+ * @param selector the selector with which to register this endpoint.
+ * @param ops this endpoints's {@link SelectionOps interest set}.
+ *
+ * @return the selection key for this registration.
+ *
+ * @throws ClosedDescriptorException if the endpoint is closed.
+ * @throws ClosedSelectorException if the selector is closed.
+ * @throws IllegalSelectorException if the key was not created by the same
+ * selector as this selector.
+ * @throws OverflowException if the selector reached it's capacity.
+ */
+ public final SelectionKey register(Selector selector, int ops)
+ throws ClosedSelectorException,
+ IllegalSelectorException,
+ ClosedDescriptorException,
+ OverflowException,
+ IOException
+ {
+ return register(selector, ops, null);
+ }
+
+
}
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java Tue May 31 11:21:54 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
import java.net.SocketException;
import org.apache.commons.runtime.io.ClosedDescriptorException;
import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.OverflowException;
import org.apache.commons.runtime.Status;
/**
@@ -73,10 +74,10 @@ public class LocalSocketEndpoint extends
{
if (!sd.valid())
throw new ClosedDescriptorException();
+ if (isRegistered())
+ throw new IllegalBlockingModeException();
if (blocking == block)
return sd;
- if (key != null && key.selected)
- throw new IllegalBlockingModeException();
int rc = nonblock0(sd.fd(), block);
if (rc != 0)
throw new SocketException(Status.describe(rc));
@@ -92,25 +93,66 @@ public class LocalSocketEndpoint extends
}
@Override
+ public boolean isRegistered()
+ {
+ synchronized(this) {
+ if (key != null && key.selected)
+ return true;
+ else
+ return false;
+ }
+ }
+
+ @Override
public void close()
throws IOException
{
- sd.close();
+ synchronized(this) {
+ if (sd.valid()) {
+ if (key != null && key.selected) {
+ try {
+ key.cancel();
+ } catch (Exception e) {
+ // Ignore selector exceptions
+ }
+ key = null;
+ }
+ }
+ sd.close();
+ }
+ }
+
+ @Override
+ public SelectionKey keyFor(Selector selector)
+ {
+ synchronized(this) {
+ if (key != null && key.selector == selector)
+ return key;
+ else
+ return null;
+ }
}
@Override
- public SelectionKey key(Selector selector)
- throws NullPointerException, IllegalSelectorException
+ public SelectionKey register(Selector selector, int ops, Object att)
+ throws ClosedSelectorException,
+ IllegalSelectorException,
+ ClosedDescriptorException,
+ OverflowException,
+ IOException
{
- if (selector == null)
- throw new NullPointerException(Local.sm.get("selector.NULL"));
+ AbstractSelector sel = (AbstractSelector)selector;
synchronized(this) {
+ if (!sd.valid())
+ throw new ClosedDescriptorException();
if (key == null)
- key = new SelectionKeyImpl(selector, this);
- if (key.selector != selector)
+ key = new SelectionKeyImpl(sel, this, ops);
+ if (key.selector != sel)
throw new IllegalSelectorException();
+ if (att != null)
+ key.attach(att);
}
- return key;
+ return sel.queue(key, sd);
}
}
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java Tue May 31 11:21:54 2011
@@ -35,37 +35,37 @@ public final class Poll
/**
* There is data to read
*/
- public static final short POLLIN = SelectionOp.READ;
+ public static final short POLLIN = 0x0001;
/**
* There is urgent data to read.
* e.g., out-of-band data on TCP socket; pseudo-terminal master in packet mode
* has seen state change in slave.
*/
- public static final short POLLPRI = SelectionOp.PRI;
+ public static final short POLLPRI = 0x0008;
/**
* Writing now will not block.
*/
- public static final short POLLOUT = SelectionOp.WRITE;
+ public static final short POLLOUT = 0x0010;
/**
* A Stream socket peer closed connection, or shut down writing half of connection.
*/
- public static final short POLLRDHUP = SelectionOp.RDHUP;
+ public static final short POLLRDHUP = 0x0020;
/**
* A stream-oriented connection was either diconnected or aborted.
*/
- public static final short POLLHUP = SelectionOp.HANGUP;
+ public static final short POLLHUP = 0x0040;
/**
* An error has occurred.
*/
- public static final short POLLERR = SelectionOp.ERROR;
+ public static final short POLLERR = 0x0100;
/**
* An invalid descriptor was used.
*/
- public static final short POLLNVAL = SelectionOp.NVAL;
+ public static final short POLLNVAL = 0x0200;
/**
* Time-To-Live event occurred.
*/
- public static final short POLLTTL = SelectionOp.TIMEOUT;
+ public static final short POLLTTL = 0x0400;
private static native int wait0(int[] fds, short[] events, short[] revents,
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/PosixSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/PosixSelector.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/PosixSelector.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/PosixSelector.java Tue May 31 11:21:54 2011
@@ -36,7 +36,7 @@ import org.apache.commons.runtime.Status
*
* @since Runtime 1.0
*/
-final class PosixSelector extends Selector
+final class PosixSelector extends AbstractSelector
{
private short[] revents;
@@ -51,7 +51,7 @@ final class PosixSelector extends Select
private static native int size0(long pollset);
private static native int add0(long pollset, SelectionKeyImpl key, int fd, int events, int ttl);
private static native int del0(long pollset, SelectionKeyImpl key, int fd);
- private static native int clear0(long pollset, SelectionKeyImpl[] set);
+ private static native int clr0(long pollset, SelectionKeyImpl[] set);
private static native int wait0(long pollset, SelectionKeyImpl[] set, short[] events,
int timeout, boolean autocancel);
@@ -90,33 +90,25 @@ final class PosixSelector extends Select
}
@Override
- public boolean register(SelectionKey key, int ops)
+ public SelectionKey queue(SelectionKeyImpl key, Descriptor sd)
throws ClosedSelectorException,
- IllegalSelectorException,
- ClosedDescriptorException,
OverflowException,
IOException
{
ensureValid();
- SelectionKeyImpl skey = (SelectionKeyImpl)key;
- if (skey.selector != this)
- throw new IllegalSelectorException();
- Descriptor sd = skey.endpoint.descriptor();
- if (!sd.valid())
- throw new ClosedDescriptorException();
- skey.selected = true;
- int rc = add0(pollset, skey, sd.fd(), ops, skey.timeout());
+ key.selected = true;
+ int rc = add0(pollset, key, sd.fd(), key.ievents, key.timeout());
if (rc != 0) {
// Add failed
- skey.selected = false;
- if (rc == Errno.EALREADY)
- return false;
- if (rc == Errno.EOVERFLOW)
- throw new OverflowException();
- else
- throw new IOException(Status.describe(rc));
+ key.selected = false;
+ if (rc != Errno.EALREADY) {
+ if (rc == Errno.EOVERFLOW)
+ throw new OverflowException();
+ else
+ throw new IOException(Status.describe(rc));
+ }
}
- return true;
+ return key;
}
@Override
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java Tue May 31 11:21:54 2011
@@ -29,6 +29,27 @@ public abstract class SelectionKey
private Object attachment;
private int timeout;
+ /** Accept operation.
+ * <p>
+ * </p>
+ */
+ public static final int OP_ACCEPT = 0x0001;
+ /** Connect operation.
+ * <p>
+ * </p>
+ */
+ public static final int OP_CONNECT = 0x0002;
+ /** Read operation.
+ * <p>
+ * </p>
+ */
+ public static final int OP_READ = 0x0004;
+ /** Write operation.
+ * <p>
+ * </p>
+ */
+ public static final int OP_WRITE = 0x0010;
+
/**
* Constructs a new {@code SelectionKey} for given selector and enpoint
*/
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java Tue May 31 11:21:54 2011
@@ -36,11 +36,16 @@ final class SelectionKeyImpl extends Sel
public int ievents;
public int revents;
- public Selector selector;
+ public AbstractSelector selector;
public Endpoint endpoint;
public volatile boolean selected;
- public SelectionKeyImpl(Selector selector, Endpoint endpoint,
+ private static native void init0();
+ static {
+ init0();
+ }
+
+ public SelectionKeyImpl(AbstractSelector selector, Endpoint endpoint,
int ievents)
{
this.selector = selector;
@@ -50,7 +55,7 @@ final class SelectionKeyImpl extends Sel
this.selected = false;
}
- public SelectionKeyImpl(Selector selector, Endpoint endpoint)
+ public SelectionKeyImpl(AbstractSelector selector, Endpoint endpoint)
{
this(selector, endpoint, 0);
}
@@ -81,7 +86,11 @@ final class SelectionKeyImpl extends Sel
selector.cancel(this);
}
- public void invalidate()
+ /**
+ * Called from the Selector.close() native method for all currently
+ * registered keys.
+ */
+ private void detach()
{
revents = 0;
selected = false;
Copied: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java (from r1129030, commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOp.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java?p2=commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java&p1=commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOp.java&r1=1129030&r2=1129629&rev=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOp.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java Tue May 31 11:21:54 2011
@@ -19,10 +19,10 @@ package org.apache.commons.runtime.net;
/**
* Selection operation type flags.
*/
-public final class SelectionOp
+final class SelectionOps
{
- private SelectionOp()
+ private SelectionOps()
{
// No Instance
}
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java Tue May 31 11:21:54 2011
@@ -128,35 +128,6 @@ public abstract class Selector implement
throws ClosedSelectorException;
/**
- * Registers the selection key with this selector.
- *
- * @return {@code true} if the key was registered, {@code false} if
- * it was already registered.
- *
- * @throws ClosedDescriptorException if the key's endpoint is closed.
- * @throws ClosedSelectorException if this selector is closed.
- * @throws IllegalSelectorException if the key was not created by the same
- * selector as this selector.
- * @throws OverflowException if the selector reached it's capacity.
- */
- protected abstract boolean register(SelectionKey key, int ops)
- throws ClosedDescriptorException,
- IllegalSelectorException,
- ClosedSelectorException,
- OverflowException,
- IOException;
- /**
- * Unregister the selection key from this selector.
- *
- * @throws ClosedSelectorException if this selector is closed.
- * @throws IllegalSelectorException if the key was not created by the same
- * selector as this selector.
- */
- protected abstract void cancel(SelectionKey key)
- throws ClosedSelectorException,
- IllegalSelectorException;
-
- /**
* Closes this selector. Ongoing calls to the {@code select} methods of this
* selector will get interrupted. This interruption behaves as if the
* {@link #interrupt()} method of this selector is called. After this, all keys
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java Tue May 31 11:21:54 2011
@@ -25,6 +25,7 @@ import java.io.SyncFailedException;
import java.net.SocketException;
import org.apache.commons.runtime.io.ClosedDescriptorException;
import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.OverflowException;
import org.apache.commons.runtime.Status;
/**
@@ -62,6 +63,17 @@ public class SocketEndpoint extends Endp
}
@Override
+ public boolean isRegistered()
+ {
+ synchronized(this) {
+ if (key != null && key.selected)
+ return true;
+ else
+ return false;
+ }
+ }
+
+ @Override
public Descriptor configureBlocking(boolean block)
throws ClosedDescriptorException,
IllegalBlockingModeException,
@@ -69,10 +81,10 @@ public class SocketEndpoint extends Endp
{
if (!sd.valid())
throw new ClosedDescriptorException();
+ if (isRegistered())
+ throw new IllegalBlockingModeException();
if (blocking == block)
return sd;
- if (key != null && key.selected)
- throw new IllegalBlockingModeException();
int rc = nonblock0(sd.fd(), block);
if (rc != 0)
throw new SocketException(Status.describe(rc));
@@ -91,21 +103,52 @@ public class SocketEndpoint extends Endp
public void close()
throws IOException
{
- key = null;
- sd.close();
+ synchronized(this) {
+ if (sd.valid()) {
+ if (key != null && key.selected) {
+ try {
+ key.cancel();
+ } catch (Exception e) {
+ // Ignore selector exceptions
+ }
+ key = null;
+ }
+ }
+ sd.close();
+ }
}
@Override
- public SelectionKey key(Selector selector)
- throws NullPointerException, IllegalSelectorException
+ public SelectionKey keyFor(Selector selector)
+ {
+ synchronized(this) {
+ if (key != null && key.selector == selector)
+ return key;
+ else
+ return null;
+ }
+ }
+
+ @Override
+ public SelectionKey register(Selector selector, int ops, Object att)
+ throws ClosedSelectorException,
+ IllegalSelectorException,
+ ClosedDescriptorException,
+ OverflowException,
+ IOException
{
- if (selector == null)
- throw new NullPointerException(Local.sm.get("selector.NULL"));
- if (key == null)
- key = new SelectionKeyImpl(selector, this);
- if (key.selector != selector)
- throw new IllegalSelectorException();
- return key;
+ AbstractSelector sel = (AbstractSelector)selector;
+ synchronized(this) {
+ if (!sd.valid())
+ throw new ClosedDescriptorException();
+ if (key == null)
+ key = new SelectionKeyImpl(sel, this, ops);
+ if (key.selector != sel)
+ throw new IllegalSelectorException();
+ if (att != null)
+ key.attach(att);
+ }
+ return sel.queue(key, sd);
}
}
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h Tue May 31 11:21:54 2011
@@ -29,6 +29,7 @@ ACR_CLASS_DTOR(HashSet);
int AcrArrayListAdd(JNI_STDARGS, jobject e);
int AcrHashSetAdd(JNI_STDARGS, jobject e);
+void AcrSelectionKeyDetach(JNI_STDARGS);
void AcrLibLockAcquire(void);
void AcrLibLockRelease(void);
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c Tue May 31 11:21:54 2011
@@ -19,41 +19,33 @@
#include "acr/misc.h"
#include "arch_opts.h"
-#if POLLSET_USE_POLL
-#define SELKEY_CP ACR_UNX_CP
-#endif
-
J_DECLARE_CLAZZ = {
INVALID_FIELD_OFFSET,
0,
0,
0,
- SELKEY_CP "SelectionKeyImpl"
+ ACR_NET_CP "SelectionKeyImpl"
};
J_DECLARE_M_ID(0000) = {
0,
- "<init>",
- "(L" ACR_NET_CP "Selector;L" ACR_NET_CP "Endpoint;)V"
+ "detach",
+ "()V"
};
-ACR_NET_EXPORT(jboolean, SelectionKeyFactory, init0)(JNI_STDARGS)
+ACR_NET_EXPORT(void, SelectionKeyImpl, init0)(JNI_STDARGS)
{
- if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
- return JNI_FALSE;
- J_LOAD_METHOD(0000);
+ _clazzn.i = (jclass)(*env)->NewGlobalRef(env, obj);
+ if (_clazzn.i == 0)
+ return;
+ V_LOAD_METHOD(0000);
_clazzn.u = 1;
- return JNI_TRUE;
}
-ACR_NET_EXPORT(jobject, SelectionKeyFactory, new0)(JNI_STDARGS,
- jobject selector,
- jobject endpoint)
+void
+AcrSelectionKeyDetach(JNI_STDARGS)
{
- if (_clazzn.u == 1)
- return (*env)->NewObject(env, _clazzn.i, J4MID(0000), selector, endpoint);
- else
- return 0;
+ if (CLAZZ_LOADED) {
+ CALL_VMETHOD0(0000, obj);
+ }
}
-
-
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c Tue May 31 11:21:54 2011
@@ -188,6 +188,7 @@ ACR_NET_EXPORT(int, PosixSelector, destr
}
ps->state = PSS_DESTROY;
for (i = 1; i < ps->used; i++) {
+ AcrSelectionKeyDetach(env, ps->ooset[i].obj);
/* Invalidate the container. */
(*env)->DeleteGlobalRef(env, ps->ooset[i].obj);
}
@@ -203,8 +204,8 @@ ACR_NET_EXPORT(int, PosixSelector, destr
return rc;
}
-ACR_NET_EXPORT(jint, PosixSelector, clear0)(JNI_STDARGS, jlong pollset,
- jobjectArray rs)
+ACR_NET_EXPORT(jint, PosixSelector, clr0)(JNI_STDARGS, jlong pollset,
+ jobjectArray rs)
{
int i;
int cnt = 0;
@@ -267,8 +268,8 @@ ACR_NET_EXPORT(jint, PosixSelector, size
}
ACR_NET_EXPORT(jint, PosixSelector, wait0)(JNI_STDARGS, jlong pollset,
- jobjectArray rs, jshortArray revents,
- jint timeout, jboolean autocancel)
+ jobjectArray rs, jshortArray revents,
+ jint timeout, jboolean autocancel)
{
int i, ns, rc = 0;
int rv = 0;
@@ -424,7 +425,7 @@ ACR_NET_EXPORT(jint, PosixSelector, wait
}
ACR_NET_EXPORT(jint, PosixSelector, add0)(JNI_STDARGS, jlong pollset, jobject fo,
- jint f, jint events, jint ttlms)
+ jint f, jint events, jint ttlms)
{
int i, rc = 0;
acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
@@ -483,7 +484,7 @@ cleanup:
}
ACR_NET_EXPORT(jint, PosixSelector, del0)(JNI_STDARGS, jlong pollset,
- jobject fo, jint f)
+ jobject fo, jint f)
{
int i, rc = ACR_EOF;
acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java?rev=1129629&r1=1129628&r2=1129629&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java Tue May 31 11:21:54 2011
@@ -25,7 +25,7 @@ public class TestSelectionKey extends As
{
@Test(groups = { "core" })
- public void createSelectionKey()
+ public void createSelector()
throws IOException
{
// Create socket bound to the first free port
@@ -34,8 +34,6 @@ public class TestSelectionKey extends As
Selector ss = Selector.newInstance();
assertNotNull(ss);
System.out.println("SocketSelector capacity=" + ss.capacity());
- SelectionKey skey = ep.key(ss);
- assertNotNull(skey);
ss.close();
}