commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1130573 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/os/unix/ native/shared/
Date Thu, 02 Jun 2011 14:32:55 GMT
Author: mturk
Date: Thu Jun  2 14:32:55 2011
New Revision: 1130573

URL: http://svn.apache.org/viewvc?rev=1130573&view=rev
Log:
Implement initial bind/accept for local endpoints

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractAddress.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractAddress.java?rev=1130573&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractAddress.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractAddress.java
Thu Jun  2 14:32:55 2011
@@ -0,0 +1,51 @@
+/*
+ * 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;
+
+/**
+ * This class represents a network address.
+ */
+final class AbstractAddress extends EndpointAddress
+{
+    public static final int            SALEN;
+    private static native int           size0();
+
+    static {
+        SALEN = size0();
+    }
+
+    /**
+     * Creates a new EndpointAddress object.
+     */
+    private AbstractAddress()
+    {
+        // No instance
+    }
+    
+    /**
+     * Creates a new AbstractAddress object.
+     * @param family The address family to which the endpoint belongs.
+     */
+    public AbstractAddress(AddressFamily family, byte[] sa)
+    {
+        super(family);
+        this.sa = sa;
+    }
+
+}

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

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java?rev=1130573&r1=1130572&r2=1130573&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
Thu Jun  2 14:32:55 2011
@@ -34,15 +34,6 @@ import org.apache.commons.runtime.Invali
 public class InetSocketAddress extends SocketAddress
 {
 
-    private static native int   size0();
-    private static final  int   sasize;
-
-    static {
-        // Gets the size of internal structure
-        //
-        sasize = size0();
-    }
-    
     protected InetSocketAddress()
     {
     }
@@ -68,12 +59,13 @@ public class InetSocketAddress extends S
     {        
         if (port < 0 || port > 65535)
             throw new InvalidRangeException(Local.sm.get("port.ERANGE"));
+        int salen = AbstractAddress.SALEN;
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.UNSPEC, hostname, null, port);
-        int n = sa.length / sasize;
-        SocketAddress[] a = new SocketAddress[n];
-        for (int i = 0; i < n; i++) {
-            byte[] ba = new byte[sasize];
-            System.arraycopy(sa, i * sasize, ba, 0, sasize);
+        int naddr = sa.length / salen;
+        SocketAddress[] a = new SocketAddress[naddr];
+        for (int i = 0; i < naddr; i++) {
+            byte[] ba = new byte[salen];
+            System.arraycopy(sa, i * salen, ba, 0, salen);
             // Create a new SocketAddress
             a[i] = new SocketAddressImpl(ba);
         }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java?rev=1130573&r1=1130572&r2=1130573&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java
Thu Jun  2 14:32:55 2011
@@ -38,6 +38,7 @@ import org.apache.commons.runtime.Status
 public class LocalEndpoint extends Endpoint
 {
     private final LocalDescriptor       sd;
+    private EndpointAddress             ea;
     private SelectionKeyImpl            key;
     private boolean                     blocking = false;
 
@@ -55,12 +56,13 @@ public class LocalEndpoint extends Endpo
     /**
      * Creates a new socket object from socket descriptor.
      */
-    public LocalEndpoint(LocalDescriptor sd)
+    public LocalEndpoint(LocalDescriptor sd, EndpointAddress ea)
     {
         super(EndpointType.LOCAL);
         if (sd == null)
             throw new NullPointerException();
         this.sd = sd;
+        this.ea = ea;
     }
 
     @Override

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1130573&r1=1130572&r2=1130573&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
Thu Jun  2 14:32:55 2011
@@ -37,6 +37,7 @@ import org.apache.commons.runtime.Status
  */
 public class LocalServerEndpoint extends Endpoint
 {
+    private static final int            LISTEN_BACKLOG =  50;
     private final LocalDescriptor       sd;
     private SelectionKeyImpl            key;
     private LocalEndpointAddress        sa;
@@ -153,20 +154,30 @@ public class LocalServerEndpoint extends
         return key.queue(ops);
     }
 
-    public void bind(LocalEndpointAddress endpoint)
+    private static native int           bind0(int fd, byte[] sa, int backlog);
+    private static native int           accept0(int fd, byte[] addr)
+        throws SocketException;
+
+    public void bind(LocalEndpointAddress endpoint, int backlog)
         throws IOException
     {
         if (bound)
             throw new IOException(Local.sm.get("endpoint.EBOUND"));
-        // TODO: Implement bind
+        if (backlog < LISTEN_BACKLOG)
+            backlog = LISTEN_BACKLOG;
+        int rc = bind0(sd.fd(), sa.sockaddr(), backlog);
+        if (rc != 0)
+            throw new IOException(Status.describe(rc));            
     }
 
     public LocalEndpoint accept()
         throws IOException
     {
-        LocalEndpoint ep = new LocalEndpoint();
+        byte[] addr = new byte[AbstractAddress.SALEN];
 
-        // TODO: Implement accept
-        return ep;
+        int fd = accept0(sd.fd(), addr);
+        LocalDescriptor ad = new LocalDescriptor(fd);
+        AbstractAddress aa = new AbstractAddress(AddressFamily.LOCAL, addr);
+        return new LocalEndpoint(ad, aa);
     }
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1130573&r1=1130572&r2=1130573&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
Thu Jun  2 14:32:55 2011
@@ -34,6 +34,7 @@ import org.apache.commons.runtime.Status
 public class SocketEndpoint extends Endpoint
 {
     private final SocketDescriptor      sd;
+    private EndpointAddress             ea;
     private SelectionKeyImpl            key;
     private boolean                     blocking = false;
 
@@ -51,12 +52,13 @@ public class SocketEndpoint extends Endp
     /**
      * Creates a new socket object from socket descriptor.
      */
-    public SocketEndpoint(SocketDescriptor sd)
+    public SocketEndpoint(SocketDescriptor sd, EndpointAddress ea)
     {
         super(EndpointType.SOCKET);
         if (sd == null)
             throw new NullPointerException();
         this.sd = sd;
+        this.ea = ea;
     }
 
     @Override

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=1130573&r1=1130572&r2=1130573&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 Thu Jun  2 14:32:55 2011
@@ -21,12 +21,18 @@
 #include "acr/string.h"
 #include "acr/memory.h"
 #include "acr/iofd.h"
+#include "acr/netapi.h"
 #include "acr/unsafe.h"
 #include "acr/port.h"
 #include "arch_opts.h"
 #include <poll.h>
 #include <sys/un.h>
 
+#define SOCKADDR_CAST(BA) \
+    (acr_sockaddr_t *)AcrGetArrayCritical(env, (BA))
+#define SOCKADDR_RELEASE(BA, SA) \
+    AcrReleaseArrayCritical(env, (BA), (SA))
+
 ACR_NET_EXPORT(jint, LocalDescriptor, close0)(JNI_STDARGS, jint fd)
 {
     if (r_close(fd) == -1)
@@ -47,3 +53,65 @@ ACR_NET_EXPORT(jint, LocalEndpoint, nonb
 {
     return AcrNonblock(fd, on);
 }
+
+ACR_NET_EXPORT(jint, LocalServerEndpoint, bind0)(JNI_STDARGS, jint fd,
+                                                 jbyteArray aa,
+                                                 jint backlog)
+{
+    int rc = 0;
+    acr_sockaddr_t *sa = SOCKADDR_CAST(aa);
+
+    if (bind(fd, (const struct sockaddr *)&sa->sa, sa->salen) == -1) {
+        rc = ACR_GET_NETOS_ERROR();
+        SOCKADDR_RELEASE(aa, sa);
+        return rc;
+    }
+    if (listen(fd, backlog) == -1)
+        rc = ACR_GET_NETOS_ERROR();
+    SOCKADDR_RELEASE(aa, sa);
+    return rc;
+}
+
+ACR_NET_EXPORT(jint, LocalServerEndpoint, accept0)(JNI_STDARGS, jint fd,
+                                                   jbyteArray aa)
+{
+    int sd;
+    acr_sockaddr_t sa;
+    socklen_t      salen;
+
+    memset(&sa, 0, sizeof(sa));
+    salen = ISIZEOF(struct sockaddr_un);
+
+    do {
+#if !HAVE_ACCEPT4
+        sd = accept(fd,  (struct sockaddr *)&sa.sa, &salen);
+#else
+        sd = accept4(fd, (struct sockaddr *)&sa.sa, &salen, SOCK_CLOEXEC);
+#endif
+    } while (sd == -1 && errno == EINTR);
+
+    if (sd == -1) {
+        ACR_THROW_NET_ERRNO();
+        return -1;
+    }
+#if !HAVE_ACCEPT4
+    AcrCloseOnExec(sd, 1);
+#endif
+#if defined(DEBUG) || defined(_DEBUG)
+    if (sa.sa.unx.sun_family != AF_LOCAL) {
+        fprintf(stderr, "Expected AF_LOCAL but found %d\n", sa.sa.unx.sun_family);
+        fflush(stderr);
+    }
+#endif    
+    sa.family  = AF_LOCAL;
+    sa.salen   = ISIZEOF(struct sockaddr_un);
+    sa.addrlen = ISIZEOF(sa.sa.unx.sun_path);
+    sa.iplen   = sa.addrlen;
+    sa.addrcnt = 1;
+    /* Copy sockaddr to the provided byte array
+     * Note that byte array must be of valid size
+     */
+    (*env)->SetByteArrayRegion(env, aa, 0, ISIZEOF(acr_sockaddr_t), (jbyte *)&sa);
+
+    return sd;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1130573&r1=1130572&r2=1130573&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Thu Jun  2 14:32:55 2011
@@ -875,7 +875,7 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
     return ba;
 }
 
-ACR_NET_EXPORT(jint, InetSocketAddress, size0)(JNI_STDARGS)
+ACR_NET_EXPORT(jint, AbstractAddress, size0)(JNI_STDARGS)
 {
     return ISIZEOF(acr_sockaddr_t);
 }



Mime
View raw message