changeset 203:4c0a94836357 simullator-nio-both-worked

*** empty log message ***
author kono
date Sat, 30 Aug 2008 11:21:43 +0900
parents ffedaf382e0c
children 0607cc699ba3
files rep/channel/ChannelSimulator.java rep/channel/NetworkSimulator.java rep/channel/REPSelectionKey.java rep/channel/SelectableChannelSimulator.java rep/channel/SelectorSimulator.java rep/channel/ServerChannelSimulator.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMa.java
diffstat 8 files changed, 28 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/rep/channel/ChannelSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/ChannelSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -56,19 +56,20 @@
 	}
 
 
+	@SuppressWarnings("unchecked")
 	public SelectionKey keyFor(Selector selector2) {
 		return ((SelectorSimulator) selector2).getKey(this);
 	}
 	
+	@SuppressWarnings("unchecked")
 	@Override
 	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
-		SelectorSimulator selector = (SelectorSimulator) sel;
+		SelectorSimulator<P> selector = (SelectorSimulator<P>) sel;
 		return selector.register(this, ops, att);
 	}
 	
-	public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException {
-		REPSelector selector = sel;
-		return selector.register(this, ops, att);
+	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
+		return sel.register(this, ops, att);
 	}
 	
 
--- a/rep/channel/NetworkSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/NetworkSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -37,7 +37,7 @@
 	}
 	
 	/*   */
-	synchronized public void listen(SocketAddress ip, SelectorSimulator selector) {
+	synchronized public void listen(SocketAddress ip, SelectorSimulator<P> selector) {
 		serverList.add(new ServerData<P>(ip, selector));
 		logger.writeLog("listen", 1);
 		printAllState();
@@ -120,11 +120,11 @@
 class ServerData<P> {
 	//int virtualIP;
 	SocketAddress IP;
-	SelectorSimulator selector;
+	SelectorSimulator<P> selector;
 	LinkedList<ChannelSimulator<P>> acceptWaitingList;
 	LinkedList<ChannelSimulator<P>> establishedList;
 
-	ServerData(SocketAddress ip, SelectorSimulator _selector){
+	ServerData(SocketAddress ip, SelectorSimulator<P> _selector){
 		IP = ip;
 		selector = _selector;
 		acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
--- a/rep/channel/REPSelectionKey.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/REPSelectionKey.java	Sat Aug 30 11:21:43 2008 +0900
@@ -5,6 +5,7 @@
 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 {
@@ -21,6 +22,8 @@
 
 	@Override
 	public SelectableChannel channel() {
+		assert(false);
+		// use public SelectableChannel channel(REPPack<P>packer) instead
 		return key.channel();
 	}
 
@@ -28,9 +31,10 @@
 		if (REPServerSocketChannel.isSimulation) return key.channel();
 		if (key.isAcceptable()) {
 			return new REPServerSocketChannel<P>(key.channel(),packer);
-		} else {
+		} else if (key.isReadable()) {
 			return new REPSocketChannel<P>(key.channel(),packer);
 		}
+		return null;
 	}
 
 	@Override
@@ -55,13 +59,18 @@
 
 	@Override
 	public Selector selector() {
-		return key.selector();
+		assert(false);
+		return key.selector(); // should return REPSelector
 	}
 
 	public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
 		assert(!REPServerSocketChannel.isSimulation);
-		ServerSocketChannel sc = (ServerSocketChannel)key.channel();
-		return new REPSocketChannel<P>(sc.accept(),pack);
+		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;
+		return new REPSocketChannel<P>(ss,pack);
 	}
 
 
--- a/rep/channel/SelectableChannelSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/SelectableChannelSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -8,8 +8,8 @@
 
 	protected BlockingQueue<P> qread;
 	protected BlockingQueue<P> qwrite;
-	protected SelectorSimulator writeSelector;
-	protected SelectorSimulator readSelector;
+	protected SelectorSimulator<P> writeSelector;
+	protected SelectorSimulator<P> readSelector;
 
 	public SelectableChannelSimulator(SocketChannel channel) {
 		super(channel, null);
@@ -62,7 +62,7 @@
 	public void createWriteQ(){
 		qwrite = new LinkedBlockingQueue<P>();
 	}
-	public void setWriteSelector(SelectorSimulator _selector){
+	public void setWriteSelector(SelectorSimulator<P> _selector){
 		writeSelector = _selector; 
 	}
 
--- a/rep/channel/SelectorSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/SelectorSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -18,7 +18,6 @@
 		keyList = new HashSet<SelectionKey>();
 	}
 
-	//@SuppressWarnings("unchecked")
 	public int select() throws IOException {
 		selectedKeys = new HashSet<SelectionKey>();
 		
@@ -26,7 +25,7 @@
 
 			while(selectedKeys.isEmpty()){
 				for(SelectionKey key : keyList){
-					if(((SelectionKeySimulator) key).isAble())
+					if(((SelectionKeySimulator<?>) key).isAble())
 						selectedKeys.add(key);
 				}
 
@@ -62,14 +61,6 @@
 			return newKeys;//(Set<REPSelectionKey<P>>)newKeys;
 	}
 	
-	/*
-	public <T> SelectionKeySimulator register(ChannelSimulator<T> cs, int opt, Object handler){
-		SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this);
-		key.attach(handler);
-		keyList.add(key);
-		return key;
-	}*/
-	
 	public <T> SelectionKey getKey(ChannelSimulator<T> channel){
 		for(SelectionKey key : keyList){
 			if(key.channel() == channel)
--- a/rep/channel/ServerChannelSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/rep/channel/ServerChannelSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -7,7 +7,6 @@
 import java.nio.channels.SelectableChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
-import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
 
 /* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装  */
@@ -44,7 +43,7 @@
 
 	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
 		REPSelector<P> selector = sel;
-		ns.listen(IP, (SelectorSimulator) selector);
+		ns.listen(IP, (SelectorSimulator<P>) selector);
 		return selector.register(this, ops, att);
 	}
 
--- a/test/channeltest/testNetworkSimulator.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/test/channeltest/testNetworkSimulator.java	Sat Aug 30 11:21:43 2008 +0900
@@ -53,7 +53,7 @@
 			semasList.add(sm);
 			sm.start();
 		}
-		logger.writeLog("all slave SessionManagers were created",1);
+		logger.writeLog("all slave SessionManagers were created "+slavePort,1);
 		synchronized (this) {
 			try {
 				wait(100);
--- a/test/channeltest/testSeMa.java	Sat Aug 30 11:15:31 2008 +0900
+++ b/test/channeltest/testSeMa.java	Sat Aug 30 11:21:43 2008 +0900
@@ -65,6 +65,7 @@
 					if(key.isAcceptable()){
 						ns.writeLog("gets acceptable channel", 1);
 						REPSocketChannel<String> channel = key.accept(pack);
+						if(channel==null) continue;
 						channel.configureBlocking(false);
 						channel.register(selector, SelectionKey.OP_READ, null);
 						ns.writeLog("accepts a client.", 1);