view src/main/java/alice/topology/manager/IncomingHosts.java @ 429:1b32ea1263f3 dispose

work but have bug
author sugi
date Tue, 22 Jul 2014 18:44:18 +0900
parents 93995b7a9a05
children c7c57f8d7538
line wrap: on
line source

package alice.topology.manager;

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

import org.apache.commons.lang3.RandomStringUtils;
import org.msgpack.type.ValueFactory;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.DataSegment;
import alice.datasegment.Receiver;
import alice.topology.HostMessage;

public class IncomingHosts extends CodeSegment {

    HashMap<String, LinkedList<NodeInfo>> topology;
    LinkedList<String> nodeNames;
    private Receiver host = ids.create(CommandType.TAKE);
    private Receiver nodes = ids.create(CommandType.TAKE);

    public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology,
            LinkedList<String> nodeNames) {
        this.topology = topology;
        this.nodeNames = nodeNames;
        this.host.setKey("host");
        this.nodes.setKey("nodes");
    }

    @Override
    public void run() {
        HostMessage host = this.host.asClass(HostMessage.class);
        boolean match = false;
        // check cookie
        if (host.cookie != null) {
            ;
            @SuppressWarnings("unchecked")
            LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
            for (HostMessage node : nodes) {
                if (host.cookie.equals(node.cookie)) {
                    match = true;
                    System.out.println("cookie is match");
                    host.absName = node.absName;
                    ods.put("reconnect", host);
                    ods.put(this.nodes.key, nodes);
                    new SearchHostName();
                }
            }
        }
        
        if (!match) {
            System.out.println("new node come");
            // not have or match cookie 
            String nodeName = nodeNames.poll();
            // Manager connect to Node
            DataSegment.connect(nodeName, "", host.name, host.port);
            ods.put(nodeName, "host", nodeName);

            String cookie = createCookie(nodeName);
            ods.put(nodeName, "cookie", cookie);

            LinkedList<NodeInfo> nodes = topology.get(nodeName);
            for (NodeInfo nodeInfo : nodes) {
                HostMessage newHost = new HostMessage(host.name, host.port,
                        nodeInfo.connectionName, nodeInfo.reverseName);
                newHost.absName = nodeName;
                newHost.remoteAbsName = nodeInfo.sourceNodeName;
                ods.put("nodeInfo", newHost);
                ods.put(nodeInfo.sourceNodeName, newHost);
                new RecodeTopology();
            }

            if (nodeNames.isEmpty()) {
                // configuration finish
                for (String key : topology.keySet()) {
                    ods.put("local", key, ValueFactory.createNilValue());
                }
            }
        }
        
        new IncomingHosts(topology, nodeNames);
    }

    private String createCookie(String hostName) {
        @SuppressWarnings("unchecked")
        LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
        boolean checkNewCookie = false;
        String str = null;
        while (!checkNewCookie){
            str = RandomStringUtils.randomAscii(10);
            int count = 0;
            for (HostMessage node : nodes) {
                if (str.equals(node.cookie)) break;
                count++;
            }
            if (count == nodes.size())
                checkNewCookie = true;
        }
        HostMessage table = new HostMessage();
        table.absName = hostName;
        table.cookie = str;
        nodes.add(table);
        ods.put(this.nodes.key, nodes);
        System.out.println(str);
        return str;
    }

}