harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r418258 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java
Date Fri, 30 Jun 2006 13:20:14 GMT
Author: gharley
Date: Fri Jun 30 06:20:14 2006
New Revision: 418258

URL: http://svn.apache.org/viewvc?rev=418258&view=rev
Log:
HARMONY 707 : ServerSocketChannel.accept() method changes the value of SO_TIMEOUT if there
are no pending connections in nonblocking mode

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java?rev=418258&r1=418257&r2=418258&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
Fri Jun 30 06:20:14 2006
@@ -26,6 +26,7 @@
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.NotYetBoundException;
+import java.nio.channels.SelectableChannel;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
@@ -55,9 +56,6 @@
     // status closed.
     private static final int SERVER_STATUS_CLOSED = 1;
 
-    // default timeout used to nonblocking mode.
-    private static final int DEFAULT_TIMEOUT = 100;
-
     // error message, for native dependent.
     private static final String ERRMSG_ASYNCHRONOUS = "The call was cancelled"; //$NON-NLS-1$
 
@@ -133,12 +131,30 @@
 
             synchronized (acceptLock) {
                 synchronized (blockingLock()) {
-                    int oldtime = socket.getSoTimeout();
-                    // timeout, 0 means blocking.
-                    socket.setSoTimeout(isBlocking() ? 0 : DEFAULT_TIMEOUT);
-                    ((ServerSocketAdapter) socket).accept(socketGot,
-                            (SocketChannelImpl) sockChannel);
-                    socket.setSoTimeout(oldtime);
+                    boolean isBlocking = isBlocking();
+                    if (!isBlocking) {
+                        // for non blocking mode, use select to see whether
+                        // there are any pending connections.
+                        int[] tryResult = Platform.getNetworkSystem().select(
+                                new SelectableChannel[] { this },
+                                new SelectableChannel[0], 0);
+                        if (0 == tryResult.length || 0 == tryResult[0]) {
+                            // no pending connections, returns immediately.
+                            return null;
+                        }
+                    }
+                    // do accept.
+                    do {
+                        try {
+                            ((ServerSocketAdapter) socket).accept(socketGot,
+                                    (SocketChannelImpl) sockChannel);
+                            // select successfully, break out immediately.
+                            break;
+                        } catch (SocketTimeoutException e) {
+                            // continue to accept if the channel is in blocking
+                            // mode.
+                        }
+                    } while (isBlocking);
                 }
             }
         } catch (BindException e) {
@@ -146,23 +162,8 @@
             if (ERRMSG_ASYNCHRONOUS.equals(e.getMessage())) {
                 throw new AsynchronousCloseException();
             }
-        } catch (SocketTimeoutException e) {
-            // nonblocking mode.
-            return null;
         } finally {
             end(socketGot.isConnected());
-        }
-
-        // security check
-        SecurityManager sm = System.getSecurityManager();
-        if (sm != null) {
-            try {
-                sm.checkAccept(socketGot.getInetAddress().getHostAddress(),
-                        socketGot.getPort());
-            } catch (SecurityException e) {
-                sockChannel.close();
-                throw e;
-            }
         }
         return sockChannel;
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java?rev=418258&r1=418257&r2=418258&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java
Fri Jun 30 06:20:14 2006
@@ -436,5 +436,24 @@
             // correct
         }
     }
-
+    
+    public void test_accept_SOTIMEOUT() throws IOException {
+        // regression test for Harmony-707        
+        final int SO_TIMEOUT = 10;
+        ServerSocketChannel sc = ServerSocketChannel.open();
+        try {
+            ServerSocket ss = sc.socket();
+            ss.bind(localAddr1);
+            sc.configureBlocking(false);
+            ss.setSoTimeout(SO_TIMEOUT);
+            SocketChannel client = sc.accept();
+            // non blocking mode, returns null since there are no pending connections.
+            assertNull(client);
+            int soTimeout = ss.getSoTimeout();
+            // Harmony fails here.
+            assertEquals(SO_TIMEOUT, soTimeout);
+        } finally {
+            sc.close();
+        }
+    }
 }



Mime
View raw message