/*
 * Created on Apr 4, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ps6.v2;

import ps6.Message;

/**
 * @author knorton
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Connector {
	private Gate 		a,b;
	private Message		toA,toB;
	private boolean		probed = false;
	private String		name = null;
	
	public Connector(String name, Gate a, Gate b) {
		this(a,b);
		this.name = name;
	}
	
	public Connector(Gate a, Gate b) {
		this.a = a;
		this.b = b;

		this.toA = new Message();
		this.toB = new Message();
		
		a.connect(this);
		b.connect(this);
	}
	
	public void probe() {
		this.probed = true;
	}
	
	public boolean isProbed() {
		return this.probed;
	}
	
	public void send(Gate caller, Message m) {
		if (caller == a)
			toB = m;
		else if (caller == b)
			toA = m;
		else
			throw new RuntimeException();
	}
	
	public Message receive(Gate caller) {
		if (caller == a)
			return toA;
		else if (caller == b)
			return toB;
		
		throw new RuntimeException();
	}
	
	public String toString() {
		String name = (this.name != null)?this.name:"";
		return name + " = " + this.toA + " " + this.toB;
	}
}
