commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1133767 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/PosixSelector.java java/org/apache/commons/runtime/net/Selector.java test/org/apache/commons/runtime/TestPosixEndpoint.java
Date Thu, 09 Jun 2011 10:15:07 GMT
Author: mturk
Date: Thu Jun  9 10:15:07 2011
New Revision: 1133767

URL: http://svn.apache.org/viewvc?rev=1133767&view=rev
Log:
Add select api.

Modified:
    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/Selector.java
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java

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=1133767&r1=1133766&r2=1133767&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
Thu Jun  9 10:15:07 2011
@@ -18,6 +18,7 @@ package org.apache.commons.runtime.net;
 import java.io.IOException;
 import java.net.SocketException;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.HashSet;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
@@ -39,9 +40,10 @@ import org.apache.commons.runtime.Status
 final class PosixSelector extends AbstractSelector
 {
 
-    private short[]             revents;
-    private SelectionKeyImpl[]  keyset;
-    private long                pollset;
+    private short[]                     revents;
+    private SelectionKeyImpl[]          keyset;
+    private long                        pollset;
+    private ArrayList<SelectionKey>     selected;
 
     private static native long  create0(int size)
         throws OutOfMemoryError,
@@ -61,9 +63,10 @@ final class PosixSelector extends Abstra
     public PosixSelector(int size)
     {
         super(size);
-        pollset = create0(size);
-        revents = new short[size];
-        keyset  = new SelectionKeyImpl[size];
+        pollset  = create0(size);
+        revents  = new short[size];
+        keyset   = new SelectionKeyImpl[size];
+        selected = new ArrayList<SelectionKey>();
     }
 
     private void ensureValid()
@@ -112,6 +115,40 @@ final class PosixSelector extends Abstra
     }
 
     @Override
+    public List<SelectionKey> select(int timeout)
+        throws ClosedSelectorException,
+               IOException
+    {
+        ensureValid();
+        selected.clear();
+        int ns = wait0(pollset, keyset, revents, timeout, autoCancel);
+        selected.ensureCapacity(ns);
+        for (int i = 0; i < ns; i++) {
+            selected.add(keyset[i]);
+            keyset[i].revents  = revents[i];
+            keyset[i].selected = false;
+        }
+        return selected;
+    }
+
+    @Override
+    public List<SelectionKey> clear()
+        throws ClosedSelectorException,
+               IOException
+    {
+        ensureValid();
+        selected.clear();
+        int ns = clr0(pollset, keyset);
+        selected.ensureCapacity(ns);
+        for (int i = 0; i < ns; i++) {
+            selected.add(keyset[i]);
+            keyset[i].revents  = 0;
+            keyset[i].selected = false;
+        }
+        return selected;
+    }
+
+    @Override
     protected void cancel(SelectionKey key)
         throws ClosedSelectorException,
                IllegalSelectorException

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=1133767&r1=1133766&r2=1133767&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
Thu Jun  9 10:15:07 2011
@@ -20,6 +20,7 @@ package org.apache.commons.runtime.net;
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.util.List;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.InvalidDescriptorException;
 import org.apache.commons.runtime.InvalidRangeException;
@@ -161,4 +162,21 @@ public abstract class Selector implement
     {
         autoCancel = on;
     }
+
+    public abstract List<SelectionKey> select(int timeout)
+        throws ClosedSelectorException,
+               IOException;
+
+    public final List<SelectionKey> select()
+        throws ClosedSelectorException,
+               IOException
+    {
+        return select(-1);
+    }
+               
+    public abstract List<SelectionKey> clear()
+        throws ClosedSelectorException,
+               IOException;
+
+    
 }

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java?rev=1133767&r1=1133766&r2=1133767&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java
Thu Jun  9 10:15:07 2011
@@ -17,18 +17,7 @@
 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 java.util.List;
 import org.testng.annotations.*;
 import org.testng.Assert;
 
@@ -66,6 +55,9 @@ public class TestPosixEndpoint extends A
             while (true) {
                 try {
                     System.out.println("Accepting connection ...");
+                    List<SelectionKey> set = ps.select();
+                    System.out.println("Accepted  connection: " + set.size());
+                    
                     LocalEndpoint e = ss.accept();
                     assertNotNull(e.descriptor());
                 } catch (Exception x) {
@@ -89,8 +81,9 @@ public class TestPosixEndpoint extends A
         Selector             ps = Selector.open(EndpointType.LOCAL);
         LocalServerEndpoint  ss = new LocalServerEndpoint();
         LocalEndpointAddress sa = new LocalEndpointAddress("/tmp/acrposixep1.sock");
+        ss.configureBlocking(false);
         ss.bind(sa);
-        // ss.configureBlocking(false);
+        // ps.setAutoCancel(true);
         Acceptor aw = new Acceptor(ss, ps);
         aw.setName("TestAcceptorThread");
         aw.start();
@@ -111,9 +104,12 @@ public class TestPosixEndpoint extends A
         cs.connect(sa);
         assertTrue(cs.isBlocking());
         cs.close();
-        ps.close();
         // Wait for acceptor thread to finish
         aw.join();
+        List<SelectionKey> set = ps.clear();
+        // Shoud equal zero if autoCancel is set to true
+        assertEquals(set.size(), 1);
+        ps.close();
     }
 
 }



Mime
View raw message