Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/REPSelectionKey.java @ 225:e173411a2499
*** empty log message ***
author | kent |
---|---|
date | Sun, 31 Aug 2008 13:32:15 +0900 |
parents | 6cd4aab9fea3 |
children | 168dd841be51 |
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(); if (key.isAcceptable()) { SelectableChannel sc = key.channel(); SelectableChannel rsc = REPSocketChannel.channels.get(sc); return rsc; } else if (key.isReadable()) { SelectableChannel sc = key.channel(); SelectableChannel rsc = REPSocketChannel.channels.get(sc); return rsc; } return null; } public SelectableChannel channel(REPPack<P>packer) { if (REPServerSocketChannel.isSimulation) return key.channel(); if (key.isAcceptable()) { SelectableChannel sc = key.channel(); SelectableChannel rsc = REPSocketChannel.channels.get(sc); if (rsc!=null) return rsc; return new REPServerSocketChannel<P>(key.channel(),packer); } else if (key.isReadable()) { SelectableChannel sc = key.channel(); SelectableChannel rsc = REPSocketChannel.channels.get(sc); if (rsc!=null) return rsc; return new REPSocketChannel<P>(key.channel(),packer); } return null; } @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(); if (ss==null) return null; REPSocketChannel<P> rsc = (REPSocketChannel<P>)REPSocketChannel.channels.get(ss); if (rsc!=null) return rsc; return new REPSocketChannel<P>(ss,pack); } }