Mercurial > hg > RemoteEditor > REPSessionManager
changeset 174:72252e970a8b
*** empty log message ***
author | kent |
---|---|
date | Thu, 28 Aug 2008 19:49:41 +0900 |
parents | 48c64244d4d4 |
children | 5653cf8e3c8b |
files | rep/channel/ChannelSimulator.java rep/channel/NetworkSimulator.java rep/channel/REPLogger.java rep/channel/REPSelector.java rep/channel/REPServerSocket.java rep/channel/REPSocketChannel.java rep/channel/SelectableChannelSimulator.java rep/channel/SelectionKeySimulator.java rep/channel/SelectorSimulator.java test/channeltest/testEditor.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMa.java test/channeltest/testSeMaSlave.java |
diffstat | 13 files changed, 77 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/channel/ChannelSimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/ChannelSimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -16,17 +16,13 @@ protected NetworkSimulator<P> ns; /** Constructors. */ - public ChannelSimulator(NetworkSimulator<P> _ns){ - this(_ns, null); + public ChannelSimulator(){ + super(null); + ns = NetworkSimulator.<P>singleton(); } - public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator _selector){ - super(null); - ns = _ns; - //ns = NetworkSimulator.singleton(); //どっちがいい? - } public ChannelSimulator<P> createConjugatedChannel() { - ChannelSimulator<P> ret = new ChannelSimulator<P>(ns); + ChannelSimulator<P> ret = new ChannelSimulator<P>(); ret.qread=qwrite; ret.qwrite=qread; ret.readSelector=writeSelector;
--- a/rep/channel/NetworkSimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/NetworkSimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -11,14 +11,15 @@ public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>(); public int ipcount = 1; - public static <T> NetworkSimulator<?> singleton(){ + @SuppressWarnings("unchecked") + public static <T> NetworkSimulator<T> singleton(){ // double check singleton if (ns==null) synchronized (NetworkSimulator.class) { if (ns==null) ns = new NetworkSimulator<T>(); } - return ns; + return (NetworkSimulator<T>) ns; } int logLevel=5; @@ -37,19 +38,19 @@ /* */ synchronized public void listen(int ip, SelectorSimulator selector) { serverList.add(new ServerData<P>(ip, selector)); - writeLog(Thread.currentThread(), "listen", 1); + writeLog("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); + writeLog("accepting..", 1); ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove(); sd.establishedList.add(serverCH); - writeLog(Thread.currentThread(), "accepted", 1); + writeLog("accepted", 1); printAllState(); return serverCH; } @@ -65,7 +66,7 @@ public boolean connect(int ip, ChannelSimulator<P> clientCH) { ServerData<P> sd = null; - writeLog(Thread.currentThread(), "connecting..", 1); + writeLog("connecting..", 1); synchronized (this){ for (ServerData<P> sd0: serverList){ if (sd0.virtualIP!=ip) continue; @@ -86,7 +87,7 @@ synchronized (sd.selector) { sd.selector.notifyAll(); } - writeLog(Thread.currentThread(), "connected", 1); + writeLog("connected", 1); printAllState(); return true; }
--- a/rep/channel/REPLogger.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/REPLogger.java Thu Aug 28 19:49:41 2008 +0900 @@ -1,21 +1,32 @@ package rep.channel; public class REPLogger { + static REPLogger single; + + public static REPLogger singleton(){ + if(single==null){ + synchronized(REPLogger.class){ + if(single==null) + single = new REPLogger(); + } + } + return single; + } + protected REPLogger(){ + } private int logLevel; /** simulation log command */ synchronized public void writeLog(String log, int level){ if ( level<=logLevel ) - System.out.println(log); + System.out.println(Thread.currentThread().getName()+": "+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) { + + synchronized public void setLogLevel(int logLevel) { this.logLevel = logLevel; }
--- a/rep/channel/REPSelector.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/REPSelector.java Thu Aug 28 19:49:41 2008 +0900 @@ -72,7 +72,13 @@ public SelectionKey register(SelectableChannel ch, int ops, Object att) throws ClosedChannelException{ return ch.register(selector, ops, att); } - + /* + public SelectionKey register(REPSocketChannel<?> ch, int ops, Object att) throws ClosedChannelException{ + return ch.register(selector, ops, att); + } + public SelectionKey register(REPServerSocketChannel<?> ch, int ops, Object att) throws ClosedChannelException{ + return ch.register(selector, ops, att); + }*/ }
--- a/rep/channel/REPServerSocket.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/REPServerSocket.java Thu Aug 28 19:49:41 2008 +0900 @@ -9,13 +9,11 @@ /* こいつはシミュレーションの時しか生成され無いゾ */ public class REPServerSocket extends ServerSocket{ - NetworkSimulator<?> ns; ServerChannelSimulator<?> scs; //scs.socket().setReuseAddress(true); //scs.socket().bind(IP); public REPServerSocket(ServerChannelSimulator<?> channel) throws IOException { - ns = NetworkSimulator.singleton(); scs = channel; }
--- a/rep/channel/REPSocketChannel.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/REPSocketChannel.java Thu Aug 28 19:49:41 2008 +0900 @@ -112,7 +112,7 @@ public static <T> REPSocketChannel<T> create() throws IOException { if (REPServerSocketChannel.isSimulation) { - return new ChannelSimulator<T>(null); + return new ChannelSimulator<T>(); } else { return new REPSocketChannel<T>(SocketChannel.open()); }
--- a/rep/channel/SelectableChannelSimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/SelectableChannelSimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -10,7 +10,7 @@ protected BlockingQueue<P> qwrite; protected SelectorSimulator writeSelector; protected SelectorSimulator readSelector; - + public SelectableChannelSimulator(SocketChannel channel) { super(channel); }
--- a/rep/channel/SelectionKeySimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/SelectionKeySimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -4,7 +4,7 @@ import java.nio.channels.SelectionKey; import java.nio.channels.Selector; -public class SelectionKeySimulator<P> extends SelectionKey{ +public class SelectionKeySimulator extends SelectionKey{ private int interestOpt; private SelectableChannel channel;
--- a/rep/channel/SelectorSimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/rep/channel/SelectorSimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -1,6 +1,7 @@ package rep.channel; import java.io.IOException; +import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.spi.SelectorProvider; @@ -44,19 +45,25 @@ } return selectedKeys.size(); } - + /* public <T> SelectionKeySimulator register(SelectableChannelSimulator<T> cs, int opt){ SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this); keyList.add(key); return key; + }*/ + public SelectionKeySimulator register(SelectableChannel cs, int opt, Object handler){ + SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this); + key.attach(handler); + keyList.add(key); + return key; } - + /* public <T> SelectionKeySimulator register(ChannelSimulator<T> cs, int opt, Object handler){ SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this); key.attach(handler); keyList.add(key); return key; - } + }*/ public <T> SelectionKey getKey(ChannelSimulator<T> channel){ for(SelectionKey key : keyList){
--- a/test/channeltest/testEditor.java Thu Aug 28 18:59:30 2008 +0900 +++ b/test/channeltest/testEditor.java Thu Aug 28 19:49:41 2008 +0900 @@ -15,7 +15,7 @@ public testEditor(String name, String _host,int _port){ super(name); semaIP = new InetSocketAddress(_host, _port); - ns = testNetworkSimulator.ns; + ns = REPLogger.singleton(); } public void run(){ @@ -23,21 +23,21 @@ REPSocketChannel<String> channel; channel = REPSocketChannel.<String>create(); - ns.writeLog(this, "try to connect to SessionManager whose ip is "+semaIP, 1); + ns.writeLog("try to connect to SessionManager whose ip is "+semaIP, 1); while (!channel.connect(semaIP)){ - ns.writeLog(this, "SeMa not listen to socket yet, wait", 1); + ns.writeLog("SeMa not listen to socket yet, wait", 1); Thread.yield(); } - ns.writeLog(this, "successes to connect", 1); + ns.writeLog("successes to connect", 1); channel.write("from "+getName()+": hello"); - ns.writeLog(this, "wrote packet", 1); + ns.writeLog("wrote packet", 1); String packet = channel.read(); - ns.writeLog(this, "gets return string==> `"+packet+"\'", 1); + ns.writeLog("gets return string==> `"+packet+"\'", 1); - ns.writeLog(this, "testEditor exits.", 1); + ns.writeLog("testEditor exits.", 1); } catch (IOException e) { } }
--- a/test/channeltest/testNetworkSimulator.java Thu Aug 28 18:59:30 2008 +0900 +++ b/test/channeltest/testNetworkSimulator.java Thu Aug 28 19:49:41 2008 +0900 @@ -12,12 +12,12 @@ private int NoSemaMaster; private int NoSemaSlave; private int NoEditor; - static public REPLogger ns = new REPLogger(); + static public REPLogger logger = REPLogger.singleton(); public static void main(String[] args){ - REPServerSocketChannel.isSimulation = false; - //testNetworkSimulator testns = new testNetworkSimulator(3, 10, 50); - testNetworkSimulator testns = new testNetworkSimulator(1, 1, 1); + REPServerSocketChannel.isSimulation = true; + testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2); + testns.startTest(); } @@ -44,6 +44,7 @@ semaList.add(sm); sm.start(); } + logger.writeLog("all master SessionManagers were created",1); slavePort = port; for (int i=0; i<NoSemaSlave; i++){ testSeMaSlave sm = new testSeMaSlave("SeMaS"+i, @@ -52,18 +53,19 @@ semasList.add(sm); sm.start(); } + logger.writeLog("all slave SessionManagers were created",1); for (int i=0; i<NoEditor; i++){ testEditor te = new testEditor("Editor"+i,host, masterPort+rand.nextInt(NoSemaMaster+NoSemaSlave)); editorList.add(te); te.start(); } - + logger.writeLog("all SessionManagers and Editors ware constructed."); Checker(); try { for (testEditor te: editorList) te.join(); - ns.writeLog("main: all clients exited.", 1); + logger.writeLog("main: all clients exited."); } catch (InterruptedException e) { e.printStackTrace();
--- a/test/channeltest/testSeMa.java Thu Aug 28 18:59:30 2008 +0900 +++ b/test/channeltest/testSeMa.java Thu Aug 28 19:49:41 2008 +0900 @@ -21,7 +21,7 @@ public testSeMa(String name, String host, int port){ super(name); IP = new InetSocketAddress(host,port); - ns = testNetworkSimulator.ns; + ns = REPLogger.singleton(); channels = new LinkedList<REPSocketChannel<String>>(); } public void init(){ @@ -55,20 +55,20 @@ for(SelectionKey key : selector.selectedKeys()){ if(key.isAcceptable()){ - ns.writeLog(this, "gets acceptable channel", 1); + ns.writeLog("gets acceptable channel", 1); REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel(); REPSocketChannel<String> channel; channel = sc.accept1(); selector.register(channel, SelectionKey.OP_READ, null); - ns.writeLog(this, "accepts a client.", 1); + ns.writeLog("accepts a client.", 1); }else if(key.isReadable()){ - ns.writeLog(this, "gets readable channel", 1); + ns.writeLog("gets readable channel", 1); //SelectableChannelSimulator<String> channel = key.channel(); REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel(); String packet; packet = channel.read(); - ns.writeLog(this, "receives String==> `"+packet+"\'", 1); + ns.writeLog("receives String==> `"+packet+"\'", 1); channel.write("from SeMa"+this.getName()+": world"); } }
--- a/test/channeltest/testSeMaSlave.java Thu Aug 28 18:59:30 2008 +0900 +++ b/test/channeltest/testSeMaSlave.java Thu Aug 28 19:49:41 2008 +0900 @@ -25,7 +25,7 @@ ownIP = new InetSocketAddress(oname,oport); masterIP = new InetSocketAddress(mname,mport); cis = new LinkedList<ClientInfo>(); - ns = testNetworkSimulator.ns; + ns = REPLogger.singleton(); } public void init(){ @@ -53,24 +53,24 @@ for(SelectionKey key : selector.selectedKeys()){ if(key.isAcceptable()){ - ns.writeLog(this, "gets acceptable channel", 1); + ns.writeLog("gets acceptable channel", 1); REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel(); REPSocketChannel<String> channel = sc.accept1(); selector.register(channel, SelectionKey.OP_READ,null); - ns.writeLog(this, "accepts a client.", 1); + ns.writeLog("accepts a client.", 1); }else if(key.isReadable()){ - ns.writeLog(this, "gets readable channel", 1); + ns.writeLog("gets readable channel", 1); REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel(); String packet = channel.read(); if (channel==masterCH){ - ns.writeLog(this, "receives String from master ==> `"+packet+"\'", 1); + ns.writeLog("receives String from master ==> `"+packet+"\'", 1); for (ClientInfo ci: cis){ if (!packet.matches(ci.str)) continue; ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'"); } }else{ - ns.writeLog(this, "receives String==> `"+packet+"\'", 1); + ns.writeLog("receives String==> `"+packet+"\'", 1); //channel.write("from "+this.getName()+": slave"); masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor"); cis.add(new ClientInfo(packet, channel)); @@ -86,12 +86,12 @@ private REPSocketChannel<String> connectToMaster(Selector _selector) throws IOException { REPSocketChannel<String> channel = REPSocketChannel.<String>create(); - ns.writeLog(this, "is connecting to masterSeMa whose ip is"+masterIP, 1); + ns.writeLog("is connecting to masterSeMa whose ip is"+masterIP, 1); while(!channel.connect(masterIP)){ - ns.writeLog(this, "SeMa not listen to socket yet, wait", 1); + ns.writeLog("SeMa not listen to socket yet, wait", 1); Thread.yield(); } - ns.writeLog(this, "connecting was successful.", 1); + ns.writeLog("connecting was successful.", 1); return channel; }