view src/main/java/suikwasha/distributedalgorithm/topologies/RingTopologyBuilder.java @ 0:38a110b13db1

added SimpleDistributedAlgorithmFramework. added NaiveAlgorithm added ChangRobertsAlgorithm added PertersonAlgorithm
author suikwasha
date Fri, 19 Oct 2012 00:05:41 +0900
parents
children
line wrap: on
line source

package suikwasha.distributedalgorithm.topologies;

import fj.data.List;
import suikwasha.distributedalgorithm.framework.Algorithm;
import suikwasha.distributedalgorithm.framework.Link;
import suikwasha.distributedalgorithm.framework.LinkBuilder;
import suikwasha.distributedalgorithm.framework.Machine;
import suikwasha.distributedalgorithm.framework.MachineBuilder;
import suikwasha.distributedalgorithm.framework.Topology;
import suikwasha.distributedalgorithm.framework.TopologyBuilder;

public class RingTopologyBuilder implements TopologyBuilder
{
	public Topology build(Iterable<Algorithm> _algos,MachineBuilder _machineBuilder,LinkBuilder _linkBuilder)
	{
		List<Machine> list = List.nil();
		
		// first , create machines
		
		for(Algorithm algo : _algos){
			Machine machine = _machineBuilder.createMachine(algo);
			list = list.snoc(machine);
		}
		
		if(list.length() < 1){
			throw new IllegalArgumentException("list.length() < 1");
		}
		
		// create links
		
		Link firstLink = _linkBuilder.build();
		Machine head = list.head();
		head.addPort(firstLink.getPort2());
		
		List<Machine> tail = list.tail();
		Machine prevMachine = head;
		if(tail.length() != 0){
			for(Machine machine : tail){
				Link link = _linkBuilder.build();
				prevMachine.addPort(link.getPort1());
				machine.addPort(link.getPort2());
				prevMachine = machine;
			}
			Machine last = tail.last();
			last.addPort(firstLink.getPort1());
		}else{
			head.addPort(firstLink.getPort1());
		}
		
		return new RingTopology(list);
	}
	
	private class RingTopology implements Topology
	{
		private final List<Machine> machines;
		
		public RingTopology(List<Machine> _machines)
		{
			machines = _machines;
		}
		
		public Iterable<Machine> getMachines()
		{
			return machines;
		}

		public long getMachineCount()
		{
			return machines.length();
		}
	}
}