Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/ServerChannelSimulator.java @ 502:49b689b17d06 default tip
merged TestEditor to REPEditor
author | suika6039 |
---|---|
date | Tue, 21 Dec 2010 18:01:15 +0900 |
parents | 5e3532db2e07 |
children |
line wrap: on
line source
package rep.channel; import java.io.IOException; import java.net.InetSocketAddress; 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.spi.SelectorProvider; import java.util.LinkedList; import java.util.Queue; /* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装 */ public class ServerChannelSimulator<P>extends REPServerSocketChannel<P> { protected NetworkSimulator ns; //public REPServerSocket<REPSocketChannel<P>> socket; protected InetSocketAddress IP; protected Queue<ChannelSimulator<P>> acceptQ; protected Selector selector; protected boolean isBlocking; private SelectionKeySimulator<P> key; /** Constructors. * @throws IOException */ public ServerChannelSimulator() throws IOException { ns = NetworkSimulator.singleton(); selector = new NullSelector(); // new Object(); acceptQ = new LinkedList<ChannelSimulator<P>>(); } public void bind(InetSocketAddress ip){ IP = ip; } public synchronized REPSocketChannel<P> accept1() throws IOException { ChannelSimulator<P> channel; while ( (channel=acceptQ.poll())==null && isBlocking ) { try { wait(); } catch (InterruptedException e) { throw new IOException(); } } return channel; } protected boolean enQ(ChannelSimulator<?> ch){ // Don't lock a selector from a locked channel, the selector may // use channel.isAble() which locks the channel. @SuppressWarnings("unchecked") ChannelSimulator<P>ch1 = (ChannelSimulator<P>)ch; synchronized(this) { acceptQ.offer(ch1); notify(); } selector.wakeup(); return true; } // // @SuppressWarnings("unchecked") // public void enQ(ChannelSimulator<?> hserver) { // // NetworkSimulator doesn't know P // ChannelSimulator<P>ch = (ChannelSimulator<P>) hserver; // enQ(ch); // } public ServerSocket socket() { try { return new REPServerSocket(this); } catch (IOException e) { e.printStackTrace(); return null; } } @SuppressWarnings("unchecked") public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { selector = sel; REPSelector<P> selector1 = sel; ns.listen(IP, this); key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); return key; } @SuppressWarnings("unchecked") @Override public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { selector = sel; REPSelector<P> selector1 = (REPSelector<P>)sel; ns.listen(IP, this); // bindに移動してもいいよ key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); return key; } public synchronized boolean isAcceptable() { return !acceptQ.isEmpty(); } @Override public Object blockingLock() { return selector; } public SelectableChannel configureBlocking(boolean block) throws IOException { isBlocking = block; return this; } @Override public boolean isBlocking() { return isBlocking; } @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 int validOps() { // TODO Auto-generated method stub return 0; } @Override protected void implCloseChannel() throws IOException { // TODO Auto-generated method stub } }