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

*** empty log message ***
author kono
date Sat, 04 Oct 2008 22:12:34 +0900
parents 3be92eb54b1c
children 2c00fa39dd84
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;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


public class REPSelectionKey<P> extends SelectionKey {
	SelectionKey key;
	private REPSelector<P> selector;
	
	public REPSelectionKey() {
		
	}
	
	public REPSelectionKey(SelectionKey key,REPSelector<P>s) {
		this.key = key;
		this.selector = s;
		attach(key.attachment());
	}
	
	@Override
	public void cancel() {
		key.cancel();
	}

	@Override
	public SelectableChannel channel() {
		if (REPServerSocketChannel.isSimulation) return key.channel();
		SelectableChannel sc = key.channel();
		SelectableChannel rsc = REPSocketChannel.channels.get(sc);
		return rsc;
	}

	@SuppressWarnings("unchecked")
	public REPSocketChannel<P> channel1() {
		assert (!REPServerSocketChannel.isSimulation); 
		SelectableChannel sc = key.channel();
		REPSocketChannel<P> rsc = (REPSocketChannel<P>) REPSocketChannel.channels.get(sc);
		return rsc;
	}
	
	@SuppressWarnings("unchecked")
	public REPServerSocketChannel<P> serverSocketChannel() {
		assert (!REPServerSocketChannel.isSimulation); 
		SelectableChannel sc = key.channel();
		REPServerSocketChannel<P> rsc = (REPServerSocketChannel<P>) REPSocketChannel.channels.get(sc);
		return rsc;
	}

	@Override
	public int interestOps() {
		return key.interestOps();
	}

	@Override
	public SelectionKey interestOps(int ops) {
		return key.interestOps(ops);
	}

	@Override
	public boolean isValid() {
		return key.isValid();
	}

	@Override
	public int readyOps() {
		return key.readyOps();
	}

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

	@SuppressWarnings("unchecked")
	public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
		assert(!REPServerSocketChannel.isSimulation);
		if (!key.isAcceptable()) throw new IOException();
		ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
		if (ssc==null) return null;
		SocketChannel ss = (SocketChannel)ssc.accept();
		//System.err.println("Accept in SelectionKey "+ss);
		if (ss==null) return null;
		return new REPSocketChannel(ss, pack);
	}


}