diff src/rep/channel/REPSelectionKey.java @ 193:3133040ee4f4

(no commit message)
author one
date Wed, 31 Dec 2008 15:06:22 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rep/channel/REPSelectionKey.java	Wed Dec 31 15:06:22 2008 +0900
@@ -0,0 +1,92 @@
+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);
+	}
+
+
+}