/* Copyright (c) 1997-2004
 Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
 http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
 Free Software Foundation; either version 2, or (at your option) any
 later version: http://www.gnu.org/licenses/gpl.txt.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 $Project: polymake $$Id: PolymakeControl.java 6537 2005-11-18 17:00:40Z wotzlaw $
 */

package de.tuberlin.polymake.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.util.HashMap;

/**
 * This class ???
 * 
 * @author Thilo Schröder
 */
public abstract class PolymakeControl {	
	public final static String CHARSET = "ISO-8859-1";
	
	protected boolean alive = true;
	protected boolean answer = false;
	protected SimpleGeometryParser parser;
	
	protected BufferedReader clientReader;
	protected Pipe.SinkChannel pipeSink;
	
	protected HashMap frameMap = new HashMap(8);
	protected MsgQueue clientQueue = new MsgQueue();
	protected ByteBuffer bbuf = ByteBuffer.allocate(32);
	
	public PolymakeControl( BufferedReader psReader, BufferedReader clientReader, 
			Pipe.SinkChannel sink) throws IOException{
		this.clientReader = clientReader;
		pipeSink = sink;
		
		bbuf.putInt(1);
	}
	
	public abstract void update() throws IOException;
	
	public int getNumberOfFrames() {
		return frameMap.size();
	}
	
	public void removeFrame(String title) throws IOException {
		answer = false;
		frameMap.remove(title);
		if(frameMap.size() == 0) {
			alive = false;
			bbuf.flip();
			pipeSink.write(bbuf);
		}
	}
	
	public java.awt.Component getFrame(String title) throws IOException {
		return (java.awt.Component)frameMap.get(title);
	}
	
	public void putMessage(String msg, boolean ans) throws IOException {
		synchronized(clientQueue) {
			answer = ans;
			boolean notifyClient = clientQueue.isEmpty();
			clientQueue.pushBack(msg);
			if(notifyClient) {
				bbuf.flip();
				pipeSink.write(bbuf);
			}
		}
	}
	
	public void popMessage() {
		synchronized(clientQueue) {
			if(!clientQueue.isEmpty()) 
				clientQueue.popFront();
		}
	}
	
	public synchronized void setAnswer(boolean ans) {
		answer = ans;
	} 
	
	public synchronized boolean getAnswer() {
		return answer;
	}
	
	public boolean isAlive() {
		return alive;
	}
	
	public String getMessage() {
		synchronized(clientQueue) {
			return (String)clientQueue.front();
		}
	}
}


syntax highlighted by Code2HTML, v. 0.9.1