[jetrix-cvs] jetrix/src/java/net/jetrix/tools ServerDirectory.java,1.3,1.4
Brought to you by:
smanux
From: Emmanuel B. <sm...@us...> - 2005-06-08 17:05:23
|
Update of /cvsroot/jetrix/jetrix/src/java/net/jetrix/tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19745/src/java/net/jetrix/tools Modified Files: ServerDirectory.java Log Message: Changed getServers() to return distinct servers only Index: ServerDirectory.java =================================================================== RCS file: /cvsroot/jetrix/jetrix/src/java/net/jetrix/tools/ServerDirectory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ServerDirectory.java 23 May 2005 23:10:55 -0000 1.3 --- ServerDirectory.java 8 Jun 2005 17:05:14 -0000 1.4 *************** *** 25,30 **** import java.net.URL; import java.net.MalformedURLException; ! import java.util.Collection; ! import java.util.TreeSet; /** --- 25,31 ---- import java.net.URL; import java.net.MalformedURLException; ! import java.net.InetAddress; ! import java.net.UnknownHostException; ! import java.util.*; /** *************** *** 55,59 **** } ! return servers; } --- 56,60 ---- } ! return getDistinctServers(servers); } *************** *** 108,110 **** --- 109,145 ---- return servers; } + + /** + * Return the distinct hosts from the specified list of servers. + * Host names (i.e tetrinet.fr) are prefered over host addresses + * (i.e 194.117.194.68). + * + * @since 0.3 + */ + private static Set<String> getDistinctServers(Collection<String> servers) + { + Map<String, String> map = new HashMap<String, String>(); + + for (String server : servers) + { + try + { + // get the address of the server + InetAddress address = InetAddress.getByName(server); + + // check if the server is missing from the map, or registered under a numerical form + String hostname = map.get(address.getHostAddress()); + if (hostname == null || hostname.equals(address.getHostAddress())) + { + map.put(address.getHostAddress(), server); + } + } + catch (UnknownHostException e) + { + // ignored + } + } + + return new HashSet<String>(map.values()); + } } |