view rep/channel/ChannelSimulator.java @ 175:5653cf8e3c8b

*** empty log message ***
author kent
date Thu, 28 Aug 2008 21:22:07 +0900
parents 72252e970a8b
children be219ba8b39c
line wrap: on
line source

package rep.channel;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.SelectorProvider;


public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{
	//private BlockingQueue<P> qread;
	//private BlockingQueue<P> qwrite;
	//private SelectorSimulator<P> waitingSelector;
	protected NetworkSimulator<P> ns;

	/**  Constructors. */
	public ChannelSimulator(){
		super(null);
		ns = NetworkSimulator.<P>singleton();
	}

	public ChannelSimulator<P> createConjugatedChannel() {
		ChannelSimulator<P> ret = new ChannelSimulator<P>();
		ret.qread=qwrite;
		ret.qwrite=qread;
		ret.readSelector=writeSelector;
		ret.writeSelector=readSelector;
		return ret;
	}

	/** Connecting methods */
	// for clients.
	public boolean connect(SocketAddress ip){
		return ns.connect(ip, this);
	}

	
	public ChannelSimulator<P> accept(){
		return null;
	}

	/* return state of the Queue(debug)  */
	/*
	public boolean readQisEmpty() {
		return qread.isEmpty();
	}
	public boolean writeQisEmpty() {
		return qwrite.isEmpty();
	}
	*/
	
	@Override
	public boolean isAcceptable() {
		return false;
	}
	@Override
	public boolean isReadable() {
		synchronized (qread){ 
		return !qread.isEmpty();
		}
	}
	@Override
	public boolean isWritable() {
		return true;
	}

	public SelectionKey keyFor(Selector selector2) {
		return ((SelectorSimulator) selector2).getKey(this);
	}
	@Override
	public Object blockingLock() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public SelectableChannel configureBlocking(boolean block) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean isBlocking() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isRegistered() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SelectorProvider provider() {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
		SelectorSimulator selector = (SelectorSimulator) sel;
		return selector.register(this, ops, att);
	}
	public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException {
		REPSelector selector = sel;
		return selector.register(this, ops, att);
	}
	
	@Override
	public int validOps() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	protected void implCloseChannel() throws IOException {
		// TODO Auto-generated method stub
		
	}

}