# HG changeset patch # User kono # Date 1224119958 -32400 # Node ID 59ef23ee73ada5d30413c59ea10aef45616444c7 # Parent ef4afcae0c92ab5acb4e2917e695d4c62329b239 SelectorSimulator is not thread safe now. diff -r ef4afcae0c92 -r 59ef23ee73ad rep/channel/SelectorSimulator.java --- a/rep/channel/SelectorSimulator.java Thu Oct 16 01:12:34 2008 +0900 +++ b/rep/channel/SelectorSimulator.java Thu Oct 16 10:19:18 2008 +0900 @@ -5,30 +5,34 @@ import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.spi.SelectorProvider; +import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; +import java.util.Map; import java.util.Set; public class SelectorSimulator

extends REPSelector

{ - // access to these set have to be synchronized - private Set keyList; - private Set selectedKeys; + // This selector cannot be shared among threads. + + private Map> keyList; + private Set> selectedKeys; private boolean isOpen=true; public SelectorSimulator() { super(null); - keyList = new HashSet(); + keyList = new HashMap>(); } - public synchronized int select() throws IOException { + public int select() throws IOException { while(true) { getSelectedKeys(); if(selectedKeys.isEmpty()) { try { - this.wait(); + synchronized(this) { + wait(); + } } catch (InterruptedException e) { - throw new IOException("Error, Selector was interrupted!"); + throw new IOException(); } } else break; @@ -37,30 +41,32 @@ } @Override - public synchronized int select(long timeout) throws IOException { + public int select(long timeout) throws IOException { getSelectedKeys(); if(selectedKeys.isEmpty()) { try { - wait(timeout); + synchronized(this) { + wait(timeout); + } // we cannot know if we time outed or not getSelectedKeys(); } catch (InterruptedException e) { - throw new IOException("Error, Selector was interrupted!"); + throw new IOException(); } } return selectedKeys.size(); } private void getSelectedKeys() { - selectedKeys = new HashSet(); - for(SelectionKey key : keyList){ - if(((SelectionKeySimulator) key).isAble()) - selectedKeys.add(key); + selectedKeys = new HashSet>(); + for(SelectionKeySimulator

key : keyList.values()){ + if(key.isAble()) + selectedKeys.add(new SelectionKeySimulator

(key)); } } @Override - public synchronized int selectNow() throws IOException { + public int selectNow() throws IOException { getSelectedKeys(); return selectedKeys.size(); } @@ -69,39 +75,26 @@ return register(cs, opt, null); } public SelectionKeySimulator

register(SelectableChannel cs, int opt, Object handler){ - SelectionKeySimulator

key = new SelectionKeySimulator

(cs, opt, this); + SelectionKeySimulator

key = keyList.get(cs); + if (key!=null) { + key.attach(handler); + key.interestOps(opt); + return key; + } + key = new SelectionKeySimulator

(cs, opt, this); key.attach(handler); - deregister(cs); - synchronized(this) { - keyList.add(key); - } + keyList.put(cs,key); return key; } - public synchronized void deregister(SelectableChannel channel) { - for(Iterator it = keyList.iterator();it.hasNext();) { - if(it.next().channel() == channel) - it.remove(); - } + public SelectionKeySimulator

deregister(SelectableChannel channel) { + SelectionKeySimulator

key = keyList.remove(channel); + return key; } - public synchronized Set> selectedKeys1() { - Set keys = keyList; - Set> newKeys = new HashSet>(); - for(SelectionKey k: keys) { - // REPSelectionKeyを生成しないように注意 - newKeys.add(new SelectionKeySimulator

(k)); - } - return newKeys; - } - - public synchronized SelectionKey getKey(ChannelSimulator channel){ - for(SelectionKey key : keyList){ - if(key.channel() == channel) - return key; - } - return null; + public SelectionKey getKey(SelectableChannel channel){ + return keyList.get(channel); } @Override @@ -116,7 +109,23 @@ @Override public Set keys() { - return keyList; + Set newKeys = new HashSet(); + for(SelectionKey k: keyList.values()) { + // REPSelectionKeyを生成しないように注意 + newKeys.add(k); + } + return newKeys; + } + + public Set> keys1() { + // we cannot solve cast, we need the same method again with different + // types + Set> newKeys = new HashSet>(); + for(SelectionKeySimulator

k: keyList.values()) { + // REPSelectionKeyを生成しないように注意 + newKeys.add(k); //new SelectionKeySimulator

(k)); + } + return newKeys; } @Override @@ -132,9 +141,26 @@ return this; } + + public Set> selectedKeys1() { + Set> newKeys = new HashSet>(); + for(SelectionKeySimulator

k: selectedKeys) { + // REPSelectionKeyを生成しないように注意 + //newKeys.add(new SelectionKeySimulator

(k)); + newKeys.add(k); + } + return newKeys; + } + @Override - public synchronized Set selectedKeys() { - return (Set)selectedKeys; + public Set selectedKeys() { + Set> keys = selectedKeys; + Set newKeys = new HashSet(); + for(SelectionKeySimulator

k: keys) { + // REPSelectionKeyを生成しないように注意 + newKeys.add(k); // new SelectionKeySimulator

(k)); + } + return newKeys; } }