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

work but have bug
author sugi
date Tue, 22 Jul 2014 18:44:18 +0900
parents 93995b7a9a05
children e565d481c52e
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{

    Receiver info = ids.create(CommandType.TAKE);
    Receiver info1 = ids.create(CommandType.TAKE);
    Receiver info2 = ids.create(CommandType.TAKE); // HashMap
    int BINATY_TREE = 2;

    public ComingServiceHosts(){
        info.setKey("host");
        info1.setKey("hostCount");
        info2.setKey("nodeConnectionInfo");
    }

    @Override
    public void run() {
        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.update(info1.key, comingHostCount+1);

        for (int i=1;i < BINATY_TREE+1; i++) {
            int num = 2 * comingHostCount+i; 
            HostMessage newHost = new HostMessage(host.name, host.port, "parent", "child"+(i-1));
            newHost.remoteAbsName = nodeName;
            newHost.absName = "node"+num;
            parentInfo.put(newHost.absName, newHost);
            ods.put(newHost.absName, newHost);
        }
        if (comingHostCount!=0) {
            HostMessage parent = parentInfo.get(nodeName);
            HostMessage newHost = new HostMessage(host.name, host.port, parent.reverseName, parent.connectionName);		
            ods.put(parent.remoteAbsName, newHost);
        } 

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

        this.recycle();
    }

}