view rep/simulator/NetworkSimulator.java @ 123:5b1a0574b406 add-simulator

*** empty log message ***
author pin
date Wed, 27 Aug 2008 17:21:25 +0900
parents
children
line wrap: on
line source

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>>();
	}
}