view src/main/java/alice/topology/fix/FixTopology.java @ 476:8968b24c1ce3 dispose

TODO is to fix ParentManager information
author sugi
date Tue, 02 Dec 2014 13:43:07 +0900
parents 6a71732cd3ee
children f644dc9e0589
line wrap: on
line source

package alice.topology.fix;

import java.util.HashMap;
import java.util.LinkedList;

import alice.codesegment.CodeSegment;
import alice.daemon.ConnectionInfo;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;
import alice.topology.HostMessage;
import alice.topology.manager.ParentManager;

public class FixTopology extends CodeSegment {

    private Receiver info = ids.create(CommandType.TAKE); // disconnection nodeInfo
    private Receiver info1 = ids.create(CommandType.TAKE); // all connection Info
    private Receiver info2 = ids.create(CommandType.TAKE); // node count
    private Receiver info3 = ids.create(CommandType.TAKE); // IP Table
    private Receiver info4 = ids.create(CommandType.TAKE); // parentManager


    public FixTopology() {
        info.setKey("_DISCONNECT");
        info1.setKey("topology");
        info2.setKey("hostCount");
        info3.setKey("nameTable");
        info4.setKey("parentManager");
    }

    @SuppressWarnings("unchecked")
    @Override
    public void run() {
        ConnectionInfo disconnect = info.asClass(ConnectionInfo.class);
        HashMap<String, HostMessage> nameTable = info3.asClass(HashMap.class);
        HostMessage disconnectNode = nameTable.get(disconnect.nodeName);
        if (disconnectNode.isAlive()) {
            // change state not Alive
            disconnectNode.alive = false;
            // get lastJoinedNode
            int last = info2.asInteger()-1;
            String lastJoinedNode = "node"+last;
            if (!lastJoinedNode.equals(disconnect.nodeName)) {
                HashMap<String, LinkedList<HostMessage>> topology = info1.asClass(HashMap.class);

                LinkedList<HostMessage> connectionList = topology.get(lastJoinedNode);
                // lastJoinedNode has one connection
                HostMessage parentInfo = connectionList.poll();
                // send close message to lastJoinedNode
                ods.put(lastJoinedNode, "_CLOSEMESSEAGE", parentInfo.reverseName);

                // send close message to lastJoinedNode's parent
                ods.put(parentInfo.absName, "_CLOSEMESSEAGE", parentInfo.connectionName);
                for (HostMessage node1 : topology.get(parentInfo.absName)) {
                    if (node1.remoteAbsName.equals(lastJoinedNode)) {
                        list.remove(node1);
                        break;
                    }
                }

                HostMessage lastJoined = nameTable.get(lastJoinedNode);
                LinkedList<HostMessage> disconnectionList = topology.get(disconnect.nodeName);
                // 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;
                            break;
                        }
                        ods.put(node1.remoteAbsName, node1);
                    }
                    node.absName = lastJoinedNode;
                    ods.put(lastJoinedNode, node);
                    connectionList.add(node);
                }
            } else {
                // disconnection node is lastJoinedNode
            }
        } else {
            // disconnect message already received.
        }
    }
}