classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Condense VMNetworkInterface.getInterfaces() result


From: Mark Wielaard
Subject: [cp-patches] FYI: Condense VMNetworkInterface.getInterfaces() result
Date: Sun, 07 Aug 2005 23:43:50 +0200

Hi,

This fixes a bug that Ito fixed a long time ago for kaffe.
(Found in bugzilla by Andrew)

2005-08-07  Ito Kazumitsu <address@hidden>

       Fixes bug #22929
       * libraries/javalib/java/net/NetworkInterface.java
       (condense): New static private method.
       (getNetworkInterfaces): Call condense().
       (getByName, getByInetAddress): Call getNetworkInterfaces()
       so that condensed result may be returned.

       * vm/reference/java/net/VMNetworkInterface.java (getInterfaces):
       Clarify return value in documentation.

For GNU Classpath this doesn't really work yet since our native
VMNetworkInterface.getInterfaces() isn't implemented yet. We should
transcribe the CNI implementation from libgcj.

Committed,

Mark
Index: java/net/NetworkInterface.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/NetworkInterface.java,v
retrieving revision 1.15
diff -u -r1.15 NetworkInterface.java
--- java/net/NetworkInterface.java      2 Jul 2005 20:32:39 -0000       1.15
+++ java/net/NetworkInterface.java      7 Aug 2005 21:38:55 -0000
@@ -38,7 +38,12 @@
 
 package java.net;
 
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Vector;
 
 /**
@@ -143,9 +148,7 @@
   public static NetworkInterface getByName(String name)
     throws SocketException
   {
-    Vector networkInterfaces = VMNetworkInterface.getInterfaces();
-
-    for (Enumeration e = networkInterfaces.elements(); e.hasMoreElements();)
+    for (Enumeration e = getNetworkInterfaces(); e.hasMoreElements();)
       {
        NetworkInterface tmp = (NetworkInterface) e.nextElement();
 
@@ -170,9 +173,7 @@
   public static NetworkInterface getByInetAddress(InetAddress addr)
     throws SocketException
   {
-    Vector networkInterfaces = VMNetworkInterface.getInterfaces();
-
-    for (Enumeration interfaces = networkInterfaces.elements();
+    for (Enumeration interfaces = getNetworkInterfaces();
          interfaces.hasMoreElements();)
       {
        NetworkInterface tmp = (NetworkInterface) interfaces.nextElement();
@@ -188,6 +189,41 @@
     throw new SocketException("no network interface is bound to such an IP 
address");
   }
 
+  static private Collection condense(Collection interfaces) 
+  {
+    final Map condensed = new HashMap();
+
+    final Iterator interfs = interfaces.iterator();
+    while (interfs.hasNext()) {
+
+      final NetworkInterface face = (NetworkInterface) interfs.next();
+      final String name = face.getName();
+      
+      if (condensed.containsKey(name))
+       {
+         final NetworkInterface conface = (NetworkInterface) 
condensed.get(name);
+         if (!conface.inetAddresses.containsAll(face.inetAddresses))
+           {
+             final Iterator faceAddresses = face.inetAddresses.iterator();
+             while (faceAddresses.hasNext())
+               {
+                 final InetAddress faceAddress = (InetAddress) 
faceAddresses.next();
+                 if (!conface.inetAddresses.contains(faceAddress))
+                   {
+                     conface.inetAddresses.add(faceAddress);
+                   }
+               }
+           }
+       }
+      else
+       {
+         condensed.put(name, face);
+       }
+    }
+
+    return condensed.values();
+  }
+
   /**
    * Return an <code>Enumeration</code> of all available network interfaces
    *
@@ -202,7 +238,9 @@
     if (networkInterfaces.isEmpty())
       return null;
 
-    return networkInterfaces.elements();
+    Collection condensed = condense(networkInterfaces);
+
+    return Collections.enumeration(condensed);
   }
 
   /**
Index: vm/reference/java/net/VMNetworkInterface.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/vm/reference/java/net/VMNetworkInterface.java,v
retrieving revision 1.2
diff -u -r1.2 VMNetworkInterface.java
--- vm/reference/java/net/VMNetworkInterface.java       2 Jul 2005 20:33:08 
-0000       1.2
+++ vm/reference/java/net/VMNetworkInterface.java       7 Aug 2005 21:38:55 
-0000
@@ -61,6 +61,16 @@
        System.loadLibrary("javanet");
     }
 
+  /**
+   * Returns a Vector of InetAddresses. The returned value will be
+   * 'condensed', meaning that all elements with the same interface
+   * name will be collapesed into one InetAddress for that name
+   * containing all addresses before the returning the result to the
+   * user. This means the native method can be implemented in a naive
+   * way mapping each address/interface to a name even if that means
+   * that the Vector contains multiple InetAddresses with the same
+   * interface name.
+   */
   public static native Vector getInterfaces()
     throws SocketException;
 }

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]