Mercurial > hg > RemoteEditor > REPSessionManager
changeset 149:b3fa37915310
*** empty log message ***
author | kono |
---|---|
date | Thu, 28 Aug 2008 14:39:08 +0900 |
parents | 6a5fe529b192 |
children | dce41e665edb |
files | rep/simulator/ChannelSimulator.java rep/simulator/NetworkSimulator.java rep/simulator/REPSelector.java rep/simulator/SelectableChannelSimulator.java rep/simulator/SelectionKeySimulator.java rep/simulator/SelectorSimulator.java rep/simulator/ServerChannelSimulator.java |
diffstat | 7 files changed, 0 insertions(+), 639 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/simulator/ChannelSimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,118 +0,0 @@ -package rep.simulator; - -import java.io.IOException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; - - -public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{ - //private BlockingQueue<P> qread; - //private BlockingQueue<P> qwrite; - //private SelectorSimulator<P> waitingSelector; - protected NetworkSimulator<P> ns; - - /** Constructors. */ - public ChannelSimulator(NetworkSimulator<P> _ns){ - this(_ns, null); - } - public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> _selector){ - ns = _ns; - //ns = NetworkSimulator.singleton(); //どっちがいい? - } - public ChannelSimulator<P> createConjugatedChannel() { - ChannelSimulator<P> ret = new ChannelSimulator<P>(ns); - ret.qread=qwrite; - ret.qwrite=qread; - ret.readSelector=writeSelector; - ret.writeSelector=readSelector; - return ret; - } - - /** Connecting methods */ - // for clients. - public boolean connect(int ip){ - return ns.connect(ip, this); - } - - public ChannelSimulator<P> accept(){ - return null; - } - - /* return state of the Queue(debug) */ - /* - public boolean readQisEmpty() { - return qread.isEmpty(); - } - public boolean writeQisEmpty() { - return qwrite.isEmpty(); - } - */ - - @Override - public boolean isAcceptable() { - return false; - } - @Override - public boolean isReadable() { - synchronized (qread){ - return !qread.isEmpty(); - } - } - @Override - public boolean isWritable() { - return true; - } - - public SelectionKeySimulator<P> keyFor(SelectorSimulator<P> selector2) { - return (SelectionKeySimulator<P>) selector2.getKey(this); - } - @Override - public Object blockingLock() { - // TODO Auto-generated method stub - return null; - } - @Override - public SelectableChannel configureBlocking(boolean block) throws IOException { - // TODO Auto-generated method stub - return null; - } - @Override - public boolean isBlocking() { - // TODO Auto-generated method stub - return false; - } - @Override - public boolean isRegistered() { - // TODO Auto-generated method stub - return false; - } - @Override - public SelectionKey keyFor(Selector sel) { - // TODO Auto-generated method stub - return null; - } - @Override - public SelectorProvider provider() { - // TODO Auto-generated method stub - return null; - } - @Override - public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { - // TODO Auto-generated method stub - return null; - } - @Override - public int validOps() { - // TODO Auto-generated method stub - return 0; - } - @Override - protected void implCloseChannel() throws IOException { - // TODO Auto-generated method stub - - } - -}
--- a/rep/simulator/NetworkSimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -package rep.simulator; - -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<P> 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<P> selector; - LinkedList<ChannelSimulator<P>> acceptWaitingList; - LinkedList<ChannelSimulator<P>> establishedList; - - ServerData(int ip, SelectorSimulator<P> _selector){ - virtualIP = ip; - selector = _selector; - acceptWaitingList = new LinkedList<ChannelSimulator<P>>(); - establishedList = new LinkedList<ChannelSimulator<P>>(); - } -} \ No newline at end of file
--- a/rep/simulator/REPSelector.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -package rep.simulator; - -import java.io.IOException; -import java.nio.channels.Selector; - -public class REPSelector { - - private static boolean isSimulator; - - - - public static Selector open() throws IOException { - if(isSimulator){ - return SelectorSimulator.open(); - }else{ - return Selector.open(); - } - } - - - - public static void setSimulation(boolean b) { - // TODO Auto-generated method stub - isSimulator = false; - } - -}
--- a/rep/simulator/SelectableChannelSimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -package rep.simulator; - -import java.nio.channels.SelectableChannel; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - - - -public abstract class SelectableChannelSimulator<P> extends SelectableChannel{ - protected BlockingQueue<P> qread; - protected BlockingQueue<P> qwrite; - protected SelectorSimulator<P> writeSelector; - protected SelectorSimulator<P> readSelector; - - /* read from Queue. */ - public P read(){ - try { - if(readSelector!=null) - synchronized (readSelector){ - return qread.take(); - } - else{ - return qread.take(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } - } - /* write to Queue. */ - public boolean write(P p){ - try { - if (writeSelector!=null) - synchronized (writeSelector){ - qwrite.put(p); - writeSelector.notifyAll(); - } - else { - qwrite.put(p); - } - return true; - } catch (InterruptedException e) { - e.printStackTrace(); - return false; - } - } - public abstract ChannelSimulator<P> accept(); - - /* accessor methods. */ - public BlockingQueue<P> getReadQ(){ - return qread; - } - public BlockingQueue<P> getWriteQ(){ - return qwrite; - } - public void createReadQ(){ - qread = new LinkedBlockingQueue<P>(); - } - public void createWriteQ(){ - qwrite = new LinkedBlockingQueue<P>(); - } - public void setWriteSelector(SelectorSimulator<P> _selector){ - writeSelector = _selector; - } - - - /* return state of the Queue */ - abstract public boolean isReadable(); - abstract public boolean isWritable(); - abstract public boolean isAcceptable(); - -}
--- a/rep/simulator/SelectionKeySimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -package rep.simulator; - -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; - -public class SelectionKeySimulator<P> extends SelectionKey{ - - private int interestOpt; - private SelectableChannelSimulator<P> channel; - private int ready; - public Selector selector; - - public SelectionKeySimulator(SelectableChannelSimulator<P> cs, int opt, Selector _selector) { - channel = cs; - interestOpt = opt; - selector = _selector; - } - - public boolean isAble() { - if ( (interestOpt&OP_READ)!=0 && isReadable() ) - return true; - else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() ) - return true; - else if( (interestOpt&OP_WRITE)!=0 && isWritable() ) - return true; - else - return false; - } - - public void setFlag() { - ready = 0; - if(channel.isAcceptable()) ready |= OP_ACCEPT; - if(channel.isReadable()) ready |= OP_READ; - if(channel.isWritable()) ready |= OP_WRITE; - } - - public SelectableChannelSimulator<P> channel() { - return channel; - } - - - @Override - public void cancel() { - // TODO Auto-generated method stub - - } - - @Override - public int interestOps() { - // TODO Auto-generated method stub - return interestOpt; - } - - @Override - public SelectionKey interestOps(int ops) { - interestOpt = ops; - return this; - } - - @Override - public boolean isValid() { - return true; - } - - @Override - public int readyOps() { - return ready; - } - - @Override - public Selector selector() { - // TODO Auto-generated method stub - return selector; - } - -}
--- a/rep/simulator/SelectorSimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -package rep.simulator; - -import java.io.IOException; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; -import java.util.ArrayList; -import java.util.Set; -import java.util.TreeSet; -//import java.util.Set; //書き直す? -import java.util.Set; - - - -public class SelectorSimulator<P> extends Selector{ - - private TreeSet<SelectionKey> keyList; - private TreeSet<SelectionKey> selectedKeys; - - public SelectorSimulator() { - // TODO Auto-generated constructor stub - keyList = new TreeSet<SelectionKey>(); - } - - @SuppressWarnings("unchecked") - public int select() throws IOException { - selectedKeys = new TreeSet<SelectionKey>(); - - synchronized(this) { - - while(selectedKeys.isEmpty()){ - for(SelectionKey key : keyList){ - if(((SelectionKeySimulator<P>) key).isAble()) - selectedKeys.add(key); - } - - if(selectedKeys.isEmpty()) - try { - this.wait(); - } catch (InterruptedException e) { - throw new IOException("Error, Selector was interrupted!"); - } - } - } - return selectedKeys.size(); - } - - public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){ - SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this); - keyList.add(key); - return key; - } - - public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){ - SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this); - key.attach(handler); - keyList.add(key); - return key; - } - - public SelectionKey getKey(ChannelSimulator<P> channel){ - for(SelectionKey key : keyList){ - if(key.channel() == channel) - return key; - } - return null; - } - - @Override - public void close() throws IOException { - // TODO Auto-generated method stub - - } - - @Override - public boolean isOpen() { - // TODO Auto-generated method stub - return false; - } - - @Override - public Set<SelectionKey> keys() { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectorProvider provider() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int select(long timeout) throws IOException { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int selectNow() throws IOException { - // TODO Auto-generated method stub - return 0; - } - - @Override - public Selector wakeup() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Set<SelectionKey> selectedKeys() { - // TODO Auto-generated method stub - return (Set<SelectionKey>)selectedKeys; - } - -}
--- a/rep/simulator/ServerChannelSimulator.java Thu Aug 28 00:11:30 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ -package rep.simulator; - -import java.io.IOException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; - - - -public class ServerChannelSimulator<P>extends SelectableChannelSimulator<P>{ - protected NetworkSimulator<P> ns; - private int virtualIP; - - /** Constructors. */ - public ServerChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> rselector){ - ns = _ns; - readSelector = rselector; - writeSelector = null; - qread = null; - qwrite = null; - } - - /** Connecting methods */ - // for servers. - public void bind(int ip){ - virtualIP = ip; - ns.listen(ip, readSelector); - } - - public ChannelSimulator<P> accept(){ - ChannelSimulator<P> channel = ns.accept(virtualIP); - return channel; - } - - - /* state check methods for SelectionKeySimulator. */ - public boolean isReadable() { - return false; - } - public boolean isWritable() { - return false; - } - public boolean isAcceptable() { - return ns.canAccept(virtualIP); - } - - @Override - public Object blockingLock() { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectableChannel configureBlocking(boolean block) throws IOException { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isBlocking() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isRegistered() { - // TODO Auto-generated method stub - return false; - } - - @Override - public SelectionKey keyFor(Selector sel) { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectorProvider provider() { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { - // TODO Auto-generated method stub - return null; - } - - @Override - public int validOps() { - // TODO Auto-generated method stub - return 0; - } - - @Override - protected void implCloseChannel() throws IOException { - // TODO Auto-generated method stub - - } - -}