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/06/02 10:33:09 UTC

svn commit: r1130435 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/include/acr/ native/os/unix/ native/shared/

Author: mturk
Date: Thu Jun  2 08:33:08 2011
New Revision: 1130435

URL: http://svn.apache.org/viewvc?rev=1130435&view=rev
Log:
Add selection key queue method

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/LocalSocketDescriptor.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/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/SocketDescriptor.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/include/acr/netapi.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
    commons/sandbox/runtime/trunk/src/main/native/shared/selectkey.c

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -105,19 +105,6 @@ public abstract class Endpoint implement
         throws IOException;
 
     /**
-     * Indicates whether this channel is registered with at least one selector.
-     * <p>
-     * This method is not thread safe. Calling this method and
-     * {@link #register(Selector, int) register} should not be done from
-     * multiple concurrent threads or the faulty results might be returned.
-     * </p>
-     *
-     * @return {@code true} if this channel is registered, {@code false}
-     *         otherwise.
-     */
-    public abstract boolean isRegistered();
-        
-    /**
      * Free the allocated resource by the Operating system.
      * <p>
      * Note that {@code Object.finalize()} method will call
@@ -205,5 +192,28 @@ public abstract class Endpoint implement
         return register(selector, ops,  null);
     }
 
+    /**
+     * Registers this endpoint with the specified selector.
+     *
+     * @param selector the selector with which to register this endpoint.
+     *
+     * @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)
+        throws ClosedSelectorException,
+               IllegalSelectorException,
+               ClosedDescriptorException,
+               OverflowException,
+               IOException
+    {
+        return register(selector, 0,  null);
+    }
+
     
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java?rev=1130435&r1=1130434&r2=1130435&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java Thu Jun  2 08:33:08 2011
@@ -74,6 +74,8 @@ final class LocalSocketDescriptor extend
     public void flush()
         throws SyncFailedException, IOException
     {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
     }
 
     /**

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -37,11 +37,11 @@ import org.apache.commons.runtime.Status
  */
 public class LocalSocketEndpoint extends Endpoint
 {
-    private LocalSocketDescriptor  sd;
-    private SelectionKeyImpl       key;
-    private boolean                blocking = false;
+    private final LocalSocketDescriptor sd;
+    private SelectionKeyImpl            key;
+    private boolean                     blocking = false;
 
-    private static native int nonblock0(int fd, boolean block);
+    private static native int           nonblock0(int fd, boolean block);
 
     /**
      * Creates a new unconnected socket object.
@@ -77,15 +77,6 @@ public class LocalSocketEndpoint extends
     }
 
     @Override
-    public boolean isRegistered()
-    {
-        if (key != null && key.selected)
-            return true;
-        else
-            return false;
-    }
-
-    @Override
     public Descriptor configureBlocking(boolean block)
         throws ClosedDescriptorException,
                IllegalBlockingModeException,
@@ -128,7 +119,7 @@ public class LocalSocketEndpoint extends
     @Override
     public SelectionKey keyFor(Selector selector)
     {
-        if (key != null && key.selector == selector)
+        if (key != null && key.selector == selector && key.ievents != 0)
             return key;
         else
             return null;
@@ -146,17 +137,17 @@ public class LocalSocketEndpoint extends
         if (sd.closed())
             throw new ClosedDescriptorException();
         AbstractSelector sel = (AbstractSelector)selector;
+        //
+        // If key.ievents is zero, this means that the
+        // key was invalidated by Selector.close().
+        // In that case create a new key instance.
         if (key == null || key.ievents == 0)
             key = new SelectionKeyImpl(sel, this);
         if (key.selector != sel)
             throw new IllegalSelectorException();
         if (att != null)
             key.attach(att);
-        if (key.selected)
-            return key;
-        if (ops != 0)
-            key.ievents = ops;
-        return sel.queue(key, sd);
+        return key.queue(ops);
     }
 
 }

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -16,7 +16,9 @@
 
 package org.apache.commons.runtime.net;
 
+import java.io.IOException;
 import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.io.Descriptor;
 import org.apache.commons.runtime.net.Endpoint;
 
@@ -250,11 +252,48 @@ public abstract class SelectionKey
 
     /**
      * Unregister this selection key from its selector.
-     *
+     * <p>
+     * If the key is not currently registered then invoking this method
+     * has no effect. Once cancelled, a key remains invalid until registered
+     * or queued again. 
+     * </p>
+     * <p>
+     * This method is not thread safe. If multiple threads are trying
+     * to cancel the same key the results are unpredictable.
+     * However it is safe to cancel different keys from
+     * the multiple threads having the same selector.
+     * </p>
      * @throws ClosedSelectorException if this selector is closed.
      */
     public abstract void cancel()
         throws ClosedSelectorException;
 
+    /**
+     * Add this selection key the selector's select queue
+     * with the given interest ops.
+     * <p>
+     * If the key is already in the selectors's select queue this
+     * method returns immediately. If the selector is currently
+     * inside the select call it will be interrupted and this key
+     * will be added to its select queue.
+     * </p>
+     * <p>
+     * This method is not thread safe. If multiple threads are trying
+     * to add the same key to the selector's queue the results are
+     * unpredictable. However it is safe to add different keys from
+     * the multiple threads having the same selector.
+     * </p>
+     * @param ops the interest set.
+     *
+     * @return this key
+     *
+     * @throws ClosedSelectorException if the key's selector is closed.
+     * @throws OverflowException if the selector reached it's capacity.
+     * @throws IOException if an I/O error occurs while adding this key.
+     */
+    public abstract SelectionKey queue(int ops)
+        throws ClosedSelectorException,
+               OverflowException,
+               IOException;
     
 }

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -15,10 +15,12 @@
  */
 package org.apache.commons.runtime.net;
 
