view src/main/java/alice/topology/manager/IncomingHosts.java @ 431:0239c1633012 dispose

add CodeSegment for creating MD5
author sugi
date Mon, 04 Aug 2014 08:40:28 +0900
parents c7c57f8d7538
children e565d481c52e
line wrap: on
line source

package alice.topology.manager;

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

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); //node name
    private Receiver messageList = ids.create(CommandType.TAKE); // HostMessage List
    private Receiver cookie = ids.create(CommandType.TAKE); // MD5

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

    @Override
    public void run() {
        HostMessage host = this.host.asClass(HostMessage.class);
        @SuppressWarnings("unchecked")
        LinkedList<HostMessage> messageList = this.messageList.asClass(LinkedList.class);
        boolean match = false;
        // check cookie
        if (host.cookie != null) {
            for (HostMessage hostMessage : messageList) {
                if (host.cookie.equals(hostMessage.cookie)) {
                    match = true;
                    System.out.println("cookie is match");
                    host.absName = hostMessage.absName;
                    ods.put("reconnect", host);
                    ods.put(this.messageList.key, messageList);
                    new SearchHostName();
                    break;
                }
            }
        }
        
        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 = this.cookie.asString();
            HostMessage record = new HostMessage();
            record.absName = nodeName;
            record.cookie = cookie;
            messageList.add(record);
            ods.put(this.messageList.key, messageList);
            
            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());
                }
            }
        }
        ods.put("orderHash", "order");
        new IncomingHosts(topology, nodeNames);
    }
}