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/04 11:26:12 UTC

svn commit: r1099380 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ native/ native/os/unix/ native/os/win32/

Author: mturk
Date: Wed May  4 09:26:12 2011
New Revision: 1099380

URL: http://svn.apache.org/viewvc?rev=1099380&view=rev
Log:
Add SocketDescriptor

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java?rev=1099380&r1=1099379&r2=1099380&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java Wed May  4 09:26:12 2011
@@ -33,7 +33,7 @@ import java.io.SyncFailedException;
  * </p>
  * @since Runtime 1.0
  */
-public abstract class Descriptor implements Closeable, Syncable
+public abstract class Descriptor implements Device, Syncable
 {
 
     /** Operating system descriptor.
@@ -113,7 +113,7 @@ public abstract class Descriptor impleme
      * @return {@code true} if descriptor is valid and not closed
      *         {@code false} otherwise.
      */
-    public boolean valid()
+    public final boolean valid()
     {
         // true if both int is negative or zero
         // Descriptor is always assured to be above the stderr (#3)
@@ -124,10 +124,29 @@ public abstract class Descriptor impleme
     }
 
     /**
+     * Test wather or not every I/O operation on {@code this} descriptor will
+     * block until it completes.
+     *
+     * @return {@code true} if, and only if, this device
+     *         is in blocking mode.
+     *
+     * @throws IOException if an I/O error occurs while determining the
+     *         blocking state.
+     */
+    public boolean isBlocking()
+        throws IOException
+    {
+        // Default implementation presumes that any OS descriptor
+        // is in blocking state when created.
+        // This is true both for files and sockets.
+        return true;
+    }
+
+    /**
      * Get underlying Operating system descriptor.
      * @return operating system descriptor.
      */
-    public int fd()
+    public final int fd()
     {
         return fd;
     }

Added: 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=1099380&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java Wed May  4 09:26:12 2011
@@ -0,0 +1,103 @@
+/* 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 Socket Descriptor 
+ * @since Runtime 1.0
+ */
+final class SocketDescriptor extends Descriptor
+{
+
+    private boolean blocking    = false;
+
+    private static native int close0(int fd);
+    private static native int sendz0(int fd);
+
+    public SocketDescriptor()
+    {
+    }
+
+    public SocketDescriptor(int fd)
+    {
+        this.fd = fd;
+    }
+
+    public void close()
+        throws IOException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException("Socket is already closed");
+        int rc = close0(fd);
+        if (rc != 0)
+            throw new SocketException(Status.describe(fd));
+    }
+
+    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));
+    }
+
+    public void flush()
+        throws SyncFailedException, IOException
+    {
+    }
+
+    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/SocketDescriptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1099380&r1=1099379&r2=1099380&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed May  4 09:26:12 2011
@@ -79,6 +79,7 @@ WIN32_SOURCES=\
 	$(TOPDIR)\os\win32\dso.c \
 	$(TOPDIR)\os\win32\exec.c \
 	$(TOPDIR)\os\win32\execmem.c \
+	$(TOPDIR)\os\win32\inetsock.c \
 	$(TOPDIR)\os\win32\init.c \
 	$(TOPDIR)\os\win32\os.c \
 	$(TOPDIR)\os\win32\path.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1099380&r1=1099379&r2=1099380&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Wed May  4 09:26:12 2011
@@ -63,6 +63,7 @@ UNIX_SOURCES=\
 	$(TOPDIR)/os/unix/dso.c \
 	$(TOPDIR)/os/unix/exec.c \
 	$(TOPDIR)/os/unix/execmem.c \
+	$(TOPDIR)/os/unix/inetsock.c \
 	$(TOPDIR)/os/unix/init.c \
 	$(TOPDIR)/os/unix/platform.c \
 	$(TOPDIR)/os/unix/posixapi.c \

Added: 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=1099380&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c Wed May  4 09:26:12 2011
@@ -0,0 +1,40 @@
+/* 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.
+ */
+
+#include "acr/jnitypes.h"
+#include "acr/error.h"
+#include "acr/memory.h"
+#include "acr/unsafe.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+#include <poll.h>
+#include <sys/un.h>
+
+ACR_NET_EXPORT(jint, SocketDescriptor, close0)(JNI_STDARGS, jint fd)
+{
+    if (r_close(fd) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_NET_EXPORT(jint, SocketDescriptor, sync0)(JNI_STDARGS, jint fd)
+{
+    if (r_write(fd, &fd, 0) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
------------------------------------------------------------------------------
    svn:eol-style = native

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=1099380&r1=1099379&r2=1099380&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 May  4 09:26:12 2011
@@ -27,10 +27,6 @@
 #include <poll.h>
 #include <sys/un.h>
 
-#if !HAVE_SYS_UN_H
-# error Missing sys/un.h header
-#endif
-
 ACR_NET_EXPORT(jstring, LocalSocketAddress, prefix0)(JNI_STDARGS)
 {
     return CSTR_TO_JSTRING(VAR_RUN_PATH "/");

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c?rev=1099380&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c Wed May  4 09:26:12 2011
@@ -0,0 +1,38 @@
+/* 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.
+ */
+
+#include "acr/jnitypes.h"
+#include "acr/error.h"
+#include "acr/memory.h"
+#include "acr/unsafe.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+
+ACR_NET_EXPORT(jint, SocketDescriptor, close0)(JNI_STDARGS, jint fd)
+{
+    if (closesocket((SOCKET)fd) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_NET_EXPORT(jint, SocketDescriptor, sync0)(JNI_STDARGS, jint fd)
+{
+    if (send((SOCKET)fd, &fd, 0, 0) == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/inetsock.c
------------------------------------------------------------------------------
    svn:eol-style = native