view src/alice/test/topology/fish/WidthReceiver.java @ 216:21bd8af1cf26 working

change asClass method
author one
date Wed, 27 Mar 2013 19:38:33 +0900
parents fec0726bb126
children b5c642ba998e
line wrap: on
line source

package alice.test.topology.fish;

import java.util.Collections;
import java.util.Comparator;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;

public class WidthReceiver extends CodeSegment {
	
	public Receiver widths = ids.create(CommandType.TAKE);
	public Receiver routing = ids.create(CommandType.PEEK);	
	
	@Override
	public void run() {
		int width = this.widths.asInteger(this.widths);
		String from = this.widths.from;
		RoutingTable routing = this.routing.asClass(RoutingTable.class);
		Routing newRouting = new Routing(from, width);
		boolean update = false;
		for (Routing r : routing.table) {
			if (r.id == newRouting.id) {
				routing.sumWidth -= r.width;
				routing.sumWidth += newRouting.width;
				r.width = width;
				update = true;
				break;
			}
		}
		if (!update) {
			routing.table.add(newRouting);
			Collections.sort(routing.table, new Comparator<Routing>() {
				@Override
				public int compare(Routing o1, Routing o2) {
					return o1.id - o2.id;
				}
			});
			routing.sumWidth += width;
		}
		
		System.out.println("SUM WIDTH: " + routing.sumWidth);
		
		System.out.println("NODE NUM: " + routing.table.size());
		
		ods.update("local", "width", routing.sumWidth);
		ods.update("local", "routing", routing);
		
		WidthReceiver cs = new WidthReceiver();
		cs.widths.setKey("local", "widths", this.widths.index);
		cs.routing.setKey("local", "routing");
		//cs.routing.setKey("local", "routing", this.routing.index);
	}

}