changeset 123:5b1a0574b406 add-simulator

*** empty log message ***
author pin
date Wed, 27 Aug 2008 17:21:25 +0900
parents 790c8dd42a7b
children 97a321d91b79
files rep/SessionManager.java rep/channel/ChannelSimulator.java rep/channel/NetworkSimulator.java rep/channel/REPServerSocketChannel.java rep/channel/REPSocketChannel.java rep/channel/SelectableChannelSimulator.java rep/channel/SelectionKeySimulator.java rep/channel/SelectorSimulator.java rep/channel/ServerChannelSimulator.java rep/channel/ServerChannelSimulatorImpl.java rep/simulator/ChannelSimulator.java rep/simulator/NetworkSimulator.java rep/simulator/REPSelector.java rep/simulator/SelectableChannelSimulator.java rep/simulator/SelectionKeySimulator.java rep/simulator/SelectorSimulator.java rep/simulator/ServerChannelSimulator.java
diffstat 17 files changed, 1444 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/rep/SessionManager.java	Tue Aug 26 16:33:47 2008 +0900
+++ b/rep/SessionManager.java	Wed Aug 27 17:21:25 2008 +0900
@@ -16,6 +16,8 @@
 import java.util.LinkedList;
 import java.util.StringTokenizer;
 
+import rep.channel.REPServerSocketChannel;
+import rep.simulator.REPSelector;
 import rep.xml.SessionXMLDecoder;
 import rep.xml.SessionXMLEncoder;
 
@@ -26,7 +28,7 @@
 //o-------header section (network order)-------------o
 /*int cmd;	// command
 int sid;	// session ID : uniqu to editing file
-int eid;	// editor ID : owner editor ID = 1。Session に対してユニーク
+int eid;	// editor ID : owner editor ID = 1。Session に対して unique
 int seqno;	// Sequence number : sequence number はエディタごとに管理
 int lineno;	// line number
 int textsize;   // textsize : bytesize
@@ -57,12 +59,15 @@
 	}
 	
 	public void openSelector() throws IOException{
-		selector = Selector.open();
+		//selector = Selector.open();
+		selector = REPSelector.open();
 	}
 	
 	public void sessionManagerNet(int port) throws InterruptedException, IOException {
 	
-		ServerSocketChannel ssc = ServerSocketChannel.open();
+		//ServerSocketChannel ssc = ServerSocketChannel.open();
+		ServerSocketChannel ssc = REPServerSocketChannel.open();
+		
 		ssc.configureBlocking(false);	//reuse address 必須
 		
 		ssc.socket().setReuseAddress(true);
@@ -81,38 +86,19 @@
 				if(key.isAcceptable()){
 					/*** serverChannelはenableになったSelectionKeyのchannel ***/
 					ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
-					/*** EditorChannel を用いない記述 ***/
 					SocketChannel channel = serverChannel.accept();	//keyからchannelを取って、accept 
 					registerChannel (selector, channel, SelectionKey.OP_READ);
 					channel = null;
