Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/NetworkSimulator.java @ 144:0bf7f8d0f5f7
*** empty log message ***
author | pin |
---|---|
date | Wed, 27 Aug 2008 22:55:43 +0900 |
parents | 785a3e8ea858 |
children | 4ae9da03cf1e |
line wrap: on
line source
package rep.channel; 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 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 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>>(); } }