view rep/channel/NetworkSimulator.java @ 436:5e3532db2e07

fix enQ
author one
date Wed, 01 Sep 2010 11:43:21 +0900
parents edb373aa421e
children
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 {
	// we don't use <P> because we need singleton. 
	public static NetworkSimulator ns = new NetworkSimulator();

	public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>();
	public int ipcount = 1;
	public REPLogger logger;
	
	public static NetworkSimulator singleton() {
		// singleton pattern may used here, but it has a little cost.
		return ns;
	}

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

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

	synchronized public boolean connect(InetSocketAddress ip, ChannelSimulator<?> CHclient) {
		logger.writeLog("connecting..", 1);
		for (ServerData 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<?> CHserver = new ChannelSimulator<?>();
			ChannelSimulator<?> CHserver = CHclient.newChannel();
			CHserver.setOtherEnd1(CHclient);
			CHclient.setOtherEnd1(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 sd: serverList){
			log += "\tSessionManager(ip="+sd.IP.toString()+"): ";
			log += channelList(sd.connectedListC);
		}
		logger.writeLog(log);
	}
	
	private String channelList(LinkedList<ChannelSimulator<?>> list){
		String tmp = "";
		for (ChannelSimulator<?> ch: list){
			tmp += ch.toString()+" ";
		}
		return "\t"+tmp;
	}



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

}

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

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