+import java.io.IOException;
 import org.apache.commons.runtime.AlreadyExistsException;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.NoSuchObjectException;
 import org.apache.commons.runtime.OperationNotImplementedException;
+import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.SystemException;
 import org.apache.commons.runtime.Errno;
 
@@ -118,6 +120,21 @@ final class SelectionKeyImpl extends Sel
         selector.cancel(this);
     }
 
+    @Override
+    public SelectionKey queue(int ops)
+        throws ClosedSelectorException,
+               OverflowException,
+               IOException
+    {
+        if (selected)
+            return this;
+        if (ops != 0)
+            ievents = ops;
+        if (ievents == 0)
+            throw new IllegalArgumentException();        
+        return selector.queue(this, endpoint.descriptor());
+    }
+
     /**
      * Called from the Selector.close() native method for all currently
      * registered keys.

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java?rev=1130435&r1=1130434&r2=1130435&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java Thu Jun  2 08:33:08 2011
@@ -70,6 +70,8 @@ final class SocketDescriptor extends Des
     public void flush()
         throws SyncFailedException, IOException
     {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
     }
 
     /**

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -33,11 +33,11 @@ import org.apache.commons.runtime.Status
  */
 public class SocketEndpoint extends Endpoint
 {
-    private SocketDescriptor  sd;
-    private SelectionKeyImpl  key;
-    private boolean           blocking = false;
+    private final SocketDescriptor      sd;
+    private SelectionKeyImpl            key;
+    private boolean                     blocking = false;
 
-    private static native int nonblock0(int fd, boolean block);
+    private static native int           nonblock0(int fd, boolean block);
     
     /**
      * Creates a new unconnected socket object.
@@ -66,22 +66,13 @@ public class SocketEndpoint extends Endp
     }
 
     @Override
-    public boolean isRegistered()
-    {
-        if (key != null && key.selected)
-            return true;
-        else
-            return false;
-    }
-
-    @Override
     public Descriptor configureBlocking(boolean block)
         throws ClosedDescriptorException,
                IllegalBlockingModeException,
                IOException
     {
         if (sd.closed())
-        throw new ClosedDescriptorException();
+            throw new ClosedDescriptorException();
         if (key != null && key.selected)
             throw new IllegalBlockingModeException();
         if (blocking == block)
@@ -123,7 +114,7 @@ public class SocketEndpoint extends Endp
     @Override
     public SelectionKey keyFor(Selector selector)
     {
-        if (key != null && key.selector == selector)
+        if (key != null && key.selector == selector && key.ievents != 0)
             return key;
         else
             return null;
@@ -141,17 +132,17 @@ public class SocketEndpoint extends Endp
         if (sd.closed())
             throw new ClosedDescriptorException();
         AbstractSelector sel = (AbstractSelector)selector;
+        //
+        // If key.ievents is zero, this means that the
+        // key was invalidated by Selector.close().
+        // In that case create a new key instance.
         if (key == null || key.ievents == 0)
             key = new SelectionKeyImpl(sel, this);
         if (key.selector != sel)
             throw new IllegalSelectorException();
         if (att != null)
             key.attach(att);
-        if (key.selected)
-            return key;
-        if (ops != 0)
-            key.ievents = ops;
-        return sel.queue(key, sd);
+        return key.queue(ops);
     }
 
 }

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -29,7 +29,6 @@ ACR_CLASS_DTOR(HashSet);
 
 int     AcrArrayListAdd(JNI_STDARGS, jobject e);
 int     AcrHashSetAdd(JNI_STDARGS, jobject e);
-void    AcrSelectionKeyReset(JNI_STDARGS);
 void    AcrLibLockAcquire(void);
 void    AcrLibLockRelease(void);
 

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h?rev=1130435&r1=1130434&r2=1130435&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h Thu Jun  2 08:33:08 2011
@@ -65,6 +65,8 @@ struct acr_sockaddr_t {
 extern "C" {
 #endif
 
+void    AcrSelectionKeyReset(JNI_STDARGS);
+
 #ifdef __cplusplus
 }
 #endif

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=1130435&r1=1130434&r2=1130435&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 Thu Jun  2 08:33:08 2011
@@ -20,7 +20,7 @@
 #include "acr/port.h"
 #include "acr/time.h"
 #include "acr/iodefs.h"
-#include "acr/misc.h"
+#include "acr/netapi.h"
 #include "arch_opts.h"
 #include <poll.h>
 #if HAVE_SYS_RESOURCE_H

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/selectkey.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/selectkey.c?rev=1130435&r1=1130434&r2=1130435&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/selectkey.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/selectkey.c Thu Jun  2 08:33:08 2011
@@ -16,7 +16,7 @@
 
 #include "acr/clazz.h"
 #include "acr/jniapi.h"
-#include "acr/misc.h"
+#include "acr/netapi.h"
 #include "arch_opts.h"
 
 J_DECLARE_CLAZZ = {