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/28 12:32:03 UTC

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

Author: mturk
Date: Sat May 28 10:32:02 2011
New Revision: 1128591

URL: http://svn.apache.org/viewvc?rev=1128591&view=rev
Log:
Start implementing the network api

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java   (with props)
Modified:
    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/SocketDescriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.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/Endpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java?rev=1128591&r1=1128590&r2=1128591&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 Sat May 28 10:32:02 2011
@@ -32,6 +32,19 @@ import org.apache.commons.runtime.io.Des
 public abstract class Endpoint implements Closeable
 {
     /**
+     * Further reception will be disallowed.
+     */
+    public static final int     SHUT_RD     = 0;
+    /**
+     * Further transimission will be disallowed.
+     */
+    public static final int     SHUT_WR     = 1;
+    /**
+     * Further receptions and transmissions will be disallowed.
+     */
+    public static final int     SHUT_RDWR   = 2;
+
+    /**
      * Creates a new Endpoint object.
      */
     protected Endpoint()

Added: 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=1128591&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketDescriptor.java Sat May 28 10:32:02 2011
@@ -0,0 +1,111 @@
+/* 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.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.SyncFailedException;
+import java.net.SocketException;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.Descriptor;
+
+/**
+ * Package private Local Socket Descriptor.
+ * <p>
+ * Local socket represents either unix domain socket or a windows pipe,
+ * depending on the operating system.
+ * </p>
+ * @since Runtime 1.0
+ */
+final class LocalSocketDescriptor extends Descriptor
+{
+
+    private boolean blocking    = false;
+
+    private static native int close0(int fd);
+    private static native int sendz0(int fd);
+
+    public LocalSocketDescriptor()
+    {
+    }
+
+    public LocalSocketDescriptor(int fd)
+    {
+        this.fd = fd;
+    }
+
+    @Override
+    public void close()
+        throws IOException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException(Local.sm.get("socketd.CLOSED"));
+        int rc = close0(fd);
+        if (rc != 0)
+            throw new SocketException(Status.describe(fd));
+    }
+
+    @Override
+    public void sync()
+        throws SyncFailedException, IOException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        int rc = sendz0(fd);
+        if (rc != 0)
+            throw new SocketException(Status.describe(fd));
+    }
+
+    @Override
+    public void flush()
+        throws SyncFailedException, IOException
+    {
+    }
+
+    @Override
+    public boolean isBlocking()
+        throws IOException
+    {
+        return blocking;
+    }
+
+    /**
+     * Called by the garbage collector when the object is destroyed.
+     * The class will free internal resources allocated by the Operating system.
+     * @see Object#finalize()
+     * @throws Throwable the {@code Exception} raised by this method.
+     */
+    @Override
+    protected final void finalize()
+        throws Throwable
+    {
+        if (valid()) {
+            try {
+                close();
+            } catch (Exception e) {
+                // Ignore exceptions during close
+            }
+            finally {
+                fd = -1;
+            }
+        }
+    }
+
+
+}

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

Added: 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=1128591&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java Sat May 28 10:32:02 2011
@@ -0,0 +1,59 @@
+/*
+ * 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.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.SyncFailedException;
+import java.net.SocketException;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.Descriptor;
+
+/**
+ * This class represents a local socket endpoint.
+ * <p>
+ * Local socket represents either unix domain socket or a windows pipe,
+ * depending on the operating system.
+ * </p>
+ */
+public class LocalSocketEndpoint extends Endpoint
+{
+    private LocalSocketDescriptor  sd;
+    /**
+     * Creates a new unconnected socket object.
+     */
+    private LocalSocketEndpoint()
+    {
+    }
+
+    @Override
+    public Descriptor descriptor()
+    {
+        return sd;
+    }
+
+    @Override
+    public void close()
+        throws IOException
+    {
+        sd.close();
+    }
+
+}

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

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java?rev=1128591&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java Sat May 28 10:32:02 2011
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+/**
+ * Creates a new local socket selector instance.
+ */
+public final class LocalSocketSelectorFactory
+{
+    private LocalSocketSelectorFactory()
+    {
+        // No instance
+    }
+
+    private static final int             maxSize;
+    private static native int            nmax0();
+    private static native SocketSelector new0(int size)
+        throws OutOfMemoryError;
+    static {
+        maxSize = nmax0();
+    }
+
+    /**
+     * Creates a new selector instance.
+     */
+    public static SocketSelector createSelector(int size)
+        throws OutOfMemoryError
+    {
+        if (size == 0)
+            size = maxSize;
+        return new0(size);
+    }
+
+}

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

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=1128591&r1=1128590&r2=1128591&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 Sat May 28 10:32:02 2011
@@ -46,6 +46,7 @@ final class SocketDescriptor extends Des
         this.fd = fd;
     }
 
+    @Override
     public void close()
         throws IOException
     {
@@ -56,6 +57,7 @@ final class SocketDescriptor extends Des
             throw new SocketException(Status.describe(fd));
     }
 
