view rep/simulator/SelectionKeySimulator.java @ 123:5b1a0574b406 add-simulator

*** empty log message ***
author pin
date Wed, 27 Aug 2008 17:21:25 +0900
parents
children
line wrap: on
line source

package rep.simulator;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

public class SelectionKeySimulator<P> extends SelectionKey{
	
	private int interestOpt;
	private SelectableChannelSimulator<P> channel;
	private int ready;
	public Selector selector;

	public SelectionKeySimulator(SelectableChannelSimulator<P> cs, int opt, Selector _selector) {
		channel = cs;
		interestOpt = opt;
		selector = _selector;
	}

	public boolean isAble() {
		if ( (interestOpt&OP_READ)!=0 && isReadable() )
			return true;
		else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() )
			return true;
		else if( (interestOpt&OP_WRITE)!=0 && isWritable() )
			return true;
		else
			return false;
	}

	public void setFlag() {
		ready = 0;
		if(channel.isAcceptable()) ready |= OP_ACCEPT;
		if(channel.isReadable()) ready |= OP_READ;
		if(channel.isWritable()) ready |= OP_WRITE;
	}

	public SelectableChannelSimulator<P> channel() {
		return channel;
	}


	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int interestOps() {
		// TODO Auto-generated method stub
		return interestOpt;
	}

	@Override
	public SelectionKey interestOps(int ops) {
		interestOpt = ops;
		return this;
	}

	@Override
	public boolean isValid() {
		return true;
	}

	@Override
	public int readyOps() {
		return ready;
	}

	@Override
	public Selector selector() {
		// TODO Auto-generated method stub
		return selector;
	}

}