view rep/channel/ChannelSimulator.java @ 203:4c0a94836357 simullator-nio-both-worked

*** empty log message ***
author kono
date Sat, 30 Aug 2008 11:21:43 +0900
parents 8d7c74610b05
children 0498425202a4
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;

public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{
	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;
	}

	@Override
	public boolean isAcceptable() {
		return false;
	}
	@Override
	public boolean isReadable() {
		synchronized (qread){ 
		return !qread.isEmpty();
		}
	}
	@Override
	public boolean isWritable() {
		return true;
	}
	@Override
	public SelectableChannel configureBlocking(boolean block) throws IOException {
		return null;
	}


	@SuppressWarnings("unchecked")
	public SelectionKey keyFor(Selector selector2) {
		return ((SelectorSimulator) selector2).getKey(this);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
		SelectorSimulator<P> selector = (SelectorSimulator<P>) sel;
		return selector.register(this, ops, att);
	}
	
	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
		return sel.register(this, ops, att);
	}
	

}