# HG changeset patch # User pin # Date 1219825285 -32400 # Node ID 5b1a0574b4064c2b1cf4f54d31d784282c41fc08 # Parent 790c8dd42a7bdc364284c08a3289be6981f6e7e5 *** empty log message *** diff -r 790c8dd42a7b -r 5b1a0574b406 rep/SessionManager.java --- a/rep/SessionManager.java Tue Aug 26 16:33:47 2008 +0900 +++ b/rep/SessionManager.java Wed Aug 27 17:21:25 2008 +0900 @@ -16,6 +16,8 @@ import java.util.LinkedList; import java.util.StringTokenizer; +import rep.channel.REPServerSocketChannel; +import rep.simulator.REPSelector; import rep.xml.SessionXMLDecoder; import rep.xml.SessionXMLEncoder; @@ -26,7 +28,7 @@ //o-------header section (network order)-------------o /*int cmd; // command int sid; // session ID : uniqu to editing file -int eid; // editor ID : owner editor ID = 1。Session に対してユニーク +int eid; // editor ID : owner editor ID = 1。Session に対して unique int seqno; // Sequence number : sequence number はエディタごとに管理 int lineno; // line number int textsize; // textsize : bytesize @@ -57,12 +59,15 @@ } public void openSelector() throws IOException{ - selector = Selector.open(); + //selector = Selector.open(); + selector = REPSelector.open(); } public void sessionManagerNet(int port) throws InterruptedException, IOException { - ServerSocketChannel ssc = ServerSocketChannel.open(); + //ServerSocketChannel ssc = ServerSocketChannel.open(); + ServerSocketChannel ssc = REPServerSocketChannel.open(); + ssc.configureBlocking(false); //reuse address 必須 ssc.socket().setReuseAddress(true); @@ -81,38 +86,19 @@ if(key.isAcceptable()){ /*** serverChannelはenableになったSelectionKeyのchannel ***/ ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel(); - /*** EditorChannel を用いない記述 ***/ SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept registerChannel (selector, channel, SelectionKey.OP_READ); channel = null; - - /*** EditorChannel を用いた記述 ****/ - //EditorChannel echannel = (EditorChannel) ssc.accept(); - //echannel.setIO(); - //registerChannel(selector, echannel, SelectionKey.OP_READ); - //echannel = null; - - /*** SelectableEditorChannel ***/ - //SocketChannel channel = ssc.accept(); - //SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel); - //registerChannel(selector, echannel2, SelectionKey.OP_READ); - //channel = null; - //echannel2 = null; + }else if(key.isReadable()){ - /*** EditorChannel を用いない記述 ***/ SocketChannel channel = (SocketChannel)key.channel(); - REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。 + REPPacketReceive receive = new REPPacketReceive(channel); receive.setkey(key); REPCommand receivedCommand = receive.unpackUConv(); - //REPCommand receivedCommand = receive.unpack(); manager(channel, receivedCommand); - /*** EditorChannel を用いた記述 ****/ - //EditorChannel echannel = (EditorChannel) key.channel(); - //REPCommand command = echannel.getPacket(); - //manager(echannel, command); }else if(key.isConnectable()){ System.out.println("Connectable"); @@ -125,7 +111,6 @@ if(channel == null) { return; } - //System.out.println("registerChannel()"); channel.configureBlocking(false); selector.wakeup(); channel.register(selector, ops); @@ -399,7 +384,6 @@ } private int reverseCmd(int cmd) { - // TODO Auto-generated method stub int kindOfCmd = 0; switch(cmd){ case REP.REPCMD_INSERT: diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/ChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/ChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,116 @@ +package rep.channel; + +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

extends SelectableChannelSimulator

