view src/pathfinder/mergetest/SelectorSimulator.java @ 151:1768e68ba98e

*** empty log message ***
author pin
date Tue, 05 Aug 2008 15:09:39 +0900
parents
children 6326e5ea4595
line wrap: on
line source

package pathfinder.mergetest;

import java.util.ArrayList;
import java.util.Set;

public class SelectorSimulator<P> {
	
	private ArrayList<SelectionKeySimulator<P>> keyList;
	private NetworkSimulator<P> ns;
	private ArrayList<SelectionKeySimulator<P>> selectedKeys;
	
	public SelectorSimulator(NetworkSimulator<P> _ns) {
		// TODO Auto-generated constructor stub
		ns = _ns;
		keyList = new ArrayList<SelectionKeySimulator<P>>();
	}

	public int select(){
		selectedKeys = new ArrayList<SelectionKeySimulator<P>>();
		
		synchronized(ns){
			boolean empty = false;
			for(SelectionKeySimulator<P> key : keyList){
				ChannelSimulator<P> channel = key.channel();
				if(channel.readQisEmpty()){
					empty = true;
				}else{
					empty = false;
					//selectedKeys = key;
					selectedKeys.add(key);
					break;
				}
			}
			
			try {
				if(empty) ns.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return selectedKeys.size();
	}
	
	public SelectionKeySimulator<P> register(ChannelSimulator<P> cs){
		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs);
		keyList.add(new SelectionKeySimulator<P>(cs));
		return key;
	}

	public ArrayList<SelectionKeySimulator<P>> selectedKeys() {
		
		return selectedKeys;
	}

}