+    @Override
     public void sync()
         throws SyncFailedException, IOException
     {
@@ -66,11 +68,13 @@ final class SocketDescriptor extends Des
             throw new SocketException(Status.describe(fd));
     }
 
+    @Override
     public void flush()
         throws SyncFailedException, IOException
     {
     }
 
+    @Override
     public boolean isBlocking()
         throws IOException
     {

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java?rev=1128591&r1=1128590&r2=1128591&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java Sat May 28 10:32:02 2011
@@ -27,28 +27,11 @@ import org.apache.commons.runtime.io.Inv
  */
 public abstract class SocketSelector extends Selector
 {
-    protected SocketSelector()
-    {
-        // No instance
-    }
-
-    private static final int             maxSize;
-    private static native int            nmax0();
-    private static native SocketSelector new0(int size)
-        throws OutOfMemoryError;
-    static {
-        maxSize = nmax0();
-    }
-
     /**
-     * Creates a new pollset instance.
+     * Creates a new object instance.
      */
-    public static SocketSelector newInstance(int size)
-        throws OutOfMemoryError
+    protected SocketSelector()
     {
-        if (size == 0)
-            size = maxSize;
-        return new0(size);
     }
 
 }

Added: 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=1128591&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java Sat May 28 10:32:02 2011
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+/**
+ * Creates a new socket selector instance.
+ */
+public final class SocketSelectorFactory
+{
+    private SocketSelectorFactory()
+    {
+        // No instance
+    }
+
+    private static final int             maxSize;
+    private static native int            nmax0();
+    private static native SocketSelector new0(int size)
+        throws OutOfMemoryError;
+    static {
+        maxSize = nmax0();
+    }
+
+    /**
+     * Creates a new selector instance.
+     */
+    public static SocketSelector createSelector(int size)
+        throws OutOfMemoryError
+    {
+        if (size == 0)
+            size = maxSize;
+        return new0(size);
+    }
+
+}

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

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java?rev=1128591&r1=1128590&r2=1128591&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java Sat May 28 10:32:02 2011
@@ -29,7 +29,7 @@ import org.apache.commons.runtime.System
 import org.apache.commons.runtime.Errno;
 
 /**
- * Selector implementation class.
+ * Socket Selector implementation class.
  * <p>
  * </p>
  *

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c?rev=1128591&r1=1128590&r2=1128591&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c Sat May 28 10:32:02 2011
@@ -107,7 +107,7 @@ static short reventt(short event)
 
 #if POLLSET_USE_POLL
 
-ACR_NET_EXPORT(jobject, SocketSelector, new0)(JNI_STDARGS, jint size)
+ACR_NET_EXPORT(jobject, SocketSelectorFactory, new0)(JNI_STDARGS, jint size)
 {
     if (_clazzn.u == 1)
         return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
@@ -118,9 +118,20 @@ ACR_NET_EXPORT(jobject, SocketSelector, 
     return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
 }
 
-ACR_NET_EXPORT(jint, SocketSelector, nmax0)(JNI_STDARGS)
+ACR_NET_EXPORT(jobject, LocalSocketSelectorFactory, new0)(JNI_STDARGS, jint size)
 {
-    int nm = 0;
+    if (_clazzn.u == 1)
+        return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
+    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+        return 0;
+    R_LOAD_METHOD(0000, 0);
+    _clazzn.u = 1;
+    return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
+}
+
+static int maxopendesc(void)
+{
+    int nm = 65536;
 #if HAVE_SYS_RESOURCE_H
     struct rlimit rl;
 
@@ -131,9 +142,21 @@ ACR_NET_EXPORT(jint, SocketSelector, nma
         nm = (int)sysconf(_SC_OPEN_MAX);
     if (nm > 1)
         --nm;
+    else
+        nm = 1023;
     return nm;
 }
 
+ACR_NET_EXPORT(jint, SocketSelectorFactory, nmax0)(JNI_STDARGS)
+{
+    return maxopendesc();
+}
+
+ACR_NET_EXPORT(jint, LocalSocketSelectorFactory, nmax0)(JNI_STDARGS)
+{
+    return maxopendesc();
+}
+
 ACR_UNX_EXPORT(jlong, SocketSelectorImpl, create0)(JNI_STDARGS, jint size)
 {
     int rc;

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=1128591&r1=1128590&r2=1128591&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 Sat May 28 10:32:02 2011
@@ -27,3 +27,18 @@
 #include <poll.h>
 #include <sys/un.h>
 
+ACR_NET_EXPORT(jint, LocalSocketDescriptor, close0)(JNI_STDARGS, jint fd)
+{
+    if (r_close(fd) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_NET_EXPORT(jint, LocalSocketDescriptor, sendz0)(JNI_STDARGS, jint fd)
+{
+    if (r_write(fd, &fd, 0) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}