view src/main/java/alice/topology/manager/StartTopologyManager.java @ 602:8a9fd716c335 dispose

change topology class extends CodeSegment from MetaCodeSegment, Chat minor fix
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Tue, 03 May 2016 20:09:45 +0900
parents 30f2c04571c2
children
line wrap: on
line source

package alice.topology.manager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import alice.codesegment.MetaCodeSegment;
import org.apache.log4j.Logger;

import alice.codesegment.CodeSegment;
import alice.topology.HostMessage;
import alice.topology.fix.ReceiveDisconnectMessage;

import com.alexmerz.graphviz.ParseException;
import com.alexmerz.graphviz.Parser;
import com.alexmerz.graphviz.objects.Edge;
import com.alexmerz.graphviz.objects.Graph;
import com.alexmerz.graphviz.objects.Node;

public class StartTopologyManager extends CodeSegment {

    TopologyManagerConfig conf;
    Logger logger = Logger.getLogger(StartTopologyManager.class);

    public StartTopologyManager(TopologyManagerConfig conf) {
        this.conf = conf;
    }

    @Override
    public void run() {
        new CheckComingHost();
        ods.put("absCookieTable", new HashMap<String, String>());

        if (!conf.dynamic) {
            LinkedList<String> nodeNames = new LinkedList<String>();
            HashMap<String, LinkedList<NodeInfo>> topology = new HashMap<String, LinkedList<NodeInfo>>();
            int nodeNum = 0;
            try {
                FileReader reader = new FileReader(new File(conf.confFilePath));
                Parser parser = new Parser();
                parser.parse(reader);
                ArrayList<Graph> graphs = parser.getGraphs();
                for (Graph graph : graphs) {
                    ArrayList<Node> nodes = graph.getNodes(false);
                    nodeNum = nodes.size();
                    for (Node node : nodes) {
                        String nodeName = node.getId().getId();
                        nodeNames.add(nodeName);
                        topology.put(nodeName, new LinkedList<NodeInfo>());
                    }
                    ArrayList<Edge> edges = graph.getEdges();
                    HashMap<String, NodeInfo> hash = new HashMap<String, NodeInfo>();
                    for (Edge edge : edges) {
                        String connection = edge.getAttribute("label");
                        String source = edge.getSource().getNode().getId().getId();
                        String target = edge.getTarget().getNode().getId().getId();
                        LinkedList<NodeInfo> sources = topology.get(target);
                        NodeInfo nodeInfo = new NodeInfo(source, connection);
                        sources.add(nodeInfo);
                        hash.put(source + "," + target, nodeInfo);
                    }
                    for (Edge edge : edges) {
                        String connection = edge.getAttribute("label");
                        String source = edge.getSource().getNode().getId().getId();
                        String target = edge.getTarget().getNode().getId().getId();
                        NodeInfo nodeInfo = hash.get(target + "," + source);
                        if (nodeInfo != null) {
                            nodeInfo.reverseName = connection;
                        }
                    }
                }

            } catch (FileNotFoundException e) {
                logger.error("File not found: " + conf.confFilePath);
                e.printStackTrace();
            } catch (ParseException e) {
                logger.error("File format error: " + conf.confFilePath);
                e.printStackTrace();
            }

            // for recode topology information
            // cookie List
            ods.put("running", false);
            ods.put("resultParse", topology);
            ods.put("nodeNames", nodeNames);

            new IncomingHosts();

            ConfigWaiter cs3 = new ConfigWaiter(nodeNum);
            cs3.done.setKey("local", "done");

        } else {
            ods.put("running", true);

            HashMap<String, HostMessage> nameTable = new HashMap<String, HostMessage>();
            int cominghostCount = 0;
            if (conf.type == TopologyType.Tree) {
                ParentManager manager = new ParentManager(conf.hasChild);
                ods.put("parentManager", manager);
                ods.put("nameTable", nameTable);
                ods.put("hostCount", cominghostCount);
                new ComingServiceHosts();
                new ReceiveDisconnectMessage();
            } else if (conf.type == TopologyType.Star){
                ods.put("nameTable", nameTable);
                ods.put("hostCount", cominghostCount);
                new ComingStarHosts();
            }
        }

        ods.put("topology", new HashMap<String, LinkedList<HostMessage>>());
        ods.put("createdList", new LinkedList<String>());
        new CreateHash();

        TopologyFinish cs2 = new TopologyFinish();
        cs2.finish.setKey("local", "finish");
    }

}