view rep/channel/ServerChannelSimulator.java @ 382:4b87f89b3afd

REP Session Manager (Java version) new structure
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:07:45 +0900
parents edb373aa421e
children 5e3532db2e07
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<P> ch){
		// Don't lock a selector from a locked channel, the selector may
		// use channel.isAble() which locks the channel.
		synchronized(this) {
			acceptQ.offer(ch);
			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
		
	}


}