changeset 175:5653cf8e3c8b

*** empty log message ***
author kent
date Thu, 28 Aug 2008 21:22:07 +0900
parents 72252e970a8b
children 9cf8147591ea
files rep/channel/ChannelSimulator.java rep/channel/NetworkSimulator.java rep/channel/REPServerSocket.java rep/channel/SelectionKeySimulator.java rep/channel/SelectorSimulator.java rep/channel/ServerChannelSimulator.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMa.java
diffstat 8 files changed, 55 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/rep/channel/ChannelSimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/ChannelSimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -32,14 +32,10 @@
 
 	/** Connecting methods */
 	// for clients.
-	public boolean connect(int ip){
+	public boolean connect(SocketAddress ip){
 		return ns.connect(ip, this);
 	}
 
-	public boolean connect(SocketAddress semaIP) throws IOException {
-		int ip = ns.nslookup(semaIP);
-		return ns.connect(ip, this);
-	}
 	
 	public ChannelSimulator<P> accept(){
 		return null;
--- a/rep/channel/NetworkSimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/NetworkSimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -11,7 +11,7 @@
 	public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>();
 	public int ipcount = 1;
 	
-	@SuppressWarnings("unchecked")
+	@SuppressWarnings("unchecked")  // <?> から <T> へのキャストのため. 
 	public static <T> NetworkSimulator<T> singleton(){
 		// double check singleton
 		if (ns==null)
@@ -32,19 +32,17 @@
 		writeLog("construct Networksimulator", 1);
 		printAllState();
 	}
-
-		
 	
 	/*   */
-	synchronized public void listen(int ip, SelectorSimulator selector) {
+	synchronized public void listen(SocketAddress ip, SelectorSimulator selector) {
 		serverList.add(new ServerData<P>(ip, selector));
 		writeLog("listen", 1);
 		printAllState();
 	}
 
-	synchronized public ChannelSimulator<P> accept(int ip) {
+	synchronized public ChannelSimulator<P> accept(SocketAddress ip) {
 		for (ServerData<P> sd: serverList){
-			if (sd.virtualIP!=ip) continue;
+			if (!sd.IP.equals(ip)) continue;
 			writeLog("accepting..", 1);
 
 			ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove();
@@ -56,23 +54,24 @@
 		}
 		return null;
 	}
-	synchronized public boolean canAccept(int ip){
+	synchronized public boolean canAccept(SocketAddress ip){
 		for (ServerData<P> sd: serverList){
-			if (sd.virtualIP!=ip) continue;
+			if (!sd.IP.equals(ip)) continue;
 			return !sd.acceptWaitingList.isEmpty();
 		}
 		return false;
 	}
 
-	public boolean connect(int ip, ChannelSimulator<P> clientCH) {
+	public boolean connect(SocketAddress ip, ChannelSimulator<P> clientCH) {
 		ServerData<P> sd = null;
 		writeLog("connecting..", 1);
 		synchronized (this){
 			for (ServerData<P> sd0: serverList){
-				if (sd0.virtualIP!=ip) continue;
-				
-				sd = sd0;
-			}	
+				if (sd0.IP.equals(ip)){
+					sd=sd0;
+					break;
+				}
+			}
 			if (sd==null) return false;
 
 			//ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector);
@@ -96,7 +95,7 @@
 	synchronized void printAllState(){
 		writeLog("NetworkSimulator State:");
 		for (ServerData<P> sd: serverList){
-			writeLog("\tSessionManager(ip="+sd.virtualIP+"): ");
+			writeLog("\tSessionManager(ip="+sd.IP.toString()+"): ");
 			writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
 			writeLog("\testablishedList="+sd.establishedList.size());
 		}
@@ -116,13 +115,14 @@
 }
 
 class ServerData<P> {
-	int virtualIP;
+	//int virtualIP;
+	SocketAddress IP;
 	SelectorSimulator selector;
 	LinkedList<ChannelSimulator<P>> acceptWaitingList;
 	LinkedList<ChannelSimulator<P>> establishedList;
 
-	ServerData(int ip, SelectorSimulator _selector){
-		virtualIP = ip;
+	ServerData(SocketAddress ip, SelectorSimulator _selector){
+		IP = ip;
 		selector = _selector;
 		acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
 		establishedList = new LinkedList<ChannelSimulator<P>>();
--- a/rep/channel/REPServerSocket.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/REPServerSocket.java	Thu Aug 28 21:22:07 2008 +0900
@@ -17,7 +17,7 @@
 		scs = channel;
 	}
 	
-	public void bind(int ip){
+	public void bind(SocketAddress ip){
 		scs.bind(ip);
 	}
 	
--- a/rep/channel/SelectionKeySimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/SelectionKeySimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -63,14 +63,27 @@
 		return true;
 	}
 
-	@Override
-	public int readyOps() {
-		return ready;
-	}
 
 	@Override
 	public Selector selector() {
 		return selector;
 	}
 
+	@Override
+	public int readyOps() {
+		int ops=0;
+		//if ( channel instanceof SelectableChannelSimulator){
+		if ( channel instanceof ServerChannelSimulator ){
+			ServerChannelSimulator<?> scs = (ServerChannelSimulator<?>) channel;
+			ops = ( OP_ACCEPT * (scs.isAcceptable()? 1:0) );
+		}
+		if ( channel instanceof ChannelSimulator ){
+			ChannelSimulator<?> scs = (ChannelSimulator<?>) channel;
+			ops = ( OP_READ * (scs.isReadable()? 1:0) )
+					| ( OP_WRITE * (scs.isWritable()? 1:0) );
+			// (OP_READ & true) がつかえないらしい.
+		}
+		return ops;
+	}
+
 }
--- a/rep/channel/SelectorSimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/SelectorSimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -23,7 +23,7 @@
 		keyList = new TreeSet<SelectionKey>();
 	}
 
-	@SuppressWarnings("unchecked")
+	//@SuppressWarnings("unchecked")
 	public int select() throws IOException {
 		selectedKeys = new TreeSet<SelectionKey>();
 		
--- a/rep/channel/ServerChannelSimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/rep/channel/ServerChannelSimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -2,6 +2,7 @@
 
 import java.io.IOException;
 import java.net.ServerSocket;
+import java.net.SocketAddress;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
@@ -10,20 +11,21 @@
 public class ServerChannelSimulator<P>extends REPServerSocketChannel<P>{
 	protected NetworkSimulator<P> ns;
 	//public REPServerSocket<REPSocketChannel<P>> socket;
-	protected int virtualIP;
+	protected SocketAddress IP;
 
 	/**  Constructors. 
 	 * @throws IOException */
 	public ServerChannelSimulator() throws IOException {
 		//socket = REPServerSocket.<REPSocketChannel<P>>create();
+		ns = NetworkSimulator.<P>singleton();
 	}
 	
-	public void bind(int ip){
-		virtualIP = ip;
+	public void bind(SocketAddress ip){
+		IP = ip;
 	}
 
 	public REPSocketChannel<P> accept1() /*throws IOException*/ {
-		return ns.accept(virtualIP);
+		return ns.accept(IP);
 	}
 
 	@Override
@@ -55,9 +57,12 @@
 
 	public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException {
 		REPSelector selector = sel;
-		ns.listen(virtualIP, (SelectorSimulator) selector);
+		ns.listen(IP, (SelectorSimulator) selector);
 		return selector.register(this, ops, att);
 	}
 
+	public boolean isAcceptable() {
+		return ns.canAccept(IP);
+	}
 
 }
--- a/test/channeltest/testNetworkSimulator.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/test/channeltest/testNetworkSimulator.java	Thu Aug 28 21:22:07 2008 +0900
@@ -17,7 +17,7 @@
 	public static void main(String[] args){
 		REPServerSocketChannel.isSimulation = true;
 		testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2);
-
+		logger.setLogLevel(5);
 		
 		testns.startTest();
 	}
--- a/test/channeltest/testSeMa.java	Thu Aug 28 19:49:41 2008 +0900
+++ b/test/channeltest/testSeMa.java	Thu Aug 28 21:22:07 2008 +0900
@@ -39,10 +39,14 @@
 			scs.socket().setReuseAddress(true);
 			scs.socket().bind(IP);
 			scs.configureBlocking(false);
-			selector.register(scs, SelectionKey.OP_ACCEPT, null);
+			//selector.register(scs, SelectionKey.OP_ACCEPT, null);
+			scs.register(selector, SelectionKey.OP_ACCEPT, null);
 		} catch (IOException e1) {
+			ns.writeLog("cannot create REPServerSocketChannel!, exit.");
 			return;
 		}
+		ns.writeLog("selector is "+selector.toString());
+		ns.writeLog("REPssc is "+scs.toString());
 
 		ns.writeLog("SessionManager starts mainroutin.", 1);
 
@@ -59,7 +63,8 @@
 						REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel();
 						REPSocketChannel<String> channel;
 						channel = sc.accept1();
-						selector.register(channel, SelectionKey.OP_READ, null);
+						//selector.register(channel, SelectionKey.OP_READ, null);
+						channel.register(selector, SelectionKey.OP_READ, null);
 						ns.writeLog("accepts a client.", 1);
 
 					}else if(key.isReadable()){