diff src/alice/topology/node/StartTopologyNode.java @ 175:d7816b9b72e9 working

minor change
author e095732
date Thu, 24 Jan 2013 21:23:16 +0900
parents 23d6a775a643
children 52a1fa5ba38b
line wrap: on
line diff
--- a/src/alice/topology/node/StartTopologyNode.java	Mon Jan 14 19:22:36 2013 +0900
+++ b/src/alice/topology/node/StartTopologyNode.java	Thu Jan 24 21:23:16 2013 +0900
@@ -1,7 +1,9 @@
 package alice.topology.node;
 
 import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
 
 import alice.codesegment.CodeSegment;
 import alice.datasegment.DataSegment;
@@ -21,8 +23,12 @@
 	public void run() {
 		DataSegment.connect("manager", "", conf.managerHostName, conf.managerPort);
 		String localHostName = null;
-            // nocalHostName = InetAddress.getLocalHost().getHostName();
-            localHostName = "192.168.128.41";
+		//localHostName = InetAddress.getLocalHost().getHostName();
+		try {
+			localHostName = getIPAddress();
+		} catch (SocketException e) {
+			e.printStackTrace();
+		}
 		HostMessage host = new HostMessage(localHostName, conf.localPort);
 		ods.put("manager", "host", host);
 		
@@ -39,4 +45,21 @@
 		cs3.configNodeNum.setKey("local", "configNodeNum");
 	}
 
+	private String getIPAddress() throws SocketException {
+		Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+
+		while(interfaces.hasMoreElements()){
+			NetworkInterface network = interfaces.nextElement();
+			Enumeration<InetAddress> addresses = network.getInetAddresses();
+
+			while(addresses.hasMoreElements()){
+				String address = addresses.nextElement().getHostAddress();
+				if(!"127.0.0.1".equals(address) && !"0.0.0.0".equals(address)){
+					return address;
+				}
+			}
+		}
+		return "127.0.0.1";
+	}
+
 }