changeset 172:a913949a0dd9

channelsimulations version2
author kent
date Sat, 30 Aug 2008 01:55:16 +0900
parents c61a52e12161
children b4f2c0aeb474
files src/pathfinder/mergetest/channels2/NetworkSimulator.java src/pathfinder/mergetest/channels2/SelectableChannelSimulator.java src/pathfinder/mergetest/channels2/SelectionKeySimulator.java src/pathfinder/mergetest/channels2/SelectorSimulator.java src/pathfinder/mergetest/channels2/ServerChannelSimulator.java src/pathfinder/mergetest/channels2/SocketChannelSimulator.java
diffstat 6 files changed, 450 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/NetworkSimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,99 @@
+package pathfinder.mergetest.channels2;
+
+import java.net.SocketAddress;
+import java.util.LinkedList;
+
+
+public class NetworkSimulator<P> {
+	public static NetworkSimulator<?> ns;
+	/** Listening Servers. */
+	private LinkedList<ServerData<P>> serverList;
+	/** log Level */
+	int logLevel=5;
+
+	/** Constructor. */
+	private NetworkSimulator(){
+		serverList = new LinkedList<ServerData<P>>();
+		writeLog("Networksimulator was constructed.", 1);
+		printAllState();
+	}
+
+	synchronized public static <T> NetworkSimulator<T> singleton(){
+		if (ns==null)
+			ns = new NetworkSimulator<T>();
+		return (NetworkSimulator<T>) ns;
+	}
+
+	
+	/*   */
+	synchronized public void listen(Object ip, ServerChannelSimulator<P> _scs) {
+		serverList.add(new ServerData<P>(ip, _scs));
+		writeLog("listen", 1);
+		printAllState();
+	}
+
+	synchronized public boolean connect(Object ip, SocketChannelSimulator<P> CHclient) {
+		for (ServerData<P> sd0: serverList){
+			if (!sd0.virtualIP.equals(ip)) continue;
+
+			SocketChannelSimulator<P> CHserver = SocketChannelSimulator.<P>open();
+			CHserver.setOtherEnd(CHclient);
+			CHclient.setOtherEnd(CHserver);
+
+			sd0.connectedListS.add(CHserver);
+			sd0.connectedListC.add(CHclient);
+			sd0.scs.enQ(CHserver);
+
+			printAllState();
+			return true;
+		}
+		return false;
+	}
+
+	/** for DEBUG methods. */
+	synchronized void printAllState(){
+		writeLog("NetworkSimulator State:");
+		for (ServerData<P> sd: serverList){
+			writeLog("\tSessionManager(ip="+sd.virtualIP.toString()+"): ");
+			//writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
+			printChannelList(sd.connectedListC);
+			//writeLog("\testablishedList="+sd.establishedList.size());
+		}
+	}
+	synchronized void printChannelList(LinkedList<SocketChannelSimulator<P>> list){
+		String tmp = "";
+		for (SocketChannelSimulator<P> ch: list){
+			tmp += ch.toString()+" ";
+		}
+		writeLog("\t"+tmp);
+	}
+	
+	/** simulation log command */
+	synchronized public void writeLog(String log, int level){
+		if ( level<=logLevel )
+			System.out.println(Thread.currentThread().getName()+": "+log);
+		System.out.flush();
+	}
+	public void writeLog(String log){
+		writeLog(log, 0);
+	}
+	public void setLogLevel(int logLevel) {
+		this.logLevel = logLevel;
+	}
+
+
+}
+
+class ServerData<P> {
+	Object virtualIP;
+	ServerChannelSimulator<P> scs;
+	LinkedList<SocketChannelSimulator<P>> connectedListS;
+	LinkedList<SocketChannelSimulator<P>> connectedListC;
+
+	ServerData(Object ip, ServerChannelSimulator<P> _scs){
+		virtualIP = ip;
+		scs = _scs;
+		connectedListC = new LinkedList<SocketChannelSimulator<P>>();
+		connectedListS = new LinkedList<SocketChannelSimulator<P>>();
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/SelectableChannelSimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,31 @@
+package pathfinder.mergetest.channels2;
+
+
+
+public abstract class SelectableChannelSimulator {
+	protected Object lock;
+	
+	protected SelectableChannelSimulator(){
+		lock = new Object();
+	}
+
+	public SelectionKeySimulator register(SelectorSimulator selector, int opt){
+		Object tmp=lock;
+		synchronized (tmp){
+			lock =  selector;
+		}
+		return selector.register(this, opt);
+	}
+	
+	abstract boolean isBlocking();
+
+	abstract SelectionKeySimulator keyFor(SelectorSimulator sel);
+	abstract boolean isRegistered();
+	abstract SelectionKeySimulator register(SelectorSimulator sel, int ops, Object att);
+	
+	/* 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/src/pathfinder/mergetest/channels2/SelectionKeySimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,53 @@
+package pathfinder.mergetest.channels2;
+
+public class SelectionKeySimulator {
+
+	public static final int OP_READ = 0x01;
+	public static final int OP_ACCEPT = 0x02;
+	public static final int OP_WRITE = 0x04;
+	
+	private int interestOpt;
+	private SelectableChannelSimulator channel;
+	private Object attachment;
+
+	public SelectionKeySimulator(SelectableChannelSimulator cs, int opt) {
+		channel = cs;
+		interestOpt = opt;
+	}
+
+	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 boolean isAcceptable() {
+		return channel.isAcceptable();
+	}
+
+	public boolean isReadable() {
+		return channel.isReadable();
+	}
+	public boolean isWritable() {
+		return channel.isWritable();
+	}
+
+	public SelectableChannelSimulator channel() {
+		return channel;
+	}
+
+	public Object attachment() {
+		return attachment;
+	}
+
+	public void attach(Object handler) {
+		attachment = handler;
+	}
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/SelectorSimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,59 @@
+package pathfinder.mergetest.channels2;
+
+import java.io.IOException;
+import java.util.ArrayList;
+//import java.util.Set; //書き直す?
+
+
+
+public class SelectorSimulator {
+
+	private ArrayList<SelectionKeySimulator> keyList;
+	private ArrayList<SelectionKeySimulator> selectedKeys;
+	
+	public SelectorSimulator() {
+		// TODO Auto-generated constructor stub
+		keyList = new ArrayList<SelectionKeySimulator>();
+	}
+
+	public int select() throws IOException {
+		selectedKeys = new ArrayList<SelectionKeySimulator>();
+		
+		synchronized(this) {
+
+			while(selectedKeys.isEmpty()){
+				for(SelectionKeySimulator key : keyList){
+					if(key.isAble())
+						selectedKeys.add(key);
+				}
+
+				if(selectedKeys.isEmpty())
+					try {
+						this.wait();
+					} catch (InterruptedException e) {
+						throw new IOException("Error, Selector was interrupted!");
+					}
+			}
+		}
+		return selectedKeys.size();
+	}
+	
+	SelectionKeySimulator register(SelectableChannelSimulator cs, int opt){
+		SelectionKeySimulator key = new SelectionKeySimulator(cs, opt);
+		keyList.add(key);
+		return key;
+	}
+	
+	SelectionKeySimulator register(SelectableChannelSimulator cs, int opt, Object handler){
+		SelectionKeySimulator key = new SelectionKeySimulator(cs, opt);
+		key.attach(handler);
+		keyList.add(key);
+		return key;
+	}
+
+	public ArrayList<SelectionKeySimulator> selectedKeys() {
+		
+		return selectedKeys;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/ServerChannelSimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,92 @@
+package pathfinder.mergetest.channels2;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.Queue;
+
+
+
+public class ServerChannelSimulator<P> extends SelectableChannelSimulator{
+	protected NetworkSimulator<P> ns;
+	protected Queue<SocketChannelSimulator<P>> acceptQ;
+	protected Object virtualIP;
+	protected boolean isBlocking=true;
+	protected SelectionKeySimulator key;
+
+	/**  Constructors. */
+	private ServerChannelSimulator(){
+		super();
+		ns = NetworkSimulator.singleton();
+		acceptQ = new LinkedList<SocketChannelSimulator<P>>();
+	}
+
+	public static <T> ServerChannelSimulator<T> open(){
+		return new ServerChannelSimulator<T>();
+	}
+
+	/** Connecting methods */
+	public void bind(Object ip){
+		virtualIP = ip;
+		ns.listen(ip, this);
+	}
+
+	public SocketChannelSimulator<P> accept() throws IOException{ // Blocking
+		SocketChannelSimulator<P> tmp;
+		synchronized (lock){
+			while ( (tmp=acceptQ.poll())==null && isBlocking ) {
+				try {
+					lock.wait();
+				} catch (InterruptedException e) {
+					throw new IOException();
+				}
+			}
+		}
+		return tmp;
+	}
+	
+	void enQ(SocketChannelSimulator<P> scs){  // for NetworkSimulator
+		synchronized (lock){
+			acceptQ.offer(scs);
+			lock.notifyAll();
+		}
+	}
+
+	/* state check methods for SelectionKeySimulator. */
+	public boolean isReadable() {
+		return false;
+	}
+	public boolean isWritable() {
+		return false;
+	}
+	public boolean isAcceptable() {
+		synchronized (lock){
+			return !acceptQ.isEmpty();
+		}
+	}
+
+	@Override
+	public SelectionKeySimulator keyFor(SelectorSimulator sel) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	boolean isBlocking() {
+		return isBlocking;
+	}
+
+	@Override
+	boolean isRegistered() {
+		return (lock instanceof SelectorSimulator);
+	}
+
+	@Override
+	public SelectionKeySimulator register(SelectorSimulator sel, int opt, Object att) {
+		synchronized (lock){
+			lock = sel;
+			key = sel.register(this, opt, att);
+			return key;
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/SocketChannelSimulator.java	Sat Aug 30 01:55:16 2008 +0900
@@ -0,0 +1,116 @@
+package pathfinder.mergetest.channels2;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.io.IOException;
+import java.nio.channels.NotYetConnectedException;
+
+public class SocketChannelSimulator<P> extends SelectableChannelSimulator{
+	protected NetworkSimulator<P> ns;
+	protected Queue<P> readQ;
+	protected SocketChannelSimulator<P> otherEnd;
+	boolean isBlocking=true;
+	protected SelectionKeySimulator key;
+	private String ownerName;
+
+	/**  Constructors. */
+	private SocketChannelSimulator(){
+		super();
+		ns = NetworkSimulator.singleton();
+		readQ = new LinkedList<P>();
+		lock = new Object();
+		otherEnd = null;
+		key = null;
+		ownerName = Thread.currentThread().getName(); 
+	}
+	
+	public static <T> SocketChannelSimulator<T> open(){
+		return new SocketChannelSimulator<T>();
+	}
+
+	/** read from own Queue. 
+	 * @throws IOException */
+	public P read() throws IOException{ // Blocking
+		P tmp;
+		synchronized (lock){
+			while ( (tmp=readQ.poll())==null && isBlocking ) {
+				try {
+					lock.wait();
+				} catch (InterruptedException e) {
+					throw new IOException();
+				}
+			}
+		}
+		return tmp;
+	}
+
+	/** write to other end Queue.  */
+	public void write(P p){
+		if (otherEnd==null) throw new NotYetConnectedException();
+		otherEnd.enQ(p);
+	}
+
+	/** other end Channel enqueue P to own queue using this method. */
+	protected void enQ(P p){
+		synchronized (lock){
+			readQ.offer(p);
+			lock.notifyAll();
+		}
+	}
+	
+	void setOtherEnd(SocketChannelSimulator<P> oe){   // for same package
+		otherEnd = oe;
+	}
+	
+
+	/** Connecting methods */
+	// for clients.
+	public boolean connect(Object ip){
+		return ns.connect(ip, this);
+	}
+	
+	@Override
+	public boolean isAcceptable() {
+		return false;
+	}
+	@Override
+	public boolean isReadable() {
+		synchronized (lock){
+			return !readQ.isEmpty();
+		}
+	}
+	@Override
+	public boolean isWritable() {
+		return true;
+	}
+
+	@Override
+	boolean isBlocking() {
+		return isBlocking;
+	}
+
+	@Override
+	boolean isRegistered() {
+		return (lock instanceof SelectorSimulator);
+	}
+
+	@Override
+	SelectionKeySimulator keyFor(SelectorSimulator sel) {
+		return key;
+	}
+
+	@Override
+	public SelectionKeySimulator register(SelectorSimulator sel, int opt, Object att) {
+		synchronized (lock){
+			lock = sel;
+			key = sel.register(this, opt, att);
+			return key;
+		}
+	}
+
+	public String toString(){
+		return "ChannelSimulator ("+ownerName+")";  
+	}
+
+
+}