view src/main/java/alice/topology/manager/IncomingHosts.java @ 345:8f71c3e6f11d

Change directory structure Maven standard
author sugi
date Wed, 16 Apr 2014 18:26:07 +0900
parents
children ffaacab84d1a
line wrap: on
line source

package alice.topology.manager;

import java.util.ArrayList;
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;
	Receiver host = ids.create(CommandType.TAKE);
	Receiver connection = ids.create(CommandType.TAKE);
	
	public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology, LinkedList<String> nodeNames) {
		this.topology = topology;
		this.nodeNames = nodeNames;
	}

	@Override
	public void run() {
		HostMessage host = this.host.asClass(HostMessage.class);
		@SuppressWarnings("unchecked")
		HashMap<String, ArrayList<HostMessage>> connectionList = this.connection.asClass(HashMap.class);
		
		String nodeName = nodeNames.poll();
		// Manager connect to Node
		
		DataSegment.connect(nodeName, "", host.name, host.port, host.reconnectFlag);
		ods.put(nodeName, "host", nodeName);
		LinkedList<NodeInfo> nodes = topology.get(nodeName);
		ArrayList<HostMessage> list;
		for (NodeInfo nodeInfo : nodes) {
			HostMessage newHost = new HostMessage(host.name, host.port, nodeInfo.connectionName, nodeInfo.reverseName);
			ods.put("local", nodeInfo.sourceNodeName, newHost);
			
			if (connectionList.containsKey(nodeInfo.sourceNodeName)){
				list = connectionList.get(nodeInfo.sourceNodeName);
			} else {
				list = new ArrayList<HostMessage>();
				connectionList.put(nodeInfo.sourceNodeName, list);
				
			}			
			list.add(newHost);
		}
		ods.update("local", "connection", connectionList);
		
		if (nodeNames.isEmpty()) {
			// configuration finish
			for (String key : topology.keySet()) {
				ods.put("local", key, ValueFactory.createNilValue());
			}
		} else {
			IncomingHosts cs = new IncomingHosts(topology, nodeNames);
			cs.host.setKey("local", "host");
			cs.connection.setKey("local", "connection");
		}
	}

}