view src/pathfinder/mergetest/NetworkSimulatorwithSeMa.java @ 153:6326e5ea4595

*** empty log message ***
author pin
date Sat, 23 Aug 2008 11:28:16 +0900
parents 34b15dfcb83e
children
line wrap: on
line source

package pathfinder.mergetest;

import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class NetworkSimulatorwithSeMa<P> extends NetworkSimulator<P> {

	/** Waiting connectionRequest to be accepted by SessionManager. */
	private LinkedList<ChannelSimulator<P>> acceptedList;
	/** Established connection */
	private LinkedList<ChannelSimulator<P>> connectedList;
	
	//private SeMaSimulator<REPCommand> sema;
	
	public NetworkSimulatorwithSeMa(){
		acceptedList = new LinkedList<ChannelSimulator<P>>();
		connectedList = new LinkedList<ChannelSimulator<P>>();
	}

	/**
	 * Establish connection. It's called by SeMa.
	 * @return
	 */
	public ChannelSimulator<P> accept(){
		ChannelSimulator<P> cs = connectedList.poll();
		if (cs==null) return null;

		acceptedList.offer(cs);
		return cs.getServerChannel();
	}

	/**
	 * Request to connect.
	 * Client editor use this method to connect SeMa. 
	 * @param cs
	 */
	public ChannelSimulator<P> connect(){
		BlockingQueue<P> rq = new LinkedBlockingQueue<P>();
		BlockingQueue<P> wq = new LinkedBlockingQueue<P>();
		ChannelSimulator<P> cs = new ChannelSimulator<P>(this, rq, wq);
		connectedList.offer(cs);
		return cs;
	}
		
	public boolean checkAllCS(){
		for(ChannelSimulator<P> cs: connectedList){
			if(!cs.readQisEmpty()) return false;
		}
		return true;
	}

	public ChannelSimulator<P> getAcceptedSession(int i) {
		return acceptedList.get(i).getServerChannel();
	}
}