view src/myVncClient/IpV6.java @ 103:0c0da17b72b2

add IpV6.java
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 25 Oct 2011 01:22:47 +0900
parents
children
line wrap: on
line source

package myVncClient;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.net.Inet6Address;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IpV6 {
	private HashMap<NetworkInterface, ArrayList<InetAddress>> interfaceMap;

	public IpV6() {
		interfaceMap = new HashMap<NetworkInterface, ArrayList<InetAddress>>();
	}

	public void getInterface() {
		interfaceMap.clear();
		try {
			Enumeration<NetworkInterface> iList = NetworkInterface
					.getNetworkInterfaces();
			if (iList == null) {
				System.out.println("notfound interface");
			} else {
				while (iList.hasMoreElements()) {
					NetworkInterface iface = iList.nextElement();
					Enumeration<InetAddress> addrList = iface
							.getInetAddresses();
					if (!addrList.hasMoreElements())
						continue;
					ArrayList<InetAddress> iaddress = new ArrayList<InetAddress>();
					while (addrList.hasMoreElements())
						iaddress.add(addrList.nextElement());
					interfaceMap.put(iface, iaddress);
				}
			}
		} catch (SocketException se) {
			System.out.println("Error getting network interfaces: "
					+ se.getMessage());
		}
	}

	public boolean match(String name) {
		Pattern pattern = Pattern.compile("lo.*");
		Matcher matcher = pattern.matcher(name);
		return matcher.matches();
	}

	public String getV6() {
		boolean flag = false;
		String name = null;
		for (NetworkInterface n : interfaceMap.keySet()) {
			if (!(match(n.getName()))) {
				for (InetAddress a : interfaceMap.get(n)) {
					if (a instanceof Inet6Address && !(flag)) {
						System.out.println(a.getHostAddress());
						flag = true;
						name = a.getHostAddress();
					}
				}
			}
		}
		return name;
	}

	public HashMap<NetworkInterface, ArrayList<InetAddress>> getInetfaceMap() {
		return interfaceMap;
	}

	public void setInterfaceMap(
			HashMap<NetworkInterface, ArrayList<InetAddress>> interfaceMap) {
		this.interfaceMap = interfaceMap;
	}

}