changeset 240:168dd841be51 iterator-bug-fig **INVALID**

selected key Interator fix.
author kono
date Sun, 31 Aug 2008 20:39:06 +0900
parents f9b1a9ba1a73
children 1e5bf172b4d6
files rep/channel/REPSelectionKey.java rep/channel/REPSelector.java rep/channel/REPSocketChannel.java
diffstat 3 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rep/channel/REPSelectionKey.java	Sun Aug 31 19:49:35 2008 +0900
+++ b/rep/channel/REPSelectionKey.java	Sun Aug 31 20:39:06 2008 +0900
@@ -31,10 +31,12 @@
 	public SelectableChannel channel() {
 		if (REPServerSocketChannel.isSimulation) return key.channel();
 		if (key.isAcceptable()) {
+			//System.err.println("Accept selectionKey.channel = "+key.channel());
 			SelectableChannel sc = key.channel();
 			SelectableChannel rsc = REPSocketChannel.channels.get(sc);
 			return rsc;
 		} else if (key.isReadable()) {
+			//System.err.println("Read selectionKey.channel = "+key.channel());
 			SelectableChannel sc = key.channel();
 			SelectableChannel rsc = REPSocketChannel.channels.get(sc);
 			return rsc;
@@ -46,11 +48,13 @@
 	public SelectableChannel channel(REPPack<P>packer) {
 		if (REPServerSocketChannel.isSimulation) return key.channel();
 		if (key.isAcceptable()) {
+			//System.err.println("AcceptP selectionKey.channel = "+key.channel());
 			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()) {
+			//System.err.println("ReadP selectionKey.channel = "+key.channel());
 			SelectableChannel sc = key.channel();
 			SelectableChannel rsc = REPSocketChannel.channels.get(sc);
 			if (rsc!=null) return rsc;
@@ -91,10 +95,9 @@
 		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;
-		REPSocketChannel<P> rsc = (REPSocketChannel<P>)REPSocketChannel.channels.get(ss);
-		if (rsc!=null) return rsc;
-		return new REPSocketChannel<P>(ss,pack);
+		return new REPSocketChannel(ss, pack);
 	}
 
 
--- a/rep/channel/REPSelector.java	Sun Aug 31 19:49:35 2008 +0900
+++ b/rep/channel/REPSelector.java	Sun Aug 31 20:39:06 2008 +0900
@@ -8,6 +8,7 @@
 import java.nio.channels.spi.AbstractSelector;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Set;
 
 
@@ -77,9 +78,13 @@
 
 	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(SelectionKey k: keys) {
+
+	    for (Iterator<SelectionKey> it = keys.iterator();it.hasNext(); ) {
+	        SelectionKey k = it.next();
 			newKeys.add(new REPSelectionKey<P>(k,this));
+	        it.remove();
 		}
 		return newKeys;
 	}
--- a/rep/channel/REPSocketChannel.java	Sun Aug 31 19:49:35 2008 +0900
+++ b/rep/channel/REPSocketChannel.java	Sun Aug 31 20:39:06 2008 +0900
@@ -32,6 +32,7 @@
 
 	public static void addChannel(SelectableChannel sc,SelectableChannel rc) {
 		channels.put(sc, rc);
+		System.err.println("add Channel "+channels);
 	}
 
 	public void close1() throws IOException {
@@ -74,11 +75,6 @@
 	}
 
 
-	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
-		return sc.register(sel.selector, ops, att);
-	}
-
-
 	@Override
 	public int validOps() {
 		return sc.validOps();
@@ -139,6 +135,11 @@
 		return sc.connect(semaIP);
 	}
 
+	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
+		return sc.register(sel.selector, ops, att);
+	}
+
+
 
 	public SelectionKey register1(REPSelector<P> sel, int ops, Object att)
 			throws ClosedChannelException {