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 07:14:35 UTC

svn commit: r1129489 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ native/os/unix/

Author: mturk
Date: Tue May 31 05:14:34 2011
New Revision: 1129489

URL: http://svn.apache.org/viewvc?rev=1129489&view=rev
Log:
Simplify configureBlocking by moving the method from Descriptor to the Endpoint

Removed:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketDescriptor.java
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java
    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/LocalStrings.properties
    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/SocketDescriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java?rev=1129489&r1=1129488&r2=1129489&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java Tue May 31 05:14:34 2011
@@ -124,41 +124,6 @@ public abstract class Descriptor impleme
     }
 
     /**
-     * Sets the blocking mode of this descriptor.
-     *
-     * @param block
-     *          {@code true} for setting this descriptor's mode to blocking,
-     *          {@code false} to set it to non-blocking.
-     * @return this descriptor.
-     * @throws ClosedChannelException
-     *          if this descriptr is closed.
-     * @throws IOException
-     *         if an I/O error occurs.
-     */
-    public abstract Descriptor configureBlocking(boolean block)
-        throws ClosedDescriptorException,
-               IOException;
-
-    /**
-     * Test wather or not every I/O operation on {@code this} descriptor will
-     * block until it completes.
-     *
-     * @return {@code true} if, and only if, this device
-     *         is in blocking mode.
-     *
-     * @throws IOException if an I/O error occurs while determining the
-     *         blocking state.
-     */
-    public boolean isBlocking()
-        throws IOException
-    {
-        // Default implementation presumes that any OS descriptor
-        // is in blocking state when created.
-        // This is true both for files and sockets.
-        return true;
-    }
-
-    /**
      * Get underlying Operating system descriptor.
      * @return operating system descriptor.
      */

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java?rev=1129489&r1=1129488&r2=1129489&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java Tue May 31 05:14:34 2011
@@ -36,16 +36,4 @@ public interface Device extends Closeabl
      */
     public boolean valid();
 
-    /**
-     * Test wather or not every I/O operation on {@code this} device will
-     * block until it completes.
-     *
-     * @return {@code true} if, and only if, this device
-     *         is in blocking mode.
-     *
-     * @throws IOException if an I/O error occurs.
-     */
-    public boolean isBlocking()
-        throws IOException;
-
 }

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=1129489&r1=1129488&r2=1129489&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 05:14:34 2011
@@ -31,22 +31,28 @@ import org.apache.commons.runtime.io.Des
  */
 public abstract class Endpoint implements Closeable
 {
-    /**
-     * This endpoint's selection key.
-     */
-    protected SelectionKey      key;
+    private EndpointType type;
+
+    private Endpoint()
+    {
+        // No instance
+    }
 
     /**
      * Creates a new Endpoint object.
      */
-    protected Endpoint()
+    protected Endpoint(EndpointType type)
     {
+        this.type = type;
     }
 
     /**
      * Gets the endpoint's type.
      */
-    public abstract EndpointType type();
+    public EndpointType type()
+    {
+        return type;
+    }
 
     /**
      * Gets the endpoint's  descriptor object.
@@ -54,6 +60,35 @@ public abstract class Endpoint implement
     public abstract Descriptor descriptor();
 
     /**
+     * Sets the blocking mode of this descriptor.
+     *
+     * @param block
+     *          {@code true} for setting this descriptor's mode to blocking,
+     *          {@code false} to set it to non-blocking.
+     * @return this descriptor.
+     * @throws ClosedChannelException
+     *          if this descriptr is closed.
+     * @throws IOException
+     *         if an I/O error occurs.
+     */
+    public abstract Descriptor configureBlocking(boolean block)
+        throws ClosedDescriptorException,
+               IOException;
+
+    /**
+     * Test wather or not every I/O operation on {@code this} descriptor will
+     * block until it completes.
+     *
+     * @return {@code true} if, and only if, this device
+     *         is in blocking mode.
+     *
+     * @throws IOException if an I/O error occurs while determining the
+     *         blocking state.
+     */
+    public abstract boolean isBlocking()
+        throws IOException;
+    
+    /**
      * Free the allocated resource by the Operating system.
      * <p>
      * Note that {@code Object.finalize()} method will call
@@ -78,16 +113,7 @@ public abstract class Endpoint implement
      * @throws IllegalSelectorException if the endpoint is already
      *          registered with a different selector.
      */
-    public synchronized SelectionKey key(Selector selector)
-        throws NullPointerException, IllegalSelectorException
-    {
-        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;
-    }
+    public abstract SelectionKey key(Selector selector)
+        throws NullPointerException, IllegalSelectorException;
 
 }

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=1129489&r1=1129488&r2=1129489&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 Tue May 31 05:14:34 2011
@@ -33,12 +33,11 @@ import org.apache.commons.runtime.io.Des
  * </p>
  * @since Runtime 1.0
  */
