Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/NetworkSimulator.java @ 281:c3969dd625b2
GUIless test routine.
author | kono |
---|---|
date | Sat, 27 Sep 2008 22:55:13 +0900 |
parents | e72e0eae1261 |
children | c410eda661e8 |
line wrap: on
line source
package rep.channel; 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(SocketAddress ip, ServerChannelSimulator<P> scs) { serverList.add(new ServerData<P>(ip, scs)); logger.writeLog("listen", 1); printAllState(); } synchronized public boolean connect(SocketAddress 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.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. */ synchronized void printAllState(){ synchronized (logger){ logger.writeLog("NetworkSimulator State:"); for (ServerData<P> sd: serverList){ logger.writeLog("\tSessionManager(ip="+sd.IP.toString()+"): "); //writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size()); printChannelList(sd.connectedListC); //writeLog("\testablishedList="+sd.establishedList.size()); } } } synchronized void printChannelList(LinkedList<ChannelSimulator<P>> list){ String tmp = ""; for (ChannelSimulator<P> ch: list){ tmp += ch.toString()+" "; } logger.writeLog("\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; SocketAddress IP; //SelectorSimulator<P> selector; ServerChannelSimulator<P> scs; LinkedList<ChannelSimulator<P>> connectedListS; LinkedList<ChannelSimulator<P>> connectedListC; ServerData(SocketAddress ip, ServerChannelSimulator<P> _scs){ IP = ip; //selector = _selector; scs = _scs; connectedListS = new LinkedList<ChannelSimulator<P>>(); connectedListC = new LinkedList<ChannelSimulator<P>>(); } }