view src/main/java/alice/topology/manager/ComingServiceHosts.java @ 435:41bb86275910 dispose

send MD5 "mode -t"
author sugi
date Tue, 12 Aug 2014 07:37:17 +0900
parents e565d481c52e
children a9ed8e932a1e
line wrap: on
line source

package alice.topology.manager;

import java.util.HashMap;

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 ComingServiceHosts extends CodeSegment{

    private Receiver info = ids.create(CommandType.TAKE);
    private Receiver info1 = ids.create(CommandType.TAKE);
    private Receiver info2 = ids.create(CommandType.TAKE); // HashMap
    private Receiver info3 = ids.create(CommandType.TAKE); // MD5

    private int BINATY_TREE = 2;

    public ComingServiceHosts(){
        info.setKey("newHost");
        info1.setKey("hostCount");
        info2.setKey("nodeConnectionInfo");
        info3.setKey("MD5");
    }

    @Override
    public void run() {
        String cookie = info3.asString();
        System.out.println(cookie);
        HostMessage host = info.asClass(HostMessage.class);
        int comingHostCount = info1.asInteger();
        @SuppressWarnings("unchecked")
        HashMap<String, HostMessage> parentInfo = info2.asClass(HashMap.class);
        String nodeName = "node"+comingHostCount;
        // Manager connect to Node
        DataSegment.connect(nodeName, "", host.name, host.port);
        ods.put(nodeName, "host", nodeName);
        ods.put(nodeName, "cookie", cookie);
        
        ods.update(info1.key, comingHostCount+1);
        
        for (int i=1;i < BINATY_TREE+1; i++) {
         // child number is own number(comingHostCount) * 2 add one or two
            int num = BINATY_TREE * comingHostCount+i; 
            HostMessage newHost = new HostMessage(host.name, host.port, "parent", "child"+(i-1));
            newHost.absName = nodeName;
            newHost.remoteAbsName = "node"+num;
            parentInfo.put(newHost.remoteAbsName, newHost);
            
            ods.put("nodeInfo", newHost);
            ods.put(newHost.remoteAbsName, newHost);
            new RecodeTopology();
        }
        if (comingHostCount!=0) {
            HostMessage parent = parentInfo.get(nodeName);
            HostMessage newHost = new HostMessage(host.name, host.port, parent.reverseName, parent.connectionName);
            newHost.absName = nodeName;
            newHost.remoteAbsName = parent.absName;
            
            ods.put("nodeInfo", newHost);
            ods.put(parent.absName, newHost);
            new RecodeTopology();
        } 

        ods.put("nodeConnectionInfo", parentInfo);
        ods.put(nodeName, ValueFactory.createNilValue());
        if (comingHostCount==0)
            ods.put("start", ValueFactory.createNilValue());

        this.recycle();
    }

}