commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1133265 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/os/unix/ test/org/apache/commons/runtime/
Date Wed, 08 Jun 2011 07:20:35 GMT
Author: mturk
Date: Wed Jun  8 07:20:35 2011
New Revision: 1133265

URL: http://svn.apache.org/viewvc?rev=1133265&view=rev
Log:
Move blocking mode set/get to the descriptor

Added:
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.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/net/LocalDescriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java?rev=1133265&r1=1133264&r2=1133265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java
Wed Jun  8 07:20:35 2011
@@ -36,12 +36,16 @@ import org.apache.commons.runtime.io.Des
  */
 final class LocalDescriptor extends Descriptor
 {
-    private boolean xclosed = false;
-    private static native int close0(int fd);
-    private static native int sendz0(int fd);
-    private static native int socket0(int type)
+    private static native int     close0(int fd);
+    private static native int     sendz0(int fd);
+    private static native int     socket0(int type)
+        throws IOException;
+    private static native int     block0(int fd, boolean block);
+    private static native boolean isBlocking0(int fd)
         throws IOException;
 
+    private boolean xclosed = false;
+
     public LocalDescriptor()
     {
     }
@@ -55,9 +59,35 @@ final class LocalDescriptor extends Desc
         throws IOException
     {
         this.fd = socket0(type.valueOf());
-        xclosed = false;
     }
 
+    public boolean isBlocking()
+        throws IOException
+    {
+        if (closed()) {
+            if (xclosed)
+                throw new ClosedDescriptorException();
+            else
+                throw new InvalidDescriptorException();
+        }
+        return isBlocking0(fd);
+    }
+
+    public LocalDescriptor configureBlocking(boolean block)
+        throws IOException
+    {
+        if (closed()) {
+            if (xclosed)
+                throw new ClosedDescriptorException();
+            else
+                throw new InvalidDescriptorException();
+        }
+        int rc = block0(fd, block);
+        if (rc != 0)
+            throw new IOException(Status.describe(rc));
+        return this;        
+    }
+    
     @Override
     public void close()
         throws IOException

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java?rev=1133265&r1=1133264&r2=1133265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
Wed Jun  8 07:20:35 2011
@@ -42,10 +42,8 @@ public class LocalEndpoint extends Endpo
     private final LocalDescriptor       sd;
     private EndpointAddress             ea;
     private SelectionKeyImpl            key;
-    private boolean                     blocking  = true;
     private boolean                     connected = false;
 
-    private static native int           block0(int fd, boolean block);
     private static native int           connect0(int fd, byte[] sa, int timeout);
 
     /**
@@ -61,12 +59,12 @@ public class LocalEndpoint extends Endpo
      * Creates a new connected local endpoint from a local descriptor
      * and address.
      */
-    public LocalEndpoint(LocalDescriptor sd, EndpointAddress ea)
+    public LocalEndpoint(Descriptor sd, EndpointAddress ea)
     {
         super(EndpointType.LOCAL);
         if (sd == null || ea == null)
             throw new NullPointerException();
-        this.sd   = sd;
+        this.sd   = (LocalDescriptor)sd;
         this.ea   = ea;
         connected = true;
     }
@@ -103,7 +101,7 @@ public class LocalEndpoint extends Endpo
     public boolean isBlocking()
         throws IOException
     {
-        return blocking;
+        return sd.isBlocking();
     }
 
     @Override
@@ -112,17 +110,9 @@ public class LocalEndpoint extends Endpo
                IllegalBlockingModeException,
                IOException
     {
-        if (sd.closed())
-            throw new ClosedDescriptorException();
         if (key != null && key.selected)
             throw new IllegalBlockingModeException();
-        if (blocking == block)
-            return sd;
-        int rc = block0(sd.fd(), block);
-        if (rc != 0)
-            throw new IOException(Status.describe(rc));
-        blocking = block;
-        return sd;
+        return sd.configureBlocking(block);
     }
 
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1133265&r1=1133264&r2=1133265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
Wed Jun  8 07:20:35 2011
@@ -41,10 +41,8 @@ public class LocalServerEndpoint extends
     private final LocalDescriptor       sd;
     private SelectionKeyImpl            key;
     private EndpointAddress             sa;
-    private boolean                     blocking = true;
-    private boolean                     bound    = false;
+    private boolean                     bound = false;
 
-    private static native int           block0(int fd, boolean block);
     private static native void          unlink0(byte[] addr);
 
     /**
@@ -78,7 +76,7 @@ public class LocalServerEndpoint extends
     public boolean isBlocking()
         throws IOException
     {
-        return blocking;
+        return sd.isBlocking();
     }
 
     @Override
@@ -87,17 +85,9 @@ public class LocalServerEndpoint extends
                IllegalBlockingModeException,
                IOException
     {
-        if (sd.closed())
-            throw new ClosedDescriptorException();
         if (key != null && key.selected)
             throw new IllegalBlockingModeException();
-        if (blocking == block)
-            return sd;
-        int rc = block0(sd.fd(), block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
-        return sd;
+        return sd.configureBlocking(block);
     }
 
 

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=1133265&r1=1133264&r2=1133265&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
Wed Jun  8 07:20:35 2011
@@ -33,12 +33,16 @@ import org.apache.commons.runtime.io.Des
 final class SocketDescriptor extends Descriptor
 {
 
-    private boolean xclosed = false;
-    private static native int close0(int fd);
-    private static native int sendz0(int fd);
-    private static native int socket0(int type)
+    private static native int     close0(int fd);
+    private static native int     sendz0(int fd);
+    private static native int     socket0(int type)
+        throws IOException;
+    private static native int     block0(int fd, boolean block);
+    private static native boolean isBlocking0(int fd)
         throws IOException;
 
+    private boolean xclosed = false;
+
     public SocketDescriptor()
     {
     }
@@ -52,7 +56,33 @@ final class SocketDescriptor extends Des
         throws IOException
     {
         this.fd = socket0(type.valueOf());
-        xclosed = false;
+    }
+
+    public boolean isBlocking()
+        throws IOException
+    {
+        if (closed()) {
+            if (xclosed)
+                throw new ClosedDescriptorException();
+            else
+                throw new InvalidDescriptorException();
+        }
+        return isBlocking0(fd);
+    }
+
+    public SocketDescriptor configureBlocking(boolean block)
+        throws IOException
+    {
+        if (closed()) {
+            if (xclosed)
+                throw new ClosedDescriptorException();
+            else
+                throw new InvalidDescriptorException();
+        }
+        int rc = block0(fd, block);
+        if (rc != 0)
+            throw new IOException(Status.describe(rc));
+        return this;
     }
 
     @Override

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=1133265&r1=1133264&r2=1133265&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
Wed Jun  8 07:20:35 2011
@@ -36,11 +36,8 @@ public class SocketEndpoint extends Endp
     private final SocketDescriptor      sd;
     private EndpointAddress             ea;
     private SelectionKeyImpl            key;
-    private boolean                     blocking  = true;
     private boolean                     connected = false;
 
-    private static native int           block0(int fd, boolean block);
-    
     /**
      * Creates a new unconnected socket endpoint.
      */
@@ -54,12 +51,12 @@ public class SocketEndpoint extends Endp
      * Creates a new connected socket endpoint from a socket descriptor
      * and local address.
      */
-    public SocketEndpoint(SocketDescriptor sd, EndpointAddress ea)
+    public SocketEndpoint(Descriptor sd, EndpointAddress ea)
     {
         super(EndpointType.SOCKET);
         if (sd == null || ea == null)
             throw new NullPointerException();
-        this.sd   = sd;
+        this.sd   = (SocketDescriptor)sd;
         this.ea   = ea;
         connected = true;
     }
@@ -84,29 +81,21 @@ public class SocketEndpoint extends Endp
     }
 
     @Override
