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/06 08:36:25 UTC

svn commit: r1100101 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/shared/

Author: mturk
Date: Fri May  6 06:36:25 2011
New Revision: 1100101

URL: http://svn.apache.org/viewvc?rev=1100101&view=rev
Log:
Add address.next()  support

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java?rev=1100101&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java Fri May  6 06:36:25 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;
+
+import java.io.File;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import org.apache.commons.runtime.Memory;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.InvalidArgumentException;
+
+/**
+ * This class represents a generic socket endpoint.
+ * It is used by {@link SocketAddress#next()} to create
+ * a next address object form internal sockaddr structure.  
+ */
+final class AbstractSocketAddress extends SocketAddress
+{
+
+    private AbstractSocketAddress()
+    {
+        // No instance
+    }
+
+    /**
+     * Create a new socket address with given family.
+     */
+    public AbstractSocketAddress(AddressFamily family)
+        throws SystemException, InvalidArgumentException
+    {
+        super(family);
+    }
+
+}

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

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java?rev=1100101&r1=1100100&r2=1100101&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java Fri May  6 06:36:25 2011
@@ -33,9 +33,10 @@ public abstract class EndpointAddress ex
      */
     private long sa;
 
-    private static native void init0();
-    private static native int  family0(long sa);
-    private static native void free0(long sa);
+    private static native void    init0();
+    private static native int     family0(long sa);
+    private static native void    free0(long sa);
+    private static native boolean hasnext0(long sa);
 
     static {
         init0();
@@ -68,6 +69,14 @@ public abstract class EndpointAddress ex
     }
 
     /**
+     * Returns {@code true} if the address has more elements.
+     */
+    public final boolean hasNext()
+    {
+        return hasnext0(sa);
+    }
+
+    /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.
      * @see Object#finalize()

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java?rev=1100101&r1=1100100&r2=1100101&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java Fri May  6 06:36:25 2011
@@ -56,6 +56,8 @@ public abstract class SocketAddress exte
         throws InvalidArgumentException;
     private native int     sockaddr0(String hostname, int family, int port, int flags);
     private native boolean equals0(SocketAddress other);
+    private native int     next0();
+    private native int     next1(SocketAddress next);
 
     /**
      * Creates an new object
@@ -159,6 +161,30 @@ public abstract class SocketAddress exte
     }
 
     /**
+     * Gets the next address.
+     */
+    public SocketAddress next()
+    {
+        int family = next0();
+        if (family == -1)
+            return null;
+        AbstractSocketAddress next;
+        try {
+            next = new AbstractSocketAddress(AddressFamily.valueOf(family));
+        } catch (Exception e) {
+            // This should never happen.
+            // Throw OOM since this is the only logical reason.
+            throw new OutOfMemoryError();
+        }
+        int rc = next1(next);
+        if (rc != 0) {
+            next = null;
+            throw new RuntimeException("internal error");
+        }
+        return next;
+    }
+
+    /**
      * Compares this {@code SocketAddress} to the specified object.
      *
      * @param other the reference {@code SocketAddress} with which to compare.

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=1100101&r1=1100100&r2=1100101&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Fri May  6 06:36:25 2011
@@ -791,6 +791,15 @@ ACR_NET_EXPORT(jint, EndpointAddress, fa
     return 0;
 }
 
+ACR_NET_EXPORT(jboolean, EndpointAddress, hasnext0)(JNI_STDARGS, jlong sa)
+{
+    acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
+    if (a != 0 && a->next != 0)
+        return JNI_TRUE;
+    else
+        return JNI_FALSE;
+}
+
 acr_sockaddr_t *
 AcrGetSockaddr(JNI_STDARGS)
 {
@@ -874,7 +883,6 @@ ACR_NET_EXPORT(jint, SocketAddress, port
         return 0;
 }
 
-
 ACR_NET_EXPORT(jint, SocketAddress, sockaddr0)(JNI_STDARGS, jstring hostname,
                                                jint family, jint port, jint flags)
 {
@@ -920,3 +928,36 @@ ACR_NET_EXPORT(jboolean, SocketAddress, 
     return JNI_FALSE; /* not equal */
 }
 
+ACR_NET_EXPORT(jint, SocketAddress, next0)(JNI_STDARGS)
+{
+    acr_sockaddr_t *sa = AcrGetSockaddr(env, obj);
+    if (sa == 0)
+        return -1;
+    switch (sa->family) {
+        case AF_INET:
+            return 1;
+        case AF_INET6:
+            return 2;
+        case AF_LOCAL:
+            return 3;
+        default:
+        break;
+    }
+    return 0;
+}
+
+ACR_NET_EXPORT(jint, SocketAddress, next1)(JNI_STDARGS, jobject na)
+{
+    int rc;
+    acr_sockaddr_t *sa = AcrGetSockaddr(env, obj);
+
+    if (sa == 0 || sa->next == 0)
+        return ACR_EBADF;
+    /* Detach the next address so that
+     * cleanup can work.
+     */
+    rc = AcrSetSockaddr(env, na, sa->next);
+    if (rc == 0)
+        sa->next = 0;
+    return rc;
+}