view rep/channel/ServerChannelSimulator.java @ 282:c410eda661e8

*** empty log message ***
author kono
date Sat, 27 Sep 2008 23:30:37 +0900
parents e72e0eae1261
children 90965a3bd4f3
line wrap: on
line source

package rep.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
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.spi.SelectorProvider;
import java.util.LinkedList;
import java.util.Queue;

/* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装  */
public class ServerChannelSimulator<P>extends REPServerSocketChannel<P> {
	protected NetworkSimulator<P> ns;
	//public REPServerSocket<REPSocketChannel<P>> socket;
	protected InetSocketAddress IP;
	protected Queue<ChannelSimulator<P>> acceptQ;
	protected Object lock;
	protected boolean isBlocking;
	private SelectionKeySimulator<P> key;

	/**  Constructors. 
	 * @throws IOException */
	public ServerChannelSimulator() throws IOException {
		//socket = REPServerSocket.<REPSocketChannel<P>>create();
		ns = NetworkSimulator.<P>singleton();
		lock = new Object();
		acceptQ = new LinkedList<ChannelSimulator<P>>();
	}
	
	public void bind(InetSocketAddress ip){
		IP = ip;
	}

	public REPSocketChannel<P> accept1() throws IOException {
		ChannelSimulator<P> tmp;
		synchronized (lock) {
			while ( (tmp=acceptQ.poll())==null && isBlocking ) {
				try {
					lock.wait();
				} catch (InterruptedException e) {
					throw new IOException();
				}
			}
		}
		//return ns.accept(IP);
		return tmp;
	}
	protected boolean enQ(ChannelSimulator<P> ch){
		synchronized (lock){
			acceptQ.offer(ch);
			lock.notifyAll();
		}
		return true;
	}

	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 {
		synchronized (lock) {
			lock = sel;
		}
		REPSelector<P> selector = sel;
		ns.listen(IP, this);
		key = (SelectionKeySimulator<P>) selector.register(this, ops, att);
		return key;
	}
	@SuppressWarnings("unchecked")
	@Override
	public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
		// TODO
		synchronized (lock) {
			lock = sel;
		}
		REPSelector<P> selector = (REPSelector<P>)sel;
		ns.listen(IP, this); // bindに移動してもいいよ
		key = (SelectionKeySimulator<P>) selector.register(this, ops, att);
		return key;
	}

	public boolean isAcceptable() {
		synchronized (lock){ 
			return !acceptQ.isEmpty();
		}
	}

	@Override
	public Object blockingLock() {
		return lock;
	}

	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
		
	}

}