view src/alice/codesegment/InputDataSegment.java @ 215:1fd7067f0044 working

remove conversion flag
author one
date Wed, 27 Mar 2013 18:35:43 +0900
parents b5daccf36104
children d50cddf64396
line wrap: on
line source

package alice.codesegment;

import java.util.concurrent.atomic.AtomicInteger;

import alice.datasegment.Command;
import alice.datasegment.CommandType;
import alice.datasegment.DataSegment;
import alice.datasegment.ReceiveRemoteData;
import alice.datasegment.Receiver;

/**
 * InputDataSegment Manager
 * 			keep tracking unbound/bound count
 * @author kazz
 *
 */
public class InputDataSegment {
	
	public CodeSegment cs;
	private AtomicInteger count = new AtomicInteger(1); // 1 for no input data segments
	private AtomicInteger keyCount = new AtomicInteger(0); // number of DataSegments
	
	public InputDataSegment(CodeSegment cs) {
		this.cs = cs;
	}
	
	public void peek(Receiver receiver, String managerKey, String key) {
		peek(receiver, managerKey, key, 0);
	}
	
	public void peek(Receiver receiver, String managerKey, String key, int index) {
		DataSegment.get(managerKey).peek(receiver, key, index, cs);
	}

	public void peek(Receiver receiver, String key) {
		peek(receiver, key, 0);
	}
	
	public void peek(Receiver receiver, String key, int index) {
		DataSegment.getLocal().peek(receiver, key, index, cs);
	}

	public void take(Receiver receiver, String managerKey, String key) {
		take(receiver, managerKey, key, 0);
	}
	
	public void take(Receiver receiver, String managerKey, String key, int index) {
		DataSegment.get(managerKey).take(receiver, key, index, cs);
	}

	public void take(Receiver receiver, String key) {
		take(receiver, key, 0);
	}
	
	public void take(Receiver receiver, String key, int index) {
		DataSegment.getLocal().take(receiver, key, index, cs);
	}

	public void reply(Receiver receiver, Command reply) {
		receiver.index = reply.index;
		receiver.from = reply.reverseKey;
		if (!reply.reverseKey.equals("local")) {
			receiver.setData(new ReceiveRemoteData(reply.val));
		} else {
			receiver.setData(new ReceiveLocalData(reply.obj));
		}
		receive();
	}

	public void regist() {
		count.getAndIncrement();
		keyCount.getAndIncrement();
	}
	
	public void setKey() {
		if (keyCount.decrementAndGet() == 0) {
			receive();
		}
	}
	
	public void receive() {
		if (count.decrementAndGet() == 0) {
			CodeSegmentManager.submit(cs);
		}
	}
	
	/**
	 * InputDataSegment factory
	 * @param type PEEK or TAKE
	 * @return Receiver of DataSegment reply 
	 */
	public Receiver create(CommandType type) {
		return new Receiver(this, type);
	}
}