view rep/channel/SelectionKeySimulator.java @ 308:c5be84d53c7f channel-simulator-update **INVALID**

*** empty log message ***
author kono
date Sat, 04 Oct 2008 22:12:34 +0900
parents cf9328e66d25
children 5e18919011a8
line wrap: on
line source

package rep.channel;

import java.io.IOException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

public class SelectionKeySimulator<P> extends REPSelectionKey<P>{
	
	private int interestOpt;
	private SelectableChannel channel;
	private int ready;
	public Selector selector;
	public boolean canceled = false;

	public SelectionKeySimulator(SelectableChannel cs, int opt, Selector _selector) {
		channel = cs;
		interestOpt = opt;
		selector = _selector;
	}
	public SelectionKeySimulator(SelectionKey k) {
		channel = k.channel();
		interestOpt = k.interestOps();
		selector = k.selector();
		attach(k.attachment());
	}


	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() {
		ChannelSimulator<?> scs = (ChannelSimulator<?>) channel;
		ready = 0;
		if(scs.isAcceptable()) ready |= OP_ACCEPT;
		if(scs.isReadable()) ready |= OP_READ;
		if(scs.isWritable()) ready |= OP_WRITE;
	}

	public SelectableChannel channel() {
		return channel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public REPSocketChannel<P> channel1() {
		return (REPSocketChannel<P>)channel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public REPServerSocketChannel<P> serverSocketChannel() {
		return (REPServerSocketChannel<P>)channel;
	}

	public SelectableChannel channel(REPPack<P> packer) {
		return channel;
	}

	@SuppressWarnings("unchecked")
	public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
		assert(channel instanceof ServerChannelSimulator);
		ServerChannelSimulator<P> scs = (ServerChannelSimulator<P>) channel;
		return scs.accept1();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void cancel() {
		canceled = true;
		SelectorSimulator s = (SelectorSimulator)selector;
		s.deregister(channel);
	}

	@Override
	public int interestOps() {
		return interestOpt;
	}

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

	@Override
	public boolean isValid() {
		return (!canceled) && channel.isOpen() && selector.isOpen();
	}


	@Override
	public Selector selector() {
		return selector;
	}

	@Override
	public int readyOps() {
		int ops=0;
		if ( channel instanceof ServerChannelSimulator ){
			ServerChannelSimulator<?> scs = (ServerChannelSimulator<?>) channel;
			ops = ( OP_ACCEPT * (scs.isAcceptable()? 1:0) );
		} else if ( channel instanceof ChannelSimulator ){
			ChannelSimulator<?> scs = (ChannelSimulator<?>) channel;
			ops = ( OP_READ * (scs.isReadable()? 1:0) )
					| ( OP_WRITE * (scs.isWritable()? 1:0) );
			// (OP_READ & true) がつかえないらしい.
		}
		return ops;
	}

}