{ + //private BlockingQueue

qread; + //private BlockingQueue

qwrite; + //private SelectorSimulator

waitingSelector; + protected NetworkSimulator

ns; + + /** Constructors. */ + public ChannelSimulator(NetworkSimulator

_ns){ + this(_ns, null); + } + + public ChannelSimulator(NetworkSimulator

_ns, SelectorSimulator

_selector){ + super(null); + ns = _ns; + //ns = NetworkSimulator.singleton(); //どっちがいい? + } + public ChannelSimulator

createConjugatedChannel() { + ChannelSimulator

ret = new ChannelSimulator

(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

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 SelectionKey keyFor(Selector selector2) { + return ((SelectorSimulator

) 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 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 + + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/NetworkSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/NetworkSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,126 @@ +package rep.channel; + +import java.util.LinkedList; + + +public class NetworkSimulator

{ + public static NetworkSimulator ns; + synchronized public static NetworkSimulator singleton(){ + if (ns==null) + ns = new NetworkSimulator(); + return (NetworkSimulator) ns; + // NetworkSimulator ns = NetworkSimulator.singleton(new NetworkSimulator()); + } + + int logLevel=5; + /** Listening Servers. */ + private LinkedList> serverList; + + /** Constructor. */ + public NetworkSimulator(){ + serverList = new LinkedList>(); + writeLog("construct Networksimulator", 1); + printAllState(); + } + + + + /* */ + synchronized public void listen(int ip, SelectorSimulator

selector) { + serverList.add(new ServerData

(ip, selector)); + writeLog(Thread.currentThread(), "listen", 1); + printAllState(); + } + + synchronized public ChannelSimulator

accept(int ip) { + for (ServerData

sd: serverList){ + if (sd.virtualIP!=ip) continue; + writeLog(Thread.currentThread(), "accepting..", 1); + + ChannelSimulator

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

sd: serverList){ + if (sd.virtualIP!=ip) continue; + return !sd.acceptWaitingList.isEmpty(); + } + return false; + } + + public boolean connect(int ip, ChannelSimulator

clientCH) { + ServerData

sd = null; + writeLog(Thread.currentThread(), "connecting..", 1); + synchronized (this){ + for (ServerData

sd0: serverList){ + if (sd0.virtualIP!=ip) continue; + + sd = sd0; + } + if (sd==null) return false; + + //ChannelSimulator

channel = new ChannelSimulator

(sd.selector); + clientCH.createReadQ(); + clientCH.createWriteQ(); + clientCH.setWriteSelector(sd.selector); + + ChannelSimulator

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

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

{ + int virtualIP; + SelectorSimulator

selector; + LinkedList> acceptWaitingList; + LinkedList> establishedList; + + ServerData(int ip, SelectorSimulator

_selector){ + virtualIP = ip; + selector = _selector; + acceptWaitingList = new LinkedList>(); + establishedList = new LinkedList>(); + } +} \ No newline at end of file diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/REPServerSocketChannel.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/REPServerSocketChannel.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,56 @@ +package rep.channel; + +import java.io.IOException; +import java.net.ServerSocket; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; + +public class REPServerSocketChannel

extends ServerSocketChannel{ + + public static boolean isSimulation; + private ServerSocketChannel ss; + + protected REPServerSocketChannel(SelectorProvider provider) { + super(provider); + + } + + public REPServerSocketChannel(ServerSocketChannel channel){ + super(null); + ss = channel; + } + + public static REPServerSocketChannel open() throws IOException { + if(isSimulation){ + return ServerChannelSimulatorImpl.open(); + }else{ + return new REPServerSocketChannel(open()); + } + } + + public REPSocketChannel

accept1() throws IOException { + return new REPSocketChannel

(ss.accept()); + } + + @Override + public ServerSocket socket() { + return ss.socket(); + } + + @Override + protected void implCloseSelectableChannel() throws IOException { + ss.close(); + } + + @Override + protected void implConfigureBlocking(boolean block) throws IOException { + ss.configureBlocking(block); + } + + @Override + public SocketChannel accept() throws IOException { + // TODO Auto-generated method stub + return null; + } +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/REPSocketChannel.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/REPSocketChannel.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,66 @@ +package rep.channel; + +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.SocketChannel; +import java.nio.channels.spi.SelectorProvider; + +public class REPSocketChannel

extends SelectableChannel{ + + private SocketChannel sc; + + public REPSocketChannel(SocketChannel channel) { + sc = channel; + } + + @Override + public Object blockingLock() { + return sc.blockingLock(); + } + + @Override + public SelectableChannel configureBlocking(boolean block) throws IOException { + return sc.configureBlocking(block); + } + + @Override + public boolean isBlocking() { + return sc.isBlocking(); + } + + @Override + public boolean isRegistered() { + return sc.isRegistered(); + } + + @Override + public SelectionKey keyFor(Selector sel) { + return sc.keyFor(sel); + } + + @Override + public SelectorProvider provider() { + return sc.provider(); + } + + @Override + public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { + return sc.register(sel, ops, att); + } + + @Override + public int validOps() { + return sc.validOps(); + } + + @Override + protected void implCloseChannel() throws IOException { + sc.close(); + } + + + +} \ No newline at end of file diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/SelectableChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/SelectableChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,78 @@ +package rep.channel; + +import java.nio.channels.SelectableChannel; +import java.nio.channels.SocketChannel; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + + + +public abstract class SelectableChannelSimulator

extends REPSocketChannel

{ + + protected BlockingQueue

qread; + protected BlockingQueue

qwrite; + protected SelectorSimulator

writeSelector; + protected SelectorSimulator

readSelector; + + public SelectableChannelSimulator(SocketChannel channel) { + super(channel); + } + + /* 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

accept(); + + /* accessor methods. */ + public BlockingQueue

getReadQ(){ + return qread; + } + public BlockingQueue

getWriteQ(){ + return qwrite; + } + public void createReadQ(){ + qread = new LinkedBlockingQueue

(); + } + public void createWriteQ(){ + qwrite = new LinkedBlockingQueue

(); + } + public void setWriteSelector(SelectorSimulator

_selector){ + writeSelector = _selector; + } + + + /* return state of the Queue */ + abstract public boolean isReadable(); + abstract public boolean isWritable(); + abstract public boolean isAcceptable(); + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/SelectionKeySimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/SelectionKeySimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,76 @@ +package rep.channel; + +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; + +public class SelectionKeySimulator

extends SelectionKey{ + + private int interestOpt; + private SelectableChannelSimulator

channel; + private int ready; + public Selector selector; + + public SelectionKeySimulator(SelectableChannelSimulator

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

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; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/SelectorSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/SelectorSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,117 @@ +package rep.channel; + +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

extends Selector{ + + private TreeSet keyList; + private TreeSet selectedKeys; + + public SelectorSimulator() { + // TODO Auto-generated constructor stub + keyList = new TreeSet(); + } + + @SuppressWarnings("unchecked") + public int select() throws IOException { + selectedKeys = new TreeSet(); + + synchronized(this) { + + while(selectedKeys.isEmpty()){ + for(SelectionKey key : keyList){ + if(((SelectionKeySimulator

) 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

register(SelectableChannelSimulator

cs, int opt){ + SelectionKeySimulator

key = new SelectionKeySimulator

(cs, opt, this); + keyList.add(key); + return key; + } + + public SelectionKeySimulator

register(ChannelSimulator

cs, int opt, Object handler){ + SelectionKeySimulator

key = new SelectionKeySimulator

(cs, opt, this); + key.attach(handler); + keyList.add(key); + return key; + } + + public SelectionKey getKey(ChannelSimulator

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 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 selectedKeys() { + // TODO Auto-generated method stub + return (Set)selectedKeys; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/ServerChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/ServerChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,110 @@ +package rep.channel; + +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.ServerSocketChannel; +import java.nio.channels.spi.SelectorProvider; + + + +public class ServerChannelSimulator

extends SelectableChannelSimulator

{ + protected NetworkSimulator

ns; + private int virtualIP; + + /** Constructors. */ + public ServerChannelSimulator(NetworkSimulator

_ns, SelectorSimulator

rselector){ + super(null); + 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

accept(){ + ChannelSimulator

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 + + } + + public static ServerSocketChannel open() { + // TODO Auto-generated method stub + return null; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/channel/ServerChannelSimulatorImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/ServerChannelSimulatorImpl.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,50 @@ +package rep.channel; + +import java.io.IOException; +import java.net.ServerSocket; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; + +public class ServerChannelSimulatorImpl extends REPServerSocketChannel{ + + +protected ServerChannelSimulatorImpl(SelectorProvider provider) { + super(provider); + } + + + @Override + public ServerSocket socket() { + // TODO Auto-generated method stub + return null; + } + + + @Override + public SocketChannel accept() throws IOException { + // TODO Auto-generated method stub + + return null; + } + + + @Override + protected void implCloseSelectableChannel() throws IOException { + // TODO Auto-generated method stub + + } + + + @Override + protected void implConfigureBlocking(boolean block) throws IOException { + // TODO Auto-generated method stub + + } + + public static REPServerSocketChannel open() { + new ServerChannelSimulator(null, null); + return null; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/ChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/ChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,118 @@ +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

extends SelectableChannelSimulator

{ + //private BlockingQueue

qread; + //private BlockingQueue

qwrite; + //private SelectorSimulator

waitingSelector; + protected NetworkSimulator

ns; + + /** Constructors. */ + public ChannelSimulator(NetworkSimulator

_ns){ + this(_ns, null); + } + public ChannelSimulator(NetworkSimulator

_ns, SelectorSimulator

_selector){ + ns = _ns; + //ns = NetworkSimulator.singleton(); //どっちがいい? + } + public ChannelSimulator

createConjugatedChannel() { + ChannelSimulator

ret = new ChannelSimulator

(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

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

keyFor(SelectorSimulator

selector2) { + return (SelectionKeySimulator

) 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 + + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/NetworkSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/NetworkSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,126 @@ +package rep.simulator; + +import java.util.LinkedList; + + +public class NetworkSimulator

{ + public static NetworkSimulator ns; + synchronized public static NetworkSimulator singleton(){ + if (ns==null) + ns = new NetworkSimulator(); + return (NetworkSimulator) ns; + // NetworkSimulator ns = NetworkSimulator.singleton(new NetworkSimulator()); + } + + int logLevel=5; + /** Listening Servers. */ + private LinkedList> serverList; + + /** Constructor. */ + public NetworkSimulator(){ + serverList = new LinkedList>(); + writeLog("construct Networksimulator", 1); + printAllState(); + } + + + + /* */ + synchronized public void listen(int ip, SelectorSimulator

selector) { + serverList.add(new ServerData

(ip, selector)); + writeLog(Thread.currentThread(), "listen", 1); + printAllState(); + } + + synchronized public ChannelSimulator

accept(int ip) { + for (ServerData

sd: serverList){ + if (sd.virtualIP!=ip) continue; + writeLog(Thread.currentThread(), "accepting..", 1); + + ChannelSimulator

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

sd: serverList){ + if (sd.virtualIP!=ip) continue; + return !sd.acceptWaitingList.isEmpty(); + } + return false; + } + + public boolean connect(int ip, ChannelSimulator

clientCH) { + ServerData

sd = null; + writeLog(Thread.currentThread(), "connecting..", 1); + synchronized (this){ + for (ServerData

sd0: serverList){ + if (sd0.virtualIP!=ip) continue; + + sd = sd0; + } + if (sd==null) return false; + + //ChannelSimulator

channel = new ChannelSimulator

(sd.selector); + clientCH.createReadQ(); + clientCH.createWriteQ(); + clientCH.setWriteSelector(sd.selector); + + ChannelSimulator

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

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

{ + int virtualIP; + SelectorSimulator

selector; + LinkedList> acceptWaitingList; + LinkedList> establishedList; + + ServerData(int ip, SelectorSimulator

_selector){ + virtualIP = ip; + selector = _selector; + acceptWaitingList = new LinkedList>(); + establishedList = new LinkedList>(); + } +} \ No newline at end of file diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/REPSelector.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/REPSelector.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,27 @@ +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; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/SelectableChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/SelectableChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,72 @@ +package rep.simulator; + +import java.nio.channels.SelectableChannel; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + + + +public abstract class SelectableChannelSimulator

extends SelectableChannel{ + protected BlockingQueue

qread; + protected BlockingQueue

qwrite; + protected SelectorSimulator

writeSelector; + protected SelectorSimulator

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

accept(); + + /* accessor methods. */ + public BlockingQueue

getReadQ(){ + return qread; + } + public BlockingQueue

getWriteQ(){ + return qwrite; + } + public void createReadQ(){ + qread = new LinkedBlockingQueue

(); + } + public void createWriteQ(){ + qwrite = new LinkedBlockingQueue

(); + } + public void setWriteSelector(SelectorSimulator

_selector){ + writeSelector = _selector; + } + + + /* return state of the Queue */ + abstract public boolean isReadable(); + abstract public boolean isWritable(); + abstract public boolean isAcceptable(); + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/SelectionKeySimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/SelectionKeySimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,76 @@ +package rep.simulator; + +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; + +public class SelectionKeySimulator

extends SelectionKey{ + + private int interestOpt; + private SelectableChannelSimulator

channel; + private int ready; + public Selector selector; + + public SelectionKeySimulator(SelectableChannelSimulator

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

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; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/SelectorSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/SelectorSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,117 @@ +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

extends Selector{ + + private TreeSet keyList; + private TreeSet selectedKeys; + + public SelectorSimulator() { + // TODO Auto-generated constructor stub + keyList = new TreeSet(); + } + + @SuppressWarnings("unchecked") + public int select() throws IOException { + selectedKeys = new TreeSet(); + + synchronized(this) { + + while(selectedKeys.isEmpty()){ + for(SelectionKey key : keyList){ + if(((SelectionKeySimulator

) 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

register(SelectableChannelSimulator

cs, int opt){ + SelectionKeySimulator

key = new SelectionKeySimulator

(cs, opt, this); + keyList.add(key); + return key; + } + + public SelectionKeySimulator

register(ChannelSimulator

cs, int opt, Object handler){ + SelectionKeySimulator

key = new SelectionKeySimulator

(cs, opt, this); + key.attach(handler); + keyList.add(key); + return key; + } + + public SelectionKey getKey(ChannelSimulator

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 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 selectedKeys() { + // TODO Auto-generated method stub + return (Set)selectedKeys; + } + +} diff -r 790c8dd42a7b -r 5b1a0574b406 rep/simulator/ServerChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/simulator/ServerChannelSimulator.java Wed Aug 27 17:21:25 2008 +0900 @@ -0,0 +1,103 @@ +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

extends SelectableChannelSimulator

{ + protected NetworkSimulator

ns; + private int virtualIP; + + /** Constructors. */ + public ServerChannelSimulator(NetworkSimulator

_ns, SelectorSimulator

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

accept(){ + ChannelSimulator

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 + + } + +}