diff src/main/java/alice/topology/fix/FixTopology.java @ 482:8227a2fd5a55 dispose

bug fix
author sugi
date Fri, 05 Dec 2014 17:22:33 +0900
parents cf345b10a21a
children c00437e91db0
line wrap: on
line diff
--- a/src/main/java/alice/topology/fix/FixTopology.java	Fri Dec 05 17:22:11 2014 +0900
+++ b/src/main/java/alice/topology/fix/FixTopology.java	Fri Dec 05 17:22:33 2014 +0900
@@ -29,58 +29,70 @@
     @SuppressWarnings("unchecked")
     @Override
     public void run() {
-        ConnectionInfo disconnect = info.asClass(ConnectionInfo.class); // send Data is wrong.
+        ConnectionInfo disconnect = info.asClass(ConnectionInfo.class);
         HashMap<String, HostMessage> nameTable = info3.asClass(HashMap.class);
         ParentManager manager = info4.asClass(ParentManager.class);
-        HostMessage disconnectNode = nameTable.get(disconnect.nodeName);
         HashMap<String, LinkedList<HostMessage>> topology = info1.asClass(HashMap.class);
+        HostMessage disconnectNode = null;
+        String disconnectNodeName = null;
+        for (String name : nameTable.keySet()) {
+            disconnectNode = nameTable.get(name);
+            if ((disconnectNode.name.equals(disconnect.addr)||disconnectNode.name.equals(disconnect.hostname))
+                    && disconnectNode.port == disconnect.port) {
+                disconnectNodeName = name;
+                break;
+            }
+            disconnectNode = null;
+        }
 
-        if (disconnectNode.isAlive()) {
+        if (disconnectNode!=null && disconnectNode.isAlive()) {
             // change state not Alive
             disconnectNode.alive = false;
             // get lastJoinedNode
             int last = info2.asInteger()-1;
-            String lastJoinedNode = "node"+last;
-            LinkedList<HostMessage> connectionList = topology.get(lastJoinedNode);
+            String lastJoinNodeName = "node"+last;
+            LinkedList<HostMessage> connectionList = topology.get(lastJoinNodeName);
             // lastJoinedNode has one connection
-            HostMessage parentInfo = connectionList.poll();
+            HostMessage parentNode = connectionList.poll();
 
-            for (HostMessage node1 : topology.get(parentInfo.absName)) {
-                if (node1.remoteAbsName.equals(lastJoinedNode)) {
-                    list.remove(node1);
+            connectionList = topology.get(parentNode.absName);
+            for (HostMessage node1 : connectionList) {
+                if (node1.absName.equals(lastJoinNodeName)) {
+                    connectionList.remove(node1);
                     break;
                 }
             }
 
-            if (!lastJoinedNode.equals(disconnect.nodeName)) {
+            if (!lastJoinNodeName.equals(disconnectNodeName)) {
                 // send close message to lastJoinedNode
-                ods.put(lastJoinedNode, "_CLOSEMESSEAGE", parentInfo.reverseName);
+                ods.put(lastJoinNodeName, "_CLOSEMESSEAGE", parentNode.reverseName);
                 // send close message to lastJoinedNode's parent
-                ods.put(parentInfo.absName, "_CLOSEMESSEAGE", parentInfo.connectionName);
+                ods.put(parentNode.absName, "_CLOSEMESSEAGE", parentNode.connectionName);
 
-                HostMessage lastJoined = nameTable.get(lastJoinedNode);
-                LinkedList<HostMessage> disconnectionList = topology.get(disconnect.nodeName);
+                HostMessage lastJoinNode = nameTable.get(lastJoinNodeName);
+                connectionList = topology.get(lastJoinNodeName);
+                LinkedList<HostMessage> disconnectionList = topology.get(disconnectNodeName);
                 // remove NodeInformation from disconnection node's children and parent
                 for (HostMessage node :disconnectionList) {
                     LinkedList<HostMessage> list = topology.get(node.absName);
                     for (HostMessage node1 : list) {
-                        if (node1.absName.equals(disconnect.nodeName)) {
-                            node1.name = lastJoined.name;
-                            node1.port = lastJoined.port;
-                            node1.absName = lastJoinedNode;
+                        if (node1.absName.equals(disconnectNodeName)) {
+                            node1.name = lastJoinNode.name;
+                            node1.port = lastJoinNode.port;
+                            node1.absName = lastJoinNodeName;
+                            ods.put(node1.remoteAbsName, node1);
                             break;
                         }
-                        ods.put(node1.remoteAbsName, node1);
                     }
-                    node.absName = lastJoinedNode;
-                    ods.put(lastJoinedNode, node);
+                    node.remoteAbsName = lastJoinNodeName;
+                    ods.put(node.remoteAbsName, node);
                     connectionList.add(node);
                 }
-
-                manager.replaceAndRemove(disconnect.nodeName, lastJoinedNode);
+                disconnectionList.clear();
+                manager.replaceAndRemove(disconnectNodeName, lastJoinNodeName);
             } else {
                 // disconnection node is lastJoinedNode
-                manager.remove(disconnect.nodeName);
+                manager.remove(disconnectNodeName);
             }
         } else {
             // disconnect message already received.
@@ -88,12 +100,15 @@
 
         // need debug option
         for (LinkedList<HostMessage> list :topology.values()){
-            System.out.print(list.get(0).remoteAbsName+" : ");
-            for (HostMessage host : list){
-                System.out.print("[ "+host.absName+" "+host.name+" "+host.port+" "+host.connectionName+" "+host.reverseName+" "+host.remoteAbsName+" ]");
+            if (list.size() !=0) {
+                System.out.print(list.get(0).remoteAbsName+" : ");
+                for (HostMessage host : list){
+                    System.out.print("[ "+host.absName+" "+host.name+" "+host.port+" "+host.connectionName+" "+host.reverseName+" "+host.remoteAbsName+" ]");
+                }
+                System.out.println();
             }
-            System.out.println();
         }
+
         ods.put(info1.key, topology);
         ods.put(info2.key, info2.getReceiveData());
         ods.put(info3.key, nameTable);