Mercurial > hg > RemoteEditor > Eclipse
diff src/rep/channel/REPSelector.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/REPSelector.java Wed Dec 31 15:06:22 2008 +0900 @@ -0,0 +1,94 @@ +package rep.channel; + +import java.io.IOException; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelector; +import java.nio.channels.spi.SelectorProvider; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + + +public class REPSelector<P> extends Selector{ + + Selector selector; + + public REPSelector(AbstractSelector selector) { + this.selector = selector; + } + + public static <T>REPSelector<T> create() throws IOException{ + if(REPServerSocketChannel.isSimulation){ + return new SelectorSimulator<T>(); + } + return new REPSelector<T>(SelectorProvider.provider().openSelector()); + } + + @Override + public void close() throws IOException { + selector.close(); + } + + @Override + public boolean isOpen() { + return selector.isOpen(); + } + + @Override + public Set<SelectionKey> keys() { + return selector.keys(); + } + + @Override + public SelectorProvider provider() { + return selector.provider(); + } + + @Override + public int select() throws IOException { + return selector.select(); + } + + @Override + public int select(long timeout) throws IOException { + return selector.select(timeout); + } + + @Override + public int selectNow() throws IOException { + return selector.selectNow(); + } + + @Override + public Set<SelectionKey> selectedKeys() { + return selector.selectedKeys(); + } + + @Override + public Selector wakeup() { + return selector.wakeup(); + } + + public SelectionKey register(SelectableChannel ch, int ops, Object att) throws ClosedChannelException{ + return ch.register(selector, ops, att); + } + + public Set<REPSelectionKey<P>> selectedKeys1() { + Set<SelectionKey> keys = selector.selectedKeys(); + //System.err.println("Selected keys = "+keys); + HashSet<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); + + for (Iterator<SelectionKey> it = keys.iterator();it.hasNext(); ) { + SelectionKey k = it.next(); + newKeys.add(new REPSelectionKey<P>(k,this)); + it.remove(); + } + return newKeys; + } + + + +}