Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/NetworkSimulator.java @ 162:2bd3264abf55
*** empty log message ***
author | pin |
---|---|
date | Thu, 28 Aug 2008 16:54:54 +0900 |
parents | 31334767e65d |
children | 72252e970a8b |
line wrap: on
line source
package rep.channel; import java.net.SocketAddress; import java.util.HashMap; import java.util.LinkedList; public class NetworkSimulator<P> extends REPLogger { public static NetworkSimulator<?> ns; public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>(); public int ipcount = 1; public static <T> NetworkSimulator<?> singleton(){ // double check singleton if (ns==null) synchronized (NetworkSimulator.class) { if (ns==null) ns = new NetworkSimulator<T>(); } return ns; } 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 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()); } } 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; SelectorSimulator selector; LinkedList<ChannelSimulator<P>> acceptWaitingList; LinkedList<ChannelSimulator<P>> establishedList; ServerData(int ip, SelectorSimulator _selector){ virtualIP = ip; selector = _selector; acceptWaitingList = new LinkedList<ChannelSimulator<P>>(); establishedList = new LinkedList<ChannelSimulator<P>>(); } }