-final class LocalSocketDescriptor extends AbstractSocketDescriptor
+final class LocalSocketDescriptor extends Descriptor
 {
 
     private static native int close0(int fd);
     private static native int sendz0(int fd);
-    private static native int nonblock0(int fd, boolean block);
 
     public LocalSocketDescriptor()
     {
@@ -77,22 +76,6 @@ final class LocalSocketDescriptor extend
     {
     }
 
-    @Override
-    public Descriptor configureBlocking(boolean block)
-        throws ClosedDescriptorException,
-               IOException
-    {
-        if (fd == -1)
-            throw new ClosedDescriptorException();
-        if (blocking == block)
-            return this;
-        int rc = nonblock0(fd, block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
-        return this;
-    }
-
     /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.
@@ -115,5 +98,4 @@ final class LocalSocketDescriptor extend
         }
     }
 
-
 }

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=1129489&r1=1129488&r2=1129489&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 05:14:34 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.Status;
 
 /**
  * This class represents a local socket endpoint.
@@ -36,11 +37,17 @@ import org.apache.commons.runtime.io.Des
 public class LocalSocketEndpoint extends Endpoint
 {
     private LocalSocketDescriptor  sd;
+    private SelectionKeyImpl       key;
+    private boolean                blocking = false;
+
+    private static native int nonblock0(int fd, boolean block);
+
     /**
      * Creates a new unconnected socket object.
      */
     private LocalSocketEndpoint()
     {
+        super(EndpointType.LOCAL);
     }
 
     /**
@@ -48,26 +55,62 @@ public class LocalSocketEndpoint extends
      */
     public LocalSocketEndpoint(LocalSocketDescriptor sd)
     {
+        super(EndpointType.LOCAL);
         this.sd = sd;
     }
 
     @Override
-    public EndpointType type()
+    public Descriptor descriptor()
     {
-        return EndpointType.LOCAL;
+        return sd;
     }
 
     @Override
-    public Descriptor descriptor()
+    public Descriptor configureBlocking(boolean block)
+        throws ClosedDescriptorException,
+               IllegalBlockingModeException,
+               IOException
     {
+        if (!sd.valid())
+            throw new ClosedDescriptorException();
+        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));
+        blocking = block;
         return sd;
     }
 
     @Override
+    public boolean isBlocking()
+        throws IOException
+    {
+        return blocking;
+    }
+
+    @Override
     public void close()
         throws IOException
     {
         sd.close();
     }
 
