changeset 208:302c4a0afab6

*** empty log message ***
author kono
date Sat, 30 Aug 2008 12:15:21 +0900
parents 9aeade335af0
children 1eec69035548
files rep/channel/REPSelectionKey.java rep/channel/REPServerSocketChannel.java rep/channel/REPSocketChannel.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMaSlave.java
diffstat 5 files changed, 48 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/rep/channel/REPSelectionKey.java	Sat Aug 30 11:51:42 2008 +0900
+++ b/rep/channel/REPSelectionKey.java	Sat Aug 30 12:15:21 2008 +0900
@@ -30,8 +30,14 @@
 	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;
@@ -63,6 +69,7 @@
 		return key.selector(); // should return REPSelector
 	}
 
+	@SuppressWarnings("unchecked")
 	public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
 		assert(!REPServerSocketChannel.isSimulation);
 		if (!key.isAcceptable()) throw new IOException();
@@ -70,6 +77,8 @@
 		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);
 	}
 
--- a/rep/channel/REPServerSocketChannel.java	Sat Aug 30 11:51:42 2008 +0900
+++ b/rep/channel/REPServerSocketChannel.java	Sat Aug 30 12:15:21 2008 +0900
@@ -38,14 +38,20 @@
 	}
 	
 	public REPServerSocketChannel(ServerSocketChannel open, REPPack<P> packer) {
-		ssc = open; this.
-		packer = packer;
+		ssc = open; 
+		this.packer = packer;
+		REPSocketChannel.addChannel(ssc,this);
 	}
 	
+	public void close1() throws IOException {
+		REPSocketChannel.removeChannel(ssc);
+		ssc.close();
+	}
 
 	public REPServerSocketChannel(SelectableChannel channel, REPPack<P> packer) {
 		this.ssc = (ServerSocketChannel)channel;
 		this.packer = packer;
+		REPSocketChannel.addChannel(ssc,this);
 	}
 
 	public REPSocketChannel<P> accept1() throws IOException {
--- a/rep/channel/REPSocketChannel.java	Sat Aug 30 11:51:42 2008 +0900
+++ b/rep/channel/REPSocketChannel.java	Sat Aug 30 12:15:21 2008 +0900
@@ -10,22 +10,39 @@
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
+import java.util.HashMap;
 
 public class REPSocketChannel<P> extends SelectableChannel{
 
 	public SocketChannel sc;
 	private REPPack<P> pack;
+	static public HashMap<SelectableChannel,SelectableChannel> channels = new HashMap<SelectableChannel,SelectableChannel>();  
 
 	public REPSocketChannel(SocketChannel channel, REPPack<P> packer) {
 		sc = channel;
 		pack = packer;
+		addChannel(sc,this);
 	}
 
 	public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) {
 		sc = (SocketChannel)channel;
 		pack = packer;
+		addChannel(sc,this);
+	}
+
+	public static void addChannel(SelectableChannel sc,SelectableChannel rc) {
+		channels.put(sc, rc);
 	}
 
+	public void close1() throws IOException {
+		removeChannel(sc);
+		sc.close();
+	}
+
+	public static void removeChannel(SelectableChannel sc) throws IOException {
+		if(channels.containsKey(sc)) channels.remove(sc);
+	}
+	
 	@Override
 	public Object blockingLock() {
 		return sc.blockingLock();
--- a/test/channeltest/testNetworkSimulator.java	Sat Aug 30 11:51:42 2008 +0900
+++ b/test/channeltest/testNetworkSimulator.java	Sat Aug 30 12:15:21 2008 +0900
@@ -16,7 +16,7 @@
 
 	public static void main(String[] args){
 		REPServerSocketChannel.isSimulation = false;
-		testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2);
+		testNetworkSimulator testns = new testNetworkSimulator(1, 2, 2);
 		logger.setLogLevel(5);
 		
 		testns.startTest();
@@ -45,6 +45,7 @@
 			sm.start();
 		}
 		logger.writeLog("all master SessionManagers were created",1);
+		sleep(100);
 		slavePort = port;
 		for (int i=0; i<NoSemaSlave; i++){
 			testSeMaSlave sm = new testSeMaSlave("SeMaS"+i, 
@@ -54,12 +55,7 @@
 			sm.start();
 		}
 		logger.writeLog("all slave SessionManagers were created "+slavePort,1);
-		synchronized (this) {
-			try {
-				wait(100);
-			} catch (InterruptedException e) {
-			}
-		}
+		sleep(100);
 		for (int i=0; i<NoEditor; i++){
 			testEditor te = new testEditor("Editor"+i,host, masterPort+rand.nextInt(NoSemaMaster+NoSemaSlave)); 
 			editorList.add(te);
@@ -82,6 +78,16 @@
 		}
 		//System.exit(0);
 	}
+
+
+	private void sleep(int time) {
+		synchronized (this) {
+			try {
+				wait(time);
+			} catch (InterruptedException e) {
+			}
+		}
+	}
 	
 	public void Checker(){
 		
--- a/test/channeltest/testSeMaSlave.java	Sat Aug 30 11:51:42 2008 +0900
+++ b/test/channeltest/testSeMaSlave.java	Sat Aug 30 12:15:21 2008 +0900
@@ -49,7 +49,7 @@
 			masterCH.register(selector, SelectionKey.OP_READ,null);
 
 
-			ns.writeLog("SessionManager starts main routin.", 1);
+			ns.writeLog("Slave SessionManager starts main routin.", 1);
 
 			/* Main Loop */
 			while(running){