Mercurial > hg > RemoteEditor > REPSessionManager
changeset 194:be219ba8b39c
*** empty log message ***
author | kono |
---|---|
date | Fri, 29 Aug 2008 20:25:46 +0900 |
parents | 311847db7429 |
children | 63f36334e8dc |
files | rep/channel/ChannelSimulator.java rep/channel/REPSelectionKey.java rep/channel/REPSelector.java rep/channel/REPServerSocket.java rep/channel/REPServerSocketChannel.java rep/channel/REPSocketChannel.java rep/channel/SelectionKeySimulator.java rep/channel/SelectorSimulator.java rep/channel/ServerChannelSimulator.java test/ServerSample.java |
diffstat | 10 files changed, 244 insertions(+), 155 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/channel/ChannelSimulator.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/ChannelSimulator.java Fri Aug 29 20:25:46 2008 +0900 @@ -1,18 +1,11 @@ package rep.channel; -import java.io.IOException; import java.net.SocketAddress; 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. */ @@ -41,16 +34,6 @@ 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; @@ -69,52 +52,17 @@ 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; - } - @SuppressWarnings("unchecked") + @Override public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { SelectorSimulator selector = (SelectorSimulator) sel; return selector.register(this, ops, att); } + public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException { REPSelector selector = sel; return selector.register(this, ops, att); } - @Override - public int validOps() { - // TODO Auto-generated method stub - return 0; - } - @Override - protected void implCloseChannel() throws IOException { - // TODO Auto-generated method stub - - } }
--- a/rep/channel/REPSelectionKey.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/REPSelectionKey.java Fri Aug 29 20:25:46 2008 +0900 @@ -1,51 +1,68 @@ 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.ServerSocketChannel; -public class REPSelectionKey extends SelectionKey { +public class REPSelectionKey<P> extends SelectionKey { + SelectionKey key; + + public REPSelectionKey(SelectionKey key) { + this.key = key; + } + @Override public void cancel() { - // TODO Auto-generated method stub - + key.cancel(); } @Override public SelectableChannel channel() { - // TODO Auto-generated method stub - return null; + return key.channel(); + } + + public SelectableChannel channel(REPPack<P>packer) { + if (REPServerSocketChannel.isSimulation) return key.channel(); + if (key.isAcceptable()) { + return new REPServerSocketChannel<P>(key.channel(),packer); + } else { + return new REPSocketChannel<P>(key.channel(),packer); + } } @Override public int interestOps() { - // TODO Auto-generated method stub - return 0; + return key.interestOps(); } @Override public SelectionKey interestOps(int ops) { - // TODO Auto-generated method stub - return null; + return key.interestOps(ops); } @Override public boolean isValid() { - // TODO Auto-generated method stub - return false; + return key.isValid(); } @Override public int readyOps() { - // TODO Auto-generated method stub - return 0; + return key.readyOps(); } @Override public Selector selector() { - // TODO Auto-generated method stub - return null; + return key.selector(); } + public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException { + assert(!REPServerSocketChannel.isSimulation); + ServerSocketChannel sc = (ServerSocketChannel)key.channel(); + return new REPSocketChannel<P>(sc.accept(),pack); + } + + }
--- a/rep/channel/REPSelector.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/REPSelector.java Fri Aug 29 20:25:46 2008 +0900 @@ -7,9 +7,11 @@ import java.nio.channels.Selector; import java.nio.channels.spi.AbstractSelector; import java.nio.channels.spi.SelectorProvider; +import java.util.HashSet; import java.util.Set; -public class REPSelector extends Selector{ + +public class REPSelector<P> extends Selector{ Selector selector; @@ -17,11 +19,11 @@ this.selector = selector; } - public static REPSelector create() throws IOException{ + public static <T>REPSelector<T> create() throws IOException{ if(REPServerSocketChannel.isSimulation){ - return new SelectorSimulator(); + return new SelectorSimulator<T>(); } - return new REPSelector(SelectorProvider.provider().openSelector()); + return new REPSelector<T>(SelectorProvider.provider().openSelector()); } @Override @@ -61,12 +63,6 @@ @Override public Set<SelectionKey> selectedKeys() { - /* - Set<SelectionKey> keys = new HashSet<SelectionKey>(); - for (SelectionKey key: selector.selectedKeys()){ - keys.add(new SelectionKeySimulator(new REPSocketChannel<?>(), key.readyOps(), selector)); - new SelectionKey(); - }*/ return selector.selectedKeys(); } @@ -78,16 +74,16 @@ public SelectionKey register(SelectableChannel ch, int ops, Object att) throws ClosedChannelException{ return ch.register(selector, ops, att); } - public SelectionKey register(SelectableChannel ch, int ops) throws ClosedChannelException{ - return register(ch, ops, null); + + public Set<REPSelectionKey<P>> selectedKeys1() { + Set<SelectionKey> keys = selector.selectedKeys(); + HashSet<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); + for(SelectionKey k: keys) { + newKeys.add(new REPSelectionKey<P>(k)); + } + return (Set<REPSelectionKey<P>>)newKeys; } - /* - 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 Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/REPServerSocket.java Fri Aug 29 20:25:46 2008 +0900 @@ -10,8 +10,6 @@ /* こいつはシミュレーションの時しか生成され無いゾ */ public class REPServerSocket extends ServerSocket{ ServerChannelSimulator<?> scs; - //scs.socket().setReuseAddress(true); - //scs.socket().bind(IP); public REPServerSocket(ServerChannelSimulator<?> channel) throws IOException { scs = channel; @@ -26,7 +24,7 @@ } @Override public ServerSocketChannel getChannel(){ - return scs; + return null; } @Override public InetAddress getInetAddress(){
--- a/rep/channel/REPServerSocketChannel.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/REPServerSocketChannel.java Fri Aug 29 20:25:46 2008 +0900 @@ -3,7 +3,9 @@ import java.io.IOException; import java.net.ServerSocket; 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.SocketChannel; import java.nio.channels.spi.SelectorProvider; @@ -12,27 +14,16 @@ * シミュレーションでは inheritance のServerChannelSimulator を生成、 * リアルコミュニケーションでは 自身を生成、内部にもつ ServerSocketChannelを扱う */ -public class REPServerSocketChannel<P> extends ServerSocketChannel{ +public class REPServerSocketChannel<P> extends SelectableChannel { public static boolean isSimulation; - private ServerSocketChannel ss; + private ServerSocketChannel ssc; private REPPack<P> packer; - - protected REPServerSocketChannel(SelectorProvider provider) { - super(provider); + + public REPServerSocketChannel() { + } - public REPServerSocketChannel(ServerSocketChannel channel, REPPack<P> _packer){ - super(SelectorProvider.provider()); - ss = channel; - packer = _packer; - } - - public REPServerSocketChannel() { - super(null); - - } - public static <T> REPServerSocketChannel<T> open(REPPack<T> packer) throws IOException{ if(isSimulation){ return new ServerChannelSimulator<T>(); @@ -40,39 +31,98 @@ return new REPServerSocketChannel<T>(ServerSocketChannel.open(), packer); } } - - public REPSocketChannel<P> accept1() throws IOException { - return new REPSocketChannel<P>(accept(), packer); + + public static <T> REPServerSocketChannel<T> open(SelectableChannel c,REPPack<T> packer) throws IOException{ + assert(!isSimulation); + return new REPServerSocketChannel<T>((ServerSocketChannel)c, packer); } + + public REPServerSocketChannel(ServerSocketChannel open, REPPack<P> packer) { + ssc = open; this. + packer = packer; + } + - @Override - public ServerSocket socket() { - return ss.socket(); + public REPServerSocketChannel(SelectableChannel channel, REPPack<P> packer) { + this.ssc = (ServerSocketChannel)channel; + this.packer = packer; } - @Override - protected void implCloseSelectableChannel() throws IOException { - ss.close(); + public REPSocketChannel<P> accept1() throws IOException { + return new REPSocketChannel<P>(ssc.accept(), packer); } - - @Override - protected void implConfigureBlocking(boolean block) throws IOException { - ss.configureBlocking(block); - } - - - public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException { + + public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { assert(!isSimulation); - REPSelector selector = sel; if(sel!=null) - return selector.register(this, ops, att); + return sel.register(ssc, ops, att); else return null; } + public SocketChannel accept() throws IOException { + return accept1().sc; + } + + + public ServerSocket socket() { + return ssc.socket(); + } + + public SelectableChannel configureBlocking(boolean block) throws IOException + { + ssc.configureBlocking(block); + return this; + } + @Override - public SocketChannel accept() throws IOException { - return null;//(SocketChannel) accept1(); + public Object blockingLock() { + // 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/channel/REPSocketChannel.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/REPSocketChannel.java Fri Aug 29 20:25:46 2008 +0900 @@ -13,7 +13,7 @@ public class REPSocketChannel<P> extends SelectableChannel{ - private SocketChannel sc; + public SocketChannel sc; private REPPack<P> pack; public REPSocketChannel(SocketChannel channel, REPPack<P> packer) { @@ -21,6 +21,11 @@ pack = packer; } + public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) { + sc = (SocketChannel)channel; + pack = packer; + } + @Override public Object blockingLock() { return sc.blockingLock(); @@ -51,9 +56,9 @@ return sc.provider(); } - @Override - public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { - return sc.register(sel, ops, att); + + public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { + return sc.register(sel.selector, ops, att); } @Override @@ -110,10 +115,18 @@ } } + public boolean connect(SocketAddress semaIP) throws IOException { return sc.connect(semaIP); } + @Override + public SelectionKey register(Selector sel, int ops, Object att) + throws ClosedChannelException { + assert(false); + return null; + } + } \ No newline at end of file
--- a/rep/channel/SelectionKeySimulator.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/SelectionKeySimulator.java Fri Aug 29 20:25:46 2008 +0900 @@ -1,8 +1,10 @@ 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.ServerSocketChannel; public class SelectionKeySimulator extends SelectionKey{ @@ -40,6 +42,14 @@ return channel; } + public SelectableChannel channel(REPPack<?> packer) { + return channel; + } + + public REPSocketChannel<?> accept(REPPack<?> pack) throws IOException { + SelectableChannelSimulator<?> scs = (SelectableChannelSimulator<?>) channel; + return scs.accept(); + } @Override public void cancel() {
--- a/rep/channel/SelectorSimulator.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/SelectorSimulator.java Fri Aug 29 20:25:46 2008 +0900 @@ -8,7 +8,7 @@ import java.util.HashSet; import java.util.Set; -public class SelectorSimulator extends REPSelector{ +public class SelectorSimulator<P> extends REPSelector<P>{ private Set<SelectionKey> keyList; private Set<SelectionKey> selectedKeys; @@ -50,6 +50,17 @@ keyList.add(key); return key; } + + + public Set<REPSelectionKey<P>> selectedKeys1() { + Set<SelectionKey> keys = keyList; + HashSet<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); + for(SelectionKey k: keys) { + newKeys.add(new REPSelectionKey<P>(k)); + } + return (Set<REPSelectionKey<P>>)newKeys; + } + /* public <T> SelectionKeySimulator register(ChannelSimulator<T> cs, int opt, Object handler){ SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this);
--- a/rep/channel/ServerChannelSimulator.java Fri Aug 29 20:02:23 2008 +0900 +++ b/rep/channel/ServerChannelSimulator.java Fri Aug 29 20:25:46 2008 +0900 @@ -4,11 +4,14 @@ import java.net.ServerSocket; import java.net.SocketAddress; 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; /* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装 */ -public class ServerChannelSimulator<P>extends REPServerSocketChannel<P>{ +public class ServerChannelSimulator<P>extends REPServerSocketChannel<P> { protected NetworkSimulator<P> ns; //public REPServerSocket<REPSocketChannel<P>> socket; protected SocketAddress IP; @@ -28,7 +31,6 @@ return ns.accept(IP); } - @Override public ServerSocket socket() { try { return new REPServerSocket(this); @@ -38,25 +40,10 @@ } } - @Override - protected void implCloseSelectableChannel() throws IOException { - return; - } - - @Override - protected void implConfigureBlocking(boolean block) throws IOException { - return; - } - - @Override - public SocketChannel accept() throws IOException { - // TODO Auto-generated method stub - return null; - } - public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException { - REPSelector selector = sel; + public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { + REPSelector<P> selector = sel; ns.listen(IP, (SelectorSimulator) selector); return selector.register(this, ops, att); } @@ -65,4 +52,59 @@ return ns.canAccept(IP); } + @Override + public Object blockingLock() { + // TODO Auto-generated method stub + return null; + } + + 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/test/ServerSample.java Fri Aug 29 20:02:23 2008 +0900 +++ b/test/ServerSample.java Fri Aug 29 20:25:46 2008 +0900 @@ -5,6 +5,9 @@ import java.net.*; import rep.REPCommand; +import rep.REPCommandPacker; +import rep.channel.REPPack; +import rep.channel.REPSelectionKey; import rep.channel.REPSelector; import rep.channel.REPServerSocketChannel; @@ -14,11 +17,12 @@ throws Exception { // セレクタの用意 - REPSelector selector = REPSelector.create(); + REPSelector<REPCommand> selector = REPSelector.create(); + REPPack<REPCommand> pack = new REPCommandPacker(); // サーバソケットチャンネルを作成。5100番ポートを受付ポートに指定 // (非ブロックモードに設定:重要) - REPServerSocketChannel<REPCommand> serverSocketChannel = REPServerSocketChannel.<REPCommand>open(); + REPServerSocketChannel<REPCommand> serverSocketChannel = REPServerSocketChannel.<REPCommand>open(pack); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(new InetSocketAddress(5100)); @@ -32,7 +36,7 @@ selector.select(); // 獲得したイベントごとに処理を実行 - for (SelectionKey selectionKey : selector.selectedKeys()) { + for (REPSelectionKey<REPCommand> selectionKey : selector.selectedKeys1()) { // サーバの受付処理: // イベントが受付可能である場合、受け付けるべき対象があれば