+    @Override
+    public SelectionKey key(Selector selector)
+        throws NullPointerException, IllegalSelectorException
+    {
+        if (selector == null)
+            throw new NullPointerException(Local.sm.get("selector.NULL"));
+        synchronized(this) {
+            if (key == null)
+                key = new SelectionKeyImpl(selector, this);
+            if (key.selector != selector)
+                throw new IllegalSelectorException();
+        }
+        return key;
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties?rev=1129489&r1=1129488&r2=1129489&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties Tue May 31 05:14:34 2011
@@ -17,3 +17,4 @@ port.ERANGE=Port is outside allowed rang
 socketd.CLOSED=Socket is already closed
 selector.NULL=Selector can't be null
 selector.ERANGE=Selector size is outsize allowed range
+selector.ETYPE=Unknown Selector type

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=1129489&r1=1129488&r2=1129489&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 05:14:34 2011
@@ -31,9 +31,8 @@ import org.apache.commons.runtime.Errno;
 import org.apache.commons.runtime.Status;
 
 /**
- * Socket Selector implementation class.
- * <p>
- * </p>
+ * Posix socket selector implementation class.
+ * PosixSelector uses posix poll() function for waiting on I/O events.
  *
  * @since Runtime 1.0
  */
@@ -100,14 +99,16 @@ final class PosixSelector extends Select
     {
         ensureValid();
         SelectionKeyImpl skey = (SelectionKeyImpl)key;
-        if (skey.selector() != this)
+        if (skey.selector != this)
             throw new IllegalSelectorException();
-        Descriptor sd = skey.endpoint().descriptor();
-        int fd  = sd.fd();
-        if (fd == -1)
+        Descriptor sd = skey.endpoint.descriptor();
+        if (!sd.valid())
             throw new ClosedDescriptorException();
-        int rc = add0(pollset, skey, fd, ops, skey.timeout());
+        skey.selected = true;
+        int rc = add0(pollset, skey, sd.fd(), ops, skey.timeout());
         if (rc != 0) {
+            // Add failed
+            skey.selected = false;
             if (rc == Errno.EALREADY)
                 return false;
             if (rc == Errno.EOVERFLOW)
@@ -124,16 +125,16 @@ final class PosixSelector extends Select
                IllegalSelectorException
     {
         ensureValid();
-        if (key.selector() != this)
-            throw new IllegalSelectorException();
         SelectionKeyImpl skey = (SelectionKeyImpl)key;
-        Descriptor sd = skey.endpoint().descriptor();
-        int fd  = sd.fd();
-        if (fd != -1) {
-            // Remove selection key
-            del0(pollset, skey, fd);
-            skey.revents = 0;
+        if (skey.selector != this)
+            throw new IllegalSelectorException();
+        Descriptor sd = skey.endpoint.descriptor();
+        if (sd.valid()) {
+            // Remove the given selection key
+            del0(pollset, skey, sd.fd());
         }
+        skey.revents  = 0;
+        skey.selected = false;
     }
 
     @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=1129489&r1=1129488&r2=1129489&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 05:14:34 2011
@@ -26,19 +26,15 @@ import org.apache.commons.runtime.net.En
 public abstract class SelectionKey
 {
 
-    private Selector        selector;
     private Object          attachment;
-    private Endpoint        endpoint;
     private int             timeout;
 
     /**
      * Constructs a new {@code SelectionKey} for given selector and enpoint
      */
-    protected SelectionKey(Selector selector, Endpoint endpoint)
+    protected SelectionKey()
     {
-        this.selector = selector;
-        this.endpoint = endpoint;
-        this.timeout  = -1;
+        timeout = -1;
     }
 
     /**
@@ -63,20 +59,7 @@ public abstract class SelectionKey
      *
      * @return the related endpoint.
      */
-    public Endpoint endpoint()
-    {
-        return endpoint;
-    }
-
-    /**
-     * Returns the selector for which this key was created.
-     *
-     * @return the related selector.
-     */
-    public Selector selector()
-    {
-        return selector;
-    }
+    public abstract Endpoint endpoint();
 
     /**
      * Attaches an object to this key.
@@ -135,35 +118,12 @@ public abstract class SelectionKey
     }
 
     /**
-     * Register this selection key with the given selector.
-     *
-     * @throws ClosedSelectorException if this selector is closed.
-     * @throws IllegalSelectorException if this key was not created by the same
-     *          selector as the given selector.
-     */
-    public SelectionKey register(Selector selector)
-        throws IllegalSelectorException
-    {
-        if (this.selector == null || selector == null)
-            this.selector  = selector;
-        if (this.selector != selector)
-            throw new IllegalSelectorException();
-        if (this.selector == null) {
-            AbstractSocketDescriptor sd = (AbstractSocketDescriptor)endpoint.descriptor();
-            sd.selecting = false;
-        }
-        return this;
-    }
-    /**
      * Unregister this selection key from its selector.
      *
      * @throws ClosedSelectorException if this selector is closed.
      */
-    public void cancel()
-        throws ClosedSelectorException
-    {
-        selector.cancel(this);
-    }
+    public abstract void cancel()
+        throws ClosedSelectorException;
 
     
 }

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=1129489&r1=1129488&r2=1129489&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 05:14:34 2011
@@ -25,6 +25,8 @@ import org.apache.commons.runtime.Errno;
 /**
  * Selector implementation class.
  * <p>
+ * This is package private class with public fields so that
+ * we can minimize the number of setters and getters.
  * </p>
  *
  * @since Runtime 1.0
@@ -32,15 +34,20 @@ import org.apache.commons.runtime.Errno;
 final class SelectionKeyImpl extends SelectionKey
 {
 
-    public  int             ievents;
-    public  int             revents;
+    public  int              ievents;
+    public  int              revents;
+    public  Selector         selector;
+    public  Endpoint         endpoint;
+    public  volatile boolean selected;
 
     public SelectionKeyImpl(Selector selector, Endpoint endpoint,
                             int ievents)
     {
-        super(selector, endpoint);
-        this.ievents    = ievents;
-        this.revents    = 0;
+        this.selector = selector;
+        this.endpoint = endpoint;
+        this.ievents  = ievents;
+        this.revents  = 0;
+        this.selected = false;
     }
 
     public SelectionKeyImpl(Selector selector, Endpoint endpoint)
@@ -49,6 +56,12 @@ final class SelectionKeyImpl extends Sel
     }
 
     @Override
+    public Endpoint endpoint()
+    {
+        return endpoint;
+    }
+
+    @Override
     public int events()
     {
         return revents;
@@ -61,9 +74,18 @@ final class SelectionKeyImpl extends Sel
         return this;
     }
 
-    public void     destroy()
+    @Override
+    public void cancel()
+        throws ClosedSelectorException
     {
-        register(null);
+        selector.cancel(this);
     }
 
+    public void invalidate()
+    {
+        revents  = 0;
+        selected = false;
+        selector = null;
+        endpoint = null;
+    }
 }

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=1129489&r1=1129488&r2=1129489&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 05:14:34 2011
@@ -51,6 +51,7 @@ public abstract class Selector implement
     /**
      * Creates a new {@code Selector} instance with the given size.
      *
+     * @param  selector size
      * @return the new socket selector.
      *
      * @throws InvalidRangeException if {@code size} is outside the
@@ -66,7 +67,7 @@ public abstract class Selector implement
             case 0:
                 return new PosixSelector(size);
             default:
-                throw new RuntimeException("Unknown Selector type");
+                throw new RuntimeException(Local.sm.get("selector.ETYPE"));
         }
     }
 

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=1129489&r1=1129488&r2=1129489&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 Tue May 31 05:14:34 2011
@@ -29,12 +29,11 @@ import org.apache.commons.runtime.io.Des
  * Package private Socket Descriptor 
  * @since Runtime 1.0
  */
-final class SocketDescriptor extends AbstractSocketDescriptor
+final class SocketDescriptor extends Descriptor
 {
 
     private static native int close0(int fd);
     private static native int sendz0(int fd);
-    private static native int nonblock0(int fd, boolean block);
 
     public SocketDescriptor()
     {
@@ -73,23 +72,6 @@ final class SocketDescriptor extends Abs
     {
     }
 
-    @Override
-    public Descriptor configureBlocking(boolean block)
-        throws ClosedDescriptorException,
-               IllegalBlockingModeException,
-               IOException
-    {
-        if (fd == -1)
-            throw new ClosedDescriptorException();
-        if (blocking == block)
-            return this;
-        int rc = nonblock0(fd, block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
-        return this;
-    }
-
     /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.

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=1129489&r1=1129488&r2=1129489&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 05:14:34 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.Status;
 
 /**
  * This class represents a socket endpoint.
@@ -32,12 +33,17 @@ import org.apache.commons.runtime.io.Des
 public class SocketEndpoint extends Endpoint
 {
     private SocketDescriptor  sd;
+    private SelectionKeyImpl  key;
+    private boolean           blocking = false;
 
+    private static native int nonblock0(int fd, boolean block);
+    
     /**
      * Creates a new unconnected socket object.
      */
     public SocketEndpoint()
     {
+        super(EndpointType.SOCKET);
     }
 
     /**
@@ -45,27 +51,61 @@ public class SocketEndpoint extends Endp
      */
     public SocketEndpoint(SocketDescriptor sd)
     {
+        super(EndpointType.SOCKET);
         this.sd = sd;
     }
 
     @Override
-    public EndpointType type()
+    public Descriptor descriptor()
     {
-        return EndpointType.SOCKET;
+        return sd;
     }
 
     @Override
-    public Descriptor descriptor()
+    public Descriptor configureBlocking(boolean block)
+        throws ClosedDescriptorException,
+               IllegalBlockingModeException,
+               IOException
     {
+        if (!sd.valid())
+            throw new ClosedDescriptorException();
+        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));
+        blocking = block;
         return sd;
     }
 
     @Override
+    public boolean isBlocking()
+        throws IOException
+    {
+        return blocking;
+    }
+
+    @Override
     public void close()
         throws IOException
     {
-        sd.close();
         key = null;
+        sd.close();
+    }
+
+    @Override
+    public SelectionKey key(Selector selector)
+        throws NullPointerException, IllegalSelectorException
+    {
+        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;
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c?rev=1129489&r1=1129488&r2=1129489&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c Tue May 31 05:14:34 2011
@@ -53,7 +53,7 @@ ACR_NET_EXPORT(jboolean, SocketAddress, 
         return JNI_FALSE;
 }
 
-ACR_NET_EXPORT(jint, SocketDescriptor, nonblock0)(JNI_STDARGS, jint fd, jboolean on)
+ACR_NET_EXPORT(jint, SocketEndpoint, nonblock0)(JNI_STDARGS, jint fd, jboolean on)
 {
     return AcrNonblock(fd, on);
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c?rev=1129489&r1=1129488&r2=1129489&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Tue May 31 05:14:34 2011
@@ -43,7 +43,7 @@ ACR_NET_EXPORT(jint, LocalSocketDescript
         return 0;
 }
 
-ACR_NET_EXPORT(jint, LocakSocketDescriptor, nonblock0)(JNI_STDARGS, jint fd, jboolean on)
+ACR_NET_EXPORT(jint, LocalSocketEndpoint, nonblock0)(JNI_STDARGS, jint fd, jboolean on)
 {
     return AcrNonblock(fd, on);
 }