Author: mturk
Date: Fri Jul  8 06:58:05 2011
New Revision: 1144178

URL: http://svn.apache.org/viewvc?rev=1144178&view=rev
Log:
Add and use LegacySelector class. This is used only on pre Vista windows

Added:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LegacySelector.java
   (with props)
Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
    commons/sandbox/runtime/trunk/src/main/native/os/win32/selectset.c

Added: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LegacySelector.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LegacySelector.java?rev=1144178&view=auto
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LegacySelector.java
 (added)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LegacySelector.java
 Fri Jul  8 06:58:05 2011
@@ -0,0 +1,191 @@
+/* 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.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;
+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;
+import org.apache.commons.runtime.Status;
+
+/**
+ * Legacy select based socket selector implementation class.
+ * LegacySelector uses select() function for waiting on I/O events.
+ *
+ * @since Runtime 1.0
+ */
+final class LegacySelector extends AbstractSelector
+{
+
+    private short[]                     revents;
+    private SelectionKeyImpl[]          keyset;
+    private long                        pollset;
+    private ArrayList<SelectionKey>     selected;
+
+    private static native long  create0(int size)
+        throws OutOfMemoryError,
+               SystemException;
+    private static native void  wakeup0(long pollset);
+    private static native int   destroy0(long pollset);
+    private static native int   size0(long pollset);
+    private static native int   add0(long pollset, SelectionKeyImpl key, long 
fd, int events, int ttl);
+    private static native int   del0(long pollset, SelectionKeyImpl key, long 
fd);
+    private static native int   clr0(long pollset, SelectionKeyImpl[] set)
+        throws IllegalStateException;
+    private static native int   wait0(long pollset, SelectionKeyImpl[] set, 
short[] events,
+                                      int timeout, boolean autocancel)
+        throws IllegalStateException, IOException;
+
+    /*
+     * Created from native
+     */
+    public LegacySelector(int size)
+    {
+        super(size);
+        pollset  = create0(size);
+        revents  = new short[size];
+        keyset   = new SelectionKeyImpl[size];
+        selected = new ArrayList<SelectionKey>();
+    }
+
+    private void ensureValid()
+        throws ClosedSelectorException
+    {
+        if (pollset == 0L)
+            throw new ClosedSelectorException();
+    }
+
+    @Override
+    public int size()
+        throws ClosedSelectorException
+    {
+        ensureValid();
+        return size0(pollset);
+    }
+
+    @Override
+    public void interrupt()
+        throws ClosedSelectorException
+    {
+        ensureValid();
+        wakeup0(pollset);
+    }
+
+    @Override
+    public SelectionKey queue(SelectionKeyImpl key, Descriptor sd)
+        throws ClosedSelectorException,
+               OverflowException,
+               IOException
+    {
+        ensureValid();
+        key.selected = true;
+        int rc = add0(pollset, key, sd.fd(), key.ievents, key.timeout());
+        if (rc != 0) {
+            // Add failed
+            key.selected = false;
+            if (rc != Errno.EALREADY) {
+                if (rc == Errno.EOVERFLOW)
+                    throw new OverflowException();
+                else
+                    throw new IOException(Status.describe(rc));
+            }
+        }
+        return key;
+    }
+
+    @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
+    {
+        ensureValid();
+        SelectionKeyImpl skey = (SelectionKeyImpl)key;
+        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
+    public boolean isOpen()
+    {
+        return pollset != 0L;
+    }
+
+    @Override
+    public void close()
+        throws IOException
+    {
+        if (pollset != 0L) {
+            long ps = pollset;
+            pollset = 0L;
+            int rc  = destroy0(ps);
+            if (rc != 0)
+                throw new IOException(Status.describe(rc));
+        }
+    }
+
+}
+

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

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java?rev=1144178&r1=1144177&r2=1144178&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
 Fri Jul  8 06:58:05 2011
@@ -58,6 +58,8 @@ final class LocalSelectorFactory
         if (size < 1 || size > MAX_CAPACITY)
             throw new InvalidRangeException(Local.sm.get("selector.ERANGE"));
         switch (type) {
+            case 0:
+                return new LegacySelector(size);
             case 1:
                 return new PollSelector(size);
             case 2:

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java?rev=1144178&r1=1144177&r2=1144178&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
 Fri Jul  8 06:58:05 2011
@@ -59,6 +59,7 @@ final class SocketSelectorFactory
             throw new InvalidRangeException(Local.sm.get("selector.ERANGE"));
         switch (type) {
             case 0:     // Posix select
+                return new LegacySelector(size);
             case 1:     // Posix poll
                 return new PollSelector(size);
             case 2:     // Platform optimal selector

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/selectset.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/selectset.c?rev=1144178&r1=1144177&r2=1144178&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/selectset.c 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/selectset.c Fri Jul  
8 06:58:05 2011
@@ -70,7 +70,10 @@ ACR_NET_EXPORT(jint, SocketSelectorFacto
 
 ACR_NET_EXPORT(jint, LocalSelectorFactory, type0)(JNI_STDARGS)
 {
-    return ACR_PS_TYPE_WINPIPE;
+    if (ACR_HAVE_LATE_DLL_FUNC(WSAPoll))
+        return ACR_PS_TYPE_POLL;
+    else
+        return ACR_PS_TYPE_SELECT;
 }
 
 ACR_NET_EXPORT(jint, SocketSelectorFactory, size0)(JNI_STDARGS)
@@ -83,7 +86,10 @@ ACR_NET_EXPORT(jint, SocketSelectorFacto
 
 ACR_NET_EXPORT(jint, LocalSelectorFactory, size0)(JNI_STDARGS)
 {
-    return MAXIMUM_WAIT_OBJECTS - 1;
+    if (ACR_HAVE_LATE_DLL_FUNC(WSAPoll))
+        return 65535;
+    else
+        return FD_SETSIZE - 1;
 }
 
 static int wwait(acr_pollset_t *ps)
@@ -107,10 +113,6 @@ ACR_NET_EXPORT(jlong, LegacySelector, cr
     int rc;
     acr_pollset_t *ps;
 
-    if (!ACR_HAVE_LATE_DLL_FUNC(WSAPoll)) {
-        ACR_THROW_NET_ERROR(ACR_ENOTIMPL);
-        return 0;
-    }
     ps = ACR_TALLOC(acr_pollset_t);
     if (ps == 0)
         return 0;


Reply via email to