view rep/channel/NetworkSimulator.java @ 284:90965a3bd4f3

editor simulator
author kono
date Sun, 28 Sep 2008 14:16:13 +0900
parents b864d2f60102
children c5be84d53c7f
line wrap: on
line source

package rep.channel;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.LinkedList;

public class NetworkSimulator<P> {
	public static NetworkSimulator<?> ns;
	

	public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>();
	public int ipcount = 1;
	public REPLogger logger;
	
	@SuppressWarnings("unchecked")  // <?> から <T> へのキャストのため. 
	public static <T> NetworkSimulator<T> singleton(){
		// double check singleton
		if (ns==null)
			synchronized (NetworkSimulator.class) {
				if (ns==null) 
					ns = new NetworkSimulator<T>();
			}
		return (NetworkSimulator<T>) ns;
	}

	int logLevel=5;
	/** Listening Servers. */
	private LinkedList<ServerData<P>> serverList;

	/** Constructor. */
	public NetworkSimulator(){
		serverList = new LinkedList<ServerData<P>>();
		logger = REPLogger.singleton();
		logger.writeLog("construct Networksimulator", 1);
		// printAllState();
	}
	
	/*   */
	synchronized public void listen(InetSocketAddress ip, ServerChannelSimulator<P> scs) {
		serverList.add(new ServerData<P>(ip, scs));
		logger.writeLog("listen", 1);
		printAllState();
	}

	synchronized public boolean connect(InetSocketAddress ip, ChannelSimulator<P> CHclient) {
		logger.writeLog("connecting..", 1);
		for (ServerData<P> sd0: serverList){
			// ANY address (0.0.0.0/0.0.0.0) should be considered.
			if (sd0.IP.getAddress().isAnyLocalAddress()) {
				if (sd0.IP.getPort() != ip.getPort()) continue;
				// we have to check, ip is really reachable to sd0 server,
				// but this simulator has no notion of host. To distinguish,
				// use different port address. 
			} else if (!sd0.IP.equals(ip)) continue;

			ChannelSimulator<P> CHserver = new ChannelSimulator<P>();
			CHserver.setOtherEnd(CHclient);
			CHclient.setOtherEnd(CHserver);

			sd0.connectedListS.add(CHserver);
			sd0.connectedListC.add(CHclient);
			sd0.scs.enQ(CHserver);

			logger.writeLog("connected", 1);
			printAllState();
			return true;
		}
		return false;
	}

	/** for DEBUG methods. */
	void printAllState(){
		String log = "NetworkSimulator State:";
		for (ServerData<P> sd: serverList){
			log += "\tSessionManager(ip="+sd.IP.toString()+"): ";
			log += channelList(sd.connectedListC);
		}
		logger.writeLog(log);
	}
	
	String channelList(LinkedList<ChannelSimulator<P>> list){
		String tmp = "";
		for (ChannelSimulator<P> ch: list){
			tmp += ch.toString()+" ";
		}
		return "\t"+tmp;
	}



	public int nslookup(SocketAddress semaIP) {
		Integer ip;
		if ((ip=namedb.get(semaIP))==null) {
			namedb.put(semaIP, (ip=ipcount++));
		}
		return ip;
	}
	

}

class ServerData<P> {
	//int virtualIP;
	InetSocketAddress IP;
	//SelectorSimulator<P> selector;
	ServerChannelSimulator<P> scs;
	LinkedList<ChannelSimulator<P>> connectedListS;
	LinkedList<ChannelSimulator<P>> connectedListC;

	ServerData(InetSocketAddress ip, ServerChannelSimulator<P> _scs){
		IP = ip;
		//selector = _selector;
		scs = _scs;
		connectedListS = new LinkedList<ChannelSimulator<P>>();
		connectedListC = new LinkedList<ChannelSimulator<P>>();
	}
}