+    public boolean isBlocking()
+        throws IOException
+    {
+        return sd.isBlocking();
+    }
+
+    @Override
     public Descriptor configureBlocking(boolean block)
         throws ClosedDescriptorException,
                IllegalBlockingModeException,
                IOException
     {
-        if (sd.closed())
-            throw new ClosedDescriptorException();
         if (key != null && key.selected)
             throw new IllegalBlockingModeException();
-        if (blocking == block)
-            return sd;
-        int rc = block0(sd.fd(), block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
-        return sd;
-    }
-
-    @Override
-    public boolean isBlocking()
-        throws IOException
-    {
-        return blocking;
+        return sd.configureBlocking(block);
     }
 
     @Override

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=1133265&r1=1133264&r2=1133265&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 Wed Jun  8 07:20:35 2011
@@ -41,6 +41,31 @@ ACR_NET_EXPORT(jint, SocketDescriptor, s
         return 0;
 }
 
+ACR_NET_EXPORT(jint, SocketDescriptor, block0)(JNI_STDARGS, jint fd, jboolean on)
+{
+    return AcrNonblock(fd, on == JNI_FALSE);
+}
+
+ACR_NET_EXPORT(jboolean, SocketDescriptor, isBlocking0)(JNI_STDARGS, jint fd)
+{
+#ifdef O_NONBLOCK
+    /* Use non-blocking I/O
+    */
+    long mode;
+    if ((mode = fcntl(fd, F_GETFL, 0)) == -1) {
+        ACR_THROW_NET_ERRNO();
+        return JNI_TRUE;
+    }
+    if ((mode & O_NONBLOCK) == O_NONBLOCK)
+        return JNI_FALSE;
+#else
+    /* Non blocking I/O is unsupported.
+    */
+    ACR_THROW_NET_ERROR(ACR_ENOTIMPL);
+#endif
+    return JNI_TRUE;
+}
+
 ACR_NET_EXPORT(jboolean, SocketAddress, haveipv6)(JNI_STDARGS)
 {
     int sock;
@@ -53,8 +78,3 @@ ACR_NET_EXPORT(jboolean, SocketAddress, 
     else
         return JNI_FALSE;
 }
-
-ACR_NET_EXPORT(jint, SocketEndpoint, block0)(JNI_STDARGS, jint fd, jboolean on)
-{
-    return AcrNonblock(fd,  on == JNI_FALSE);
-}

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=1133265&r1=1133264&r2=1133265&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 Wed Jun  8 07:20:35 2011
@@ -90,11 +90,31 @@ ACR_NET_EXPORT(jint, LocalDescriptor, so
     return sd;
 }
 
-ACR_NET_EXPORT(jint, LocalEndpoint, block0)(JNI_STDARGS, jint fd, jboolean on)
+ACR_NET_EXPORT(jint, LocalDescriptor, block0)(JNI_STDARGS, jint fd, jboolean on)
 {
     return AcrNonblock(fd, on == JNI_FALSE);
 }
 
+ACR_NET_EXPORT(jboolean, LocalDescriptor, isBlocking0)(JNI_STDARGS, jint fd)
+{
+#ifdef O_NONBLOCK
+    /* Use non-blocking I/O
+    */
+    long mode;
+    if ((mode = fcntl(fd, F_GETFL, 0)) == -1) {
+        ACR_THROW_NET_ERRNO();
+        return JNI_TRUE;
+    }
+    if ((mode & O_NONBLOCK) == O_NONBLOCK)
+        return JNI_FALSE;
+#else
+    /* Non blocking I/O is unsupported.
+    */
+    ACR_THROW_NET_ERROR(ACR_ENOTIMPL);
+#endif
+    return JNI_TRUE;
+}
+
 ACR_NET_EXPORT(jint, LocalEndpoint, connect0)(JNI_STDARGS, jint fd,
                                               jbyteArray cb, jint timeout)
 {
@@ -127,12 +147,6 @@ ACR_NET_EXPORT(jint, LocalEndpoint, conn
     return rc;
 }
 
-ACR_NET_EXPORT(jint, LocalServerEndpoint, block0)(JNI_STDARGS, jint fd,
-                                                  jboolean on)
-{
-    return AcrNonblock(fd,  on == JNI_FALSE);
-}
-
 ACR_NET_EXPORT(jint, LocalServerEndpoint, bind0)(JNI_STDARGS, jint fd,
                                                  jbyteArray ba,
                                                  jint backlog)

Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java?rev=1133265&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java
Wed Jun  8 07:20:35 2011
@@ -0,0 +1,49 @@
+/* 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 java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.DatagramSocket;
+import java.net.DatagramSocketImpl;
+import java.net.Socket;
+import java.net.SocketImpl;
+import java.net.ServerSocket;
+import java.net.SocketException;
+import org.testng.annotations.*;
+import org.testng.Assert;
+
+public class TestEndpoint extends Assert
+{
+
+    @Test(groups = { "core" }, expectedExceptions = ClassCastException.class)
+    public void createInvalidEndpoint()
+        throws IOException
+    {
+        SocketEndpoint se = new SocketEndpoint();
+        // This will throw ClassCastException
+        //
+        LocalEndpoint  le = new LocalEndpoint(se.descriptor(), new LocalEndpointAddress("dummy"));
+        assertNull(le);
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message