view src/alice/codesegment/InputDataSegment.java @ 28:98ab26e09a98

Configuration Manager work and implements reverseKey
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 17 Jan 2012 08:41:34 +0900
parents e7867328a2fb
children 2bfb796b0fa1
line wrap: on
line source

package alice.codesegment;

import java.util.concurrent.atomic.AtomicInteger;

import alice.datasegment.DataSegment;
import alice.datasegment.DataSegmentReceiver;
import alice.datasegment.DataSegmentValue;

public class InputDataSegment {
	
	private CodeSegment cs;
	private AtomicInteger count = new AtomicInteger(1); // for no input data segments
	private AtomicInteger keyCount = new AtomicInteger(0); 
	
	public InputDataSegment(CodeSegment cs) {
		this.cs = cs;
	}
	
	public void peek(DataSegmentReceiver receiver, String managerKey, String key) {
		peek(receiver, managerKey, key, 0);
	}
	
	public void peek(DataSegmentReceiver receiver, String managerKey, String key, int index) {
		DataSegment.get(managerKey).peek(receiver, key, index, cs);
	}

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

	public void reply(DataSegmentReceiver receiver, DataSegmentValue val) {
		receiver.index = val.index;
		receiver.val = val.val;
		receiver.from = val.from;
		receive();
	}

	public void regist() {
		count.getAndIncrement();
		keyCount.getAndIncrement();
	}
	
	public void setKey() {
		if (keyCount.decrementAndGet() == 0) {
			receive();
		}
	}
	
	public void receive() {
		if (count.decrementAndGet() == 0) {
			try {
				CodeSegmentManager.get().readyQueue.put(cs);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}