-					
-					/*** EditorChannel を用いた記述 ****/
-					//EditorChannel echannel = (EditorChannel) ssc.accept();
-					//echannel.setIO();
-					//registerChannel(selector, echannel, SelectionKey.OP_READ);
-					//echannel = null;
-					
-					/*** SelectableEditorChannel ***/
-					//SocketChannel channel = ssc.accept();
-					//SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel);
-					//registerChannel(selector, echannel2, SelectionKey.OP_READ);
-					//channel = null;
-					//echannel2 = null;
+
 					
 				}else if(key.isReadable()){
 					
-					/*** EditorChannel を用いない記述 ***/
 					SocketChannel channel = (SocketChannel)key.channel();
-					REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。
+					REPPacketReceive receive = new REPPacketReceive(channel);
 					receive.setkey(key);
 					REPCommand receivedCommand = receive.unpackUConv();
-					//REPCommand receivedCommand = receive.unpack();
 					manager(channel, receivedCommand);
 					
-					/*** EditorChannel を用いた記述 ****/
-					//EditorChannel echannel = (EditorChannel) key.channel();
-					//REPCommand command = echannel.getPacket();
-					//manager(echannel, command);
 					
 				}else if(key.isConnectable()){
 					System.out.println("Connectable");
@@ -125,7 +111,6 @@
 		if(channel == null) {
 			return;
 		}
-		//System.out.println("registerChannel()");
 		channel.configureBlocking(false);
 		selector.wakeup();
 		channel.register(selector, ops);
@@ -399,7 +384,6 @@
 	}
 
 	private int reverseCmd(int cmd) {
-		// TODO Auto-generated method stub
 		int kindOfCmd = 0;
 		switch(cmd){
 		case REP.REPCMD_INSERT:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/ChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,116 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+
+
+public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{
+	//private BlockingQueue<P> qread;
+	//private BlockingQueue<P> qwrite;
+	//private SelectorSimulator<P> waitingSelector;
+	protected NetworkSimulator<P> ns;
+
+	/**  Constructors. */
+	public ChannelSimulator(NetworkSimulator<P> _ns){
+		this(_ns, null);
+	}
+	
+	public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> _selector){
+		super(null);
+		ns = _ns;
+		//ns = NetworkSimulator.singleton();  //どっちがいい?
+	}
+	public ChannelSimulator<P> createConjugatedChannel() {
+		ChannelSimulator<P> ret = new ChannelSimulator<P>(ns);
+		ret.qread=qwrite;
+		ret.qwrite=qread;
+		ret.readSelector=writeSelector;
+		ret.writeSelector=readSelector;
+		return ret;
+	}
+
+	/** Connecting methods */
+	// for clients.
+	public boolean connect(int ip){
+		return ns.connect(ip, this);
+	}
+	
+	public ChannelSimulator<P> accept(){
+		return null;
+	}
+
+	/* return state of the Queue(debug)  */
+	/*
+	public boolean readQisEmpty() {
+		return qread.isEmpty();
+	}
+	public boolean writeQisEmpty() {
+		return qwrite.isEmpty();
+	}
+	*/
+	
+	@Override
+	public boolean isAcceptable() {
+		return false;
+	}
+	@Override
+	public boolean isReadable() {
+		synchronized (qread){ 
+		return !qread.isEmpty();
+		}
+	}
+	@Override
+	public boolean isWritable() {
+		return true;
+	}
+
+	public SelectionKey keyFor(Selector selector2) {
+		return ((SelectorSimulator<P>) selector2).getKey(this);
+	}
+	@Override
+	public Object blockingLock() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public boolean isBlocking() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+	@Override
+	public boolean isRegistered() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public int validOps() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+	@Override
+	protected void implCloseChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/NetworkSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,126 @@
+package rep.channel;
+
+import java.util.LinkedList;
+
+
+public class NetworkSimulator<P> {
+	public static NetworkSimulator<?> ns;
+	synchronized public static <T> NetworkSimulator<T> singleton(){
+		if (ns==null)
+			ns = new NetworkSimulator<T>();
+		return (NetworkSimulator<T>) ns;
+		// NetworkSimulator<Obj> ns = NetworkSimulator.singleton(new NetworkSimulator<Obj>());
+	}
+
+	int logLevel=5;
+	/** Listening Servers. */
+	private LinkedList<ServerData<P>> serverList;
+
+	/** Constructor. */
+	public NetworkSimulator(){
+		serverList = new LinkedList<ServerData<P>>();
+		writeLog("construct Networksimulator", 1);
+		printAllState();
+	}
+
+		
+	
+	/*   */
+	synchronized public void listen(int ip, SelectorSimulator<P> selector) {
+		serverList.add(new ServerData<P>(ip, selector));
+		writeLog(Thread.currentThread(), "listen", 1);
+		printAllState();
+	}
+
+	synchronized public ChannelSimulator<P> accept(int ip) {
+		for (ServerData<P> sd: serverList){
+			if (sd.virtualIP!=ip) continue;
+			writeLog(Thread.currentThread(), "accepting..", 1);
+
+			ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove();
+			sd.establishedList.add(serverCH);
+
+			writeLog(Thread.currentThread(), "accepted", 1);
+			printAllState();
+			return serverCH;
+		}
+		return null;
+	}
+	synchronized public boolean canAccept(int ip){
+		for (ServerData<P> sd: serverList){
+			if (sd.virtualIP!=ip) continue;
+			return !sd.acceptWaitingList.isEmpty();
+		}
+		return false;
+	}
+
+	public boolean connect(int ip, ChannelSimulator<P> clientCH) {
+		ServerData<P> sd = null;
+		writeLog(Thread.currentThread(), "connecting..", 1);
+		synchronized (this){
+			for (ServerData<P> sd0: serverList){
+				if (sd0.virtualIP!=ip) continue;
+				
+				sd = sd0;
+			}	
+			if (sd==null) return false;
+
+			//ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector);
+			clientCH.createReadQ();
+			clientCH.createWriteQ();
+			clientCH.setWriteSelector(sd.selector);
+
+			ChannelSimulator<P> serverCH = clientCH.createConjugatedChannel();
+			sd.acceptWaitingList.add(serverCH);
+		}
+
+		synchronized (sd.selector) {
+			sd.selector.notifyAll();
+		}
+		writeLog(Thread.currentThread(), "connected", 1);
+		printAllState();
+		return true;
+	}
+
+	/** for DEBUG methods. */
+	synchronized void printAllState(){
+		writeLog("NetworkSimulator State:");
+		for (ServerData<P> sd: serverList){
+			writeLog("\tSessionManager(ip="+sd.virtualIP+"): ");
+			writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
+			writeLog("\testablishedList="+sd.establishedList.size());
+		}
+	}
+	
+	/** simulation log command */
+	synchronized public void writeLog(String log, int level){
+		if ( level<=logLevel )
+			System.out.println(log);
+		System.out.flush();
+	}
+	public void writeLog(String log){
+		writeLog(log, 0);
+	}
+	public void writeLog(Thread thr, String log, int level){
+		writeLog(thr.getName()+": "+log, level);
+	}
+	public void setLogLevel(int logLevel) {
+		this.logLevel = logLevel;
+	}
+
+
+}
+
+class ServerData<P> {
+	int virtualIP;
+	SelectorSimulator<P> selector;
+	LinkedList<ChannelSimulator<P>> acceptWaitingList;
+	LinkedList<ChannelSimulator<P>> establishedList;
+
+	ServerData(int ip, SelectorSimulator<P> _selector){
+		virtualIP = ip;
+		selector = _selector;
+		acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
+		establishedList = new LinkedList<ChannelSimulator<P>>();
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/REPServerSocketChannel.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,56 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+
+public class REPServerSocketChannel<P> extends ServerSocketChannel{
+
+	public static boolean isSimulation;
+	private ServerSocketChannel ss;
+	
+	protected REPServerSocketChannel(SelectorProvider provider) {
+		super(provider);
+		
+	}
+	
+	public REPServerSocketChannel(ServerSocketChannel channel){
+		super(null);
+		ss = channel;
+	}
+
+	public static REPServerSocketChannel open() throws IOException {
+		if(isSimulation){
+			return ServerChannelSimulatorImpl.open();
+		}else{
+			return new REPServerSocketChannel(open());
+		}
+	}
+
+	public REPSocketChannel<P> accept1() throws IOException {
+		return new REPSocketChannel<P>(ss.accept());
+	}
+
+	@Override
+	public ServerSocket socket() {
+		return ss.socket();
+	}
+
+	@Override
+	protected void implCloseSelectableChannel() throws IOException {
+		ss.close();
+	}
+
+	@Override
+	protected void implConfigureBlocking(boolean block) throws IOException {
+		ss.configureBlocking(block);
+	}
+
+	@Override
+	public SocketChannel accept() throws IOException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/REPSocketChannel.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,66 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+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;
+
+public class REPSocketChannel<P> extends SelectableChannel{
+
+	private SocketChannel sc;
+
+	public REPSocketChannel(SocketChannel channel) {
+		sc = channel;
+	}
+
+	@Override
+	public Object blockingLock() {
+		return sc.blockingLock();
+	}
+
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		return sc.configureBlocking(block);
+	}
+
+	@Override
+	public boolean isBlocking() {
+		return sc.isBlocking();
+	}
+
+	@Override
+	public boolean isRegistered() {
+		return sc.isRegistered();
+	}
+
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		return sc.keyFor(sel);
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		return sc.provider();
+	}
+
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
+		return sc.register(sel, ops, att);
+	}
+
+	@Override
+	public int validOps() {
+		return sc.validOps();
+	}
+
+	@Override
+	protected void implCloseChannel() throws IOException {
+		sc.close();
+	}
+	
+	
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/SelectableChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,78 @@
+package rep.channel;
+
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SocketChannel;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+
+
+public abstract class SelectableChannelSimulator<P> extends REPSocketChannel<P>{
+
+	protected BlockingQueue<P> qread;
+	protected BlockingQueue<P> qwrite;
+	protected SelectorSimulator<P> writeSelector;
+	protected SelectorSimulator<P> readSelector;
+	
+	public SelectableChannelSimulator(SocketChannel channel) {
+		super(channel);
+	}
+	
+	/* read from Queue.  */
+	public P read(){
+		try {
+			if(readSelector!=null)
+				synchronized (readSelector){
+					return qread.take();
+				}
+			else{
+				return qread.take();
+			}
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+	/* write to Queue.  */
+	public boolean write(P p){
+		try {
+			if (writeSelector!=null)
+				synchronized (writeSelector){
+					qwrite.put(p);
+					writeSelector.notifyAll();
+				}
+			else {
+				qwrite.put(p);
+			}
+			return true;
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return false;
+		}
+	}
+	public abstract ChannelSimulator<P> accept();
+	
+	/* accessor methods.  */
+	public BlockingQueue<P> getReadQ(){
+		return qread;
+	}
+	public BlockingQueue<P> getWriteQ(){
+		return qwrite;
+	}
+	public void createReadQ(){
+		qread = new LinkedBlockingQueue<P>();
+	}
+	public void createWriteQ(){
+		qwrite = new LinkedBlockingQueue<P>();
+	}
+	public void setWriteSelector(SelectorSimulator<P> _selector){
+		writeSelector = _selector; 
+	}
+
+
+	/* return state of the Queue */
+	abstract public boolean isReadable();
+	abstract public boolean isWritable();
+	abstract public boolean isAcceptable();
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/SelectionKeySimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,76 @@
+package rep.channel;
+
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+
+public class SelectionKeySimulator<P> extends SelectionKey{
+	
+	private int interestOpt;
+	private SelectableChannelSimulator<P> channel;
+	private int ready;
+	public Selector selector;
+
+	public SelectionKeySimulator(SelectableChannelSimulator<P> cs, int opt, Selector _selector) {
+		channel = cs;
+		interestOpt = opt;
+		selector = _selector;
+	}
+
+	public boolean isAble() {
+		if ( (interestOpt&OP_READ)!=0 && isReadable() )
+			return true;
+		else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() )
+			return true;
+		else if( (interestOpt&OP_WRITE)!=0 && isWritable() )
+			return true;
+		else
+			return false;
+	}
+
+	public void setFlag() {
+		ready = 0;
+		if(channel.isAcceptable()) ready |= OP_ACCEPT;
+		if(channel.isReadable()) ready |= OP_READ;
+		if(channel.isWritable()) ready |= OP_WRITE;
+	}
+
+	public SelectableChannelSimulator<P> channel() {
+		return channel;
+	}
+
+
+	@Override
+	public void cancel() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public int interestOps() {
+		// TODO Auto-generated method stub
+		return interestOpt;
+	}
+
+	@Override
+	public SelectionKey interestOps(int ops) {
+		interestOpt = ops;
+		return this;
+	}
+
+	@Override
+	public boolean isValid() {
+		return true;
+	}
+
+	@Override
+	public int readyOps() {
+		return ready;
+	}
+
+	@Override
+	public Selector selector() {
+		// TODO Auto-generated method stub
+		return selector;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/SelectorSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,117 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.TreeSet;
+//import java.util.Set; //書き直す?
+import java.util.Set;
+
+
+
+public class SelectorSimulator<P> extends Selector{
+	
+	private TreeSet<SelectionKey> keyList;
+	private TreeSet<SelectionKey> selectedKeys;
+	
+	public SelectorSimulator() {
+		// TODO Auto-generated constructor stub
+		keyList = new TreeSet<SelectionKey>();
+	}
+
+	@SuppressWarnings("unchecked")
+	public int select() throws IOException {
+		selectedKeys = new TreeSet<SelectionKey>();
+		
+		synchronized(this) {
+
+			while(selectedKeys.isEmpty()){
+				for(SelectionKey key : keyList){
+					if(((SelectionKeySimulator<P>) key).isAble())
+						selectedKeys.add(key);
+				}
+
+				if(selectedKeys.isEmpty())
+					try {
+						this.wait();
+					} catch (InterruptedException e) {
+						throw new IOException("Error, Selector was interrupted!");
+					}
+			}
+		}
+		return selectedKeys.size();
+	}
+	
+	public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){
+		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this);
+		keyList.add(key);
+		return key;
+	}
+	
+	public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
+		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this);
+		key.attach(handler);
+		keyList.add(key);
+		return key;
+	}
+	
+	public SelectionKey getKey(ChannelSimulator<P> channel){
+		for(SelectionKey key : keyList){
+			if(key.channel() == channel)
+				return key;
+		}
+		return null;
+	}
+
+	@Override
+	public void close() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public boolean isOpen() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public Set<SelectionKey> keys() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int select(long timeout) throws IOException {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public int selectNow() throws IOException {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public Selector wakeup() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Set<SelectionKey> selectedKeys() {
+		// TODO Auto-generated method stub
+		return (Set<SelectionKey>)selectedKeys;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/ServerChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,110 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+
+
+
+public class ServerChannelSimulator<P>extends SelectableChannelSimulator<P>{
+	protected NetworkSimulator<P> ns;
+	private int virtualIP;
+
+	/**  Constructors. */
+	public ServerChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> rselector){
+		super(null);
+		ns = _ns;
+		readSelector = rselector;
+		writeSelector = null;
+		qread = null;
+		qwrite = null;
+	}
+
+	/** Connecting methods */
+	// for servers.
+	public void bind(int ip){
+		virtualIP = ip;
+		ns.listen(ip, readSelector);
+	}
+
+	public ChannelSimulator<P> accept(){
+		ChannelSimulator<P> channel = ns.accept(virtualIP);
+		return channel;
+	}
+
+
+	/* state check methods for SelectionKeySimulator. */
+	public boolean isReadable() {
+		return false;
+	}
+	public boolean isWritable() {
+		return false;
+	}
+	public boolean isAcceptable() {
+		return ns.canAccept(virtualIP);
+	}
+
+	@Override
+	public Object blockingLock() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public boolean isBlocking() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isRegistered() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int validOps() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	protected void implCloseChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	public static ServerSocketChannel open() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/ServerChannelSimulatorImpl.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,50 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+
+public class ServerChannelSimulatorImpl extends REPServerSocketChannel{
+	
+
+protected ServerChannelSimulatorImpl(SelectorProvider provider) {
+		super(provider);
+	}
+
+
+	@Override
+	public ServerSocket socket() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+
+	@Override
+	public SocketChannel accept() throws IOException {
+		// TODO Auto-generated method stub
+		
+		return null;
+	}
+
+
+	@Override
+	protected void implCloseSelectableChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+
+	@Override
+	protected void implConfigureBlocking(boolean block) throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+	
+	public static REPServerSocketChannel open() {
+		new ServerChannelSimulator(null, null);
+		return null;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/ChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,118 @@
+package rep.simulator;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+
+
+public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{
+	//private BlockingQueue<P> qread;
+	//private BlockingQueue<P> qwrite;
+	//private SelectorSimulator<P> waitingSelector;
+	protected NetworkSimulator<P> ns;
+
+	/**  Constructors. */
+	public ChannelSimulator(NetworkSimulator<P> _ns){
+		this(_ns, null);
+	}
+	public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> _selector){
+		ns = _ns;
+		//ns = NetworkSimulator.singleton();  //どっちがいい?
+	}
+	public ChannelSimulator<P> createConjugatedChannel() {
+		ChannelSimulator<P> ret = new ChannelSimulator<P>(ns);
+		ret.qread=qwrite;
+		ret.qwrite=qread;
+		ret.readSelector=writeSelector;
+		ret.writeSelector=readSelector;
+		return ret;
+	}
+
+	/** Connecting methods */
+	// for clients.
+	public boolean connect(int ip){
+		return ns.connect(ip, this);
+	}
+	
+	public ChannelSimulator<P> accept(){
+		return null;
+	}
+
+	/* return state of the Queue(debug)  */
+	/*
+	public boolean readQisEmpty() {
+		return qread.isEmpty();
+	}
+	public boolean writeQisEmpty() {
+		return qwrite.isEmpty();
+	}
+	*/
+	
+	@Override
+	public boolean isAcceptable() {
+		return false;
+	}
+	@Override
+	public boolean isReadable() {
+		synchronized (qread){ 
+		return !qread.isEmpty();
+		}
+	}
+	@Override
+	public boolean isWritable() {
+		return true;
+	}
+
+	public SelectionKeySimulator<P> keyFor(SelectorSimulator<P> selector2) {
+		return (SelectionKeySimulator<P>) selector2.getKey(this);
+	}
+	@Override
+	public Object blockingLock() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public boolean isBlocking() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+	@Override
+	public boolean isRegistered() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	@Override
+	public int validOps() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+	@Override
+	protected void implCloseChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/NetworkSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,126 @@
+package rep.simulator;
+
+import java.util.LinkedList;
+
+
+public class NetworkSimulator<P> {
+	public static NetworkSimulator<?> ns;
+	synchronized public static <T> NetworkSimulator<T> singleton(){
+		if (ns==null)
+			ns = new NetworkSimulator<T>();
+		return (NetworkSimulator<T>) ns;
+		// NetworkSimulator<Obj> ns = NetworkSimulator.singleton(new NetworkSimulator<Obj>());
+	}
+
+	int logLevel=5;
+	/** Listening Servers. */
+	private LinkedList<ServerData<P>> serverList;
+
+	/** Constructor. */
+	public NetworkSimulator(){
+		serverList = new LinkedList<ServerData<P>>();
+		writeLog("construct Networksimulator", 1);
+		printAllState();
+	}
+
+		
+	
+	/*   */
+	synchronized public void listen(int ip, SelectorSimulator<P> selector) {
+		serverList.add(new ServerData<P>(ip, selector));
+		writeLog(Thread.currentThread(), "listen", 1);
+		printAllState();
+	}
+
+	synchronized public ChannelSimulator<P> accept(int ip) {
+		for (ServerData<P> sd: serverList){
+			if (sd.virtualIP!=ip) continue;
+			writeLog(Thread.currentThread(), "accepting..", 1);
+
+			ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove();
+			sd.establishedList.add(serverCH);
+
+			writeLog(Thread.currentThread(), "accepted", 1);
+			printAllState();
+			return serverCH;
+		}
+		return null;
+	}
+	synchronized public boolean canAccept(int ip){
+		for (ServerData<P> sd: serverList){
+			if (sd.virtualIP!=ip) continue;
+			return !sd.acceptWaitingList.isEmpty();
+		}
+		return false;
+	}
+
+	public boolean connect(int ip, ChannelSimulator<P> clientCH) {
+		ServerData<P> sd = null;
+		writeLog(Thread.currentThread(), "connecting..", 1);
+		synchronized (this){
+			for (ServerData<P> sd0: serverList){
+				if (sd0.virtualIP!=ip) continue;
+				
+				sd = sd0;
+			}	
+			if (sd==null) return false;
+
+			//ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector);
+			clientCH.createReadQ();
+			clientCH.createWriteQ();
+			clientCH.setWriteSelector(sd.selector);
+
+			ChannelSimulator<P> serverCH = clientCH.createConjugatedChannel();
+			sd.acceptWaitingList.add(serverCH);
+		}
+
+		synchronized (sd.selector) {
+			sd.selector.notifyAll();
+		}
+		writeLog(Thread.currentThread(), "connected", 1);
+		printAllState();
+		return true;
+	}
+
+	/** for DEBUG methods. */
+	synchronized void printAllState(){
+		writeLog("NetworkSimulator State:");
+		for (ServerData<P> sd: serverList){
+			writeLog("\tSessionManager(ip="+sd.virtualIP+"): ");
+			writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
+			writeLog("\testablishedList="+sd.establishedList.size());
+		}
+	}
+	
+	/** simulation log command */
+	synchronized public void writeLog(String log, int level){
+		if ( level<=logLevel )
+			System.out.println(log);
+		System.out.flush();
+	}
+	public void writeLog(String log){
+		writeLog(log, 0);
+	}
+	public void writeLog(Thread thr, String log, int level){
+		writeLog(thr.getName()+": "+log, level);
+	}
+	public void setLogLevel(int logLevel) {
+		this.logLevel = logLevel;
+	}
+
+
+}
+
+class ServerData<P> {
+	int virtualIP;
+	SelectorSimulator<P> selector;
+	LinkedList<ChannelSimulator<P>> acceptWaitingList;
+	LinkedList<ChannelSimulator<P>> establishedList;
+
+	ServerData(int ip, SelectorSimulator<P> _selector){
+		virtualIP = ip;
+		selector = _selector;
+		acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
+		establishedList = new LinkedList<ChannelSimulator<P>>();
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/REPSelector.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,27 @@
+package rep.simulator;
+
+import java.io.IOException;
+import java.nio.channels.Selector;
+
+public class REPSelector {
+
+	private static boolean isSimulator;
+	
+	
+
+	public static Selector open() throws IOException {
+		if(isSimulator){
+			return SelectorSimulator.open();
+		}else{
+			return Selector.open();
+		}
+	}
+
+
+
+	public static void setSimulation(boolean b) {
+		// TODO Auto-generated method stub
+		isSimulator = false;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/SelectableChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,72 @@
+package rep.simulator;
+
+import java.nio.channels.SelectableChannel;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+
+
+public abstract class SelectableChannelSimulator<P> extends SelectableChannel{
+	protected BlockingQueue<P> qread;
+	protected BlockingQueue<P> qwrite;
+	protected SelectorSimulator<P> writeSelector;
+	protected SelectorSimulator<P> readSelector;
+	
+	/* read from Queue.  */
+	public P read(){
+		try {
+			if(readSelector!=null)
+				synchronized (readSelector){
+					return qread.take();
+				}
+			else{
+				return qread.take();
+			}
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+	/* write to Queue.  */
+	public boolean write(P p){
+		try {
+			if (writeSelector!=null)
+				synchronized (writeSelector){
+					qwrite.put(p);
+					writeSelector.notifyAll();
+				}
+			else {
+				qwrite.put(p);
+			}
+			return true;
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return false;
+		}
+	}
+	public abstract ChannelSimulator<P> accept();
+	
+	/* accessor methods.  */
+	public BlockingQueue<P> getReadQ(){
+		return qread;
+	}
+	public BlockingQueue<P> getWriteQ(){
+		return qwrite;
+	}
+	public void createReadQ(){
+		qread = new LinkedBlockingQueue<P>();
+	}
+	public void createWriteQ(){
+		qwrite = new LinkedBlockingQueue<P>();
+	}
+	public void setWriteSelector(SelectorSimulator<P> _selector){
+		writeSelector = _selector; 
+	}
+
+
+	/* return state of the Queue */
+	abstract public boolean isReadable();
+	abstract public boolean isWritable();
+	abstract public boolean isAcceptable();
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/SelectionKeySimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,76 @@
+package rep.simulator;
+
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+
+public class SelectionKeySimulator<P> extends SelectionKey{
+	
+	private int interestOpt;
+	private SelectableChannelSimulator<P> channel;
+	private int ready;
+	public Selector selector;
+
+	public SelectionKeySimulator(SelectableChannelSimulator<P> cs, int opt, Selector _selector) {
+		channel = cs;
+		interestOpt = opt;
+		selector = _selector;
+	}
+
+	public boolean isAble() {
+		if ( (interestOpt&OP_READ)!=0 && isReadable() )
+			return true;
+		else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() )
+			return true;
+		else if( (interestOpt&OP_WRITE)!=0 && isWritable() )
+			return true;
+		else
+			return false;
+	}
+
+	public void setFlag() {
+		ready = 0;
+		if(channel.isAcceptable()) ready |= OP_ACCEPT;
+		if(channel.isReadable()) ready |= OP_READ;
+		if(channel.isWritable()) ready |= OP_WRITE;
+	}
+
+	public SelectableChannelSimulator<P> channel() {
+		return channel;
+	}
+
+
+	@Override
+	public void cancel() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public int interestOps() {
+		// TODO Auto-generated method stub
+		return interestOpt;
+	}
+
+	@Override
+	public SelectionKey interestOps(int ops) {
+		interestOpt = ops;
+		return this;
+	}
+
+	@Override
+	public boolean isValid() {
+		return true;
+	}
+
+	@Override
+	public int readyOps() {
+		return ready;
+	}
+
+	@Override
+	public Selector selector() {
+		// TODO Auto-generated method stub
+		return selector;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/SelectorSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,117 @@
+package rep.simulator;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.TreeSet;
+//import java.util.Set; //書き直す?
+import java.util.Set;
+
+
+
+public class SelectorSimulator<P> extends Selector{
+	
+	private TreeSet<SelectionKey> keyList;
+	private TreeSet<SelectionKey> selectedKeys;
+	
+	public SelectorSimulator() {
+		// TODO Auto-generated constructor stub
+		keyList = new TreeSet<SelectionKey>();
+	}
+
+	@SuppressWarnings("unchecked")
+	public int select() throws IOException {
+		selectedKeys = new TreeSet<SelectionKey>();
+		
+		synchronized(this) {
+
+			while(selectedKeys.isEmpty()){
+				for(SelectionKey key : keyList){
+					if(((SelectionKeySimulator<P>) key).isAble())
+						selectedKeys.add(key);
+				}
+
+				if(selectedKeys.isEmpty())
+					try {
+						this.wait();
+					} catch (InterruptedException e) {
+						throw new IOException("Error, Selector was interrupted!");
+					}
+			}
+		}
+		return selectedKeys.size();
+	}
+	
+	public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){
+		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this);
+		keyList.add(key);
+		return key;
+	}
+	
+	public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
+		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this);
+		key.attach(handler);
+		keyList.add(key);
+		return key;
+	}
+	
+	public SelectionKey getKey(ChannelSimulator<P> channel){
+		for(SelectionKey key : keyList){
+			if(key.channel() == channel)
+				return key;
+		}
+		return null;
+	}
+
+	@Override
+	public void close() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public boolean isOpen() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public Set<SelectionKey> keys() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int select(long timeout) throws IOException {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public int selectNow() throws IOException {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public Selector wakeup() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Set<SelectionKey> selectedKeys() {
+		// TODO Auto-generated method stub
+		return (Set<SelectionKey>)selectedKeys;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/simulator/ServerChannelSimulator.java	Wed Aug 27 17:21:25 2008 +0900
@@ -0,0 +1,103 @@
+package rep.simulator;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+
+
+
+public class ServerChannelSimulator<P>extends SelectableChannelSimulator<P>{
+	protected NetworkSimulator<P> ns;
+	private int virtualIP;
+
+	/**  Constructors. */
+	public ServerChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> rselector){
+		ns = _ns;
+		readSelector = rselector;
+		writeSelector = null;
+		qread = null;
+		qwrite = null;
+	}
+
+	/** Connecting methods */
+	// for servers.
+	public void bind(int ip){
+		virtualIP = ip;
+		ns.listen(ip, readSelector);
+	}
+
+	public ChannelSimulator<P> accept(){
+		ChannelSimulator<P> channel = ns.accept(virtualIP);
+		return channel;
+	}
+
+
+	/* state check methods for SelectionKeySimulator. */
+	public boolean isReadable() {
+		return false;
+	}
+	public boolean isWritable() {
+		return false;
+	}
+	public boolean isAcceptable() {
+		return ns.canAccept(virtualIP);
+	}
+
+	@Override
+	public Object blockingLock() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public boolean isBlocking() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isRegistered() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int validOps() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	protected void implCloseChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+}