changeset 337:c455b05f163b

in decideWhereToConnect, host on a different network isn't add to nodeList.
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Fri, 06 Feb 2015 16:14:46 +0900
parents 2aa4fcbc5ba2
children 931e1abda61d
files src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java
diffstat 1 files changed, 42 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java	Fri Feb 06 15:11:49 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java	Fri Feb 06 16:14:46 2015 +0900
@@ -3,6 +3,7 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.LinkedList;
 
 public class TreeManagement {
@@ -85,7 +86,11 @@
         me.setHostName(myHostName);
         if (deadParent.getTreeNum() == 0) {
             // if dead root, connect me.
-            connectTo(me, lostParentNode);
+            try {
+                connectTo(me, lostParentNode);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
             return;
         }
         if (getChildNode(deadParent, 0) == null) return;
@@ -133,19 +138,25 @@
         TreeVNCNode newNode = nodeList.get(deadNode.getTreeNum());
         TreeVNCNode parentNode = getParentNode(newNode.getTreeNum());
         // new node connect to parent node.
-        connectTo(parentNode, newNode);
+        try {
+            connectTo(parentNode, newNode);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
         // if children node exist, children connect to new node.
         for(int i=0; i < treebranch; i++) {
             TreeVNCNode child = getChildNode(newNode, i);
             if (child != null) {
-                connectTo(newNode, child);
+                try {
+                    connectTo(newNode, child);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
         }
     }
 
-
-
-    public void connectTo(TreeVNCNode newparent, TreeVNCNode n) {
+    public void connectTo(TreeVNCNode newparent, TreeVNCNode n) throws IOException {
         TreeVncProtocol vc1 = new TreeVncProtocol(n.getHostname(),n.getPort());
             short nodeId = (short) n.getTreeNum();
             vc1.connectTo(newparent.getHostname(), newparent.getPort(), isLeader(n), nodeId);
@@ -171,16 +182,37 @@
     public void decideWhereToConnect(String hostname, int port, String localhostname) {
         TreeVNCNode node = new TreeVNCNode(hostname,port,localhostname);
         node.setTreeNum(nodeList.size());
-        nodeList.add(node);
+
+        InetAddress ipAddress = null;
+        try {
+            ipAddress = InetAddress.getByName(hostname);
+        } catch (UnknownHostException e) {
+            System.out.println("root : whereToConnect : cannot get ipAddress" + hostname);
+            return;
+        }
+        if (onTheSameNetwork(ipAddress))
+            nodeList.add(node);
         if (nodeList.size() >= treebranch + 1) {
             TreeVNCNode parent = getParentNode(node.getTreeNum());
             checkParameter(parent.getTreeNum(), nodeList.size(), isLeader(node));
-            connectTo(parent, node);
+            try {
+                connectTo(parent, node);
+            } catch (IOException e) {
+                nodeList.remove(node);
+                System.out.println("root : whereToConnect : Connection Faild" + hostname);
+                return;
+            }
         } else {
-            // connect to root
+            // connect to me
             TreeVNCNode me = nodeList.getFirst();
             me.setHostName(localhostname);
-            connectTo(me, node);
+            try {
+                connectTo(me, node);
+            } catch (IOException e) {
+                nodeList.remove(node);
+                System.out.println("root : whereToConnect : Connection Faild" + hostname);
+                return;
+            }
         }
         if (showTreeNode) {
             showTreeNode();