view src/main/java/alice/topology/manager/SearchHostName.java @ 432:59e84cd75403 dispose

SearchHostName has bug
author sugi
date Mon, 04 Aug 2014 20:34:41 +0900
parents 1b32ea1263f3
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 SearchHostName extends CodeSegment {

    private Receiver info = ids.create(CommandType.TAKE);  // reconnect NodeInfo
    private Receiver info1 = ids.create(CommandType.TAKE); // topology recode (HashMap)
    private Receiver info2 = ids.create(CommandType.PEEK); // check App running 

    public SearchHostName(){
        info.setKey("reconnect");
        info1.setKey("topology");
        info2.setKey("running");
    }

    @Override
    public void run() {
        HostMessage hostInfo = info.asClass(HostMessage.class);
        boolean running = info2.asClass(boolean.class);
        @SuppressWarnings("unchecked")
        HashMap<String, LinkedList<HostMessage>> topology = info1.asClass(HashMap.class);

        DataSegment.remove(hostInfo.absName);
        DataSegment.connect(hostInfo.absName, "", hostInfo.name, hostInfo.port);
        ods.put(hostInfo.absName, "host", hostInfo.absName);
        
        // put Host dataSegment on reconnect node 
        if (topology.containsKey(hostInfo.absName)) {
            LinkedList<HostMessage> clist = topology.get(hostInfo.absName);
            ods.put(hostInfo.absName, "dummy");
            if (running){
                ods.put(hostInfo.absName, ValueFactory.createNilValue());
            }
            for (HostMessage node : clist){
                ods.put("local" ,hostInfo.absName, node);
                System.out.println("put data in "+ hostInfo.absName);
            }
        }

        // update topology information
        for (LinkedList<HostMessage> list :topology.values()){
            for (HostMessage host : list){

                // find and update old info 
                if (hostInfo.absName.equals(host.absName)){ 
                    if (!hostInfo.name.equals(host.name) || (hostInfo.port != host.port)){ 
                        host.name = hostInfo.name;
                        host.port = hostInfo.port;
                        
                        ods.put(host.remoteAbsName, host);
                        
                    } else {
                        // nothing to do ?
                    }
                }
            }
        }
        
        for (LinkedList<HostMessage> list :topology.values()){
            System.out.print(list.get(0).remoteAbsName+" : ");
            for (HostMessage host : list){
                System.out.print("[ "+host.absName+" "+host.name+" "+host.port+" "+host.connectionName+" "+host.reverseName+" "+host.remoteAbsName+" ]");
            }
            System.out.println();
        }
        ods.put("topology", topology);
    }

}