All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.platform.service.message.streams.LocalOutputConnectionHandler Maven / Gradle / Ivy

package jadex.platform.service.message.streams;

import java.util.Map;

import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

/**
 * 
 */
public class LocalOutputConnectionHandler extends LocalAbstractConnectionHandler 
	implements IOutputConnectionHandler
{
	/** The maximum bytes of data that can be stored in connection (without being consumed). */
	protected int maxstored;
	
	/** The ready future. */
	protected Future readyfuture;
	
	/**
	 * 
	 */
	public LocalOutputConnectionHandler(Map nonfunc)
	{
		this(nonfunc, null);
	}
	
	/**
	 * 
	 */
	public LocalOutputConnectionHandler(Map nonfunc, LocalAbstractConnectionHandler conhandler)
	{
		super(nonfunc, conhandler);
	}

	//-------- methods called from connection --------
	
	/**
	 *  Called from connection.
	 */
	public IFuture send(final byte[] data)
	{
		((LocalInputConnectionHandler)getConnectionHandler()).dataReceived(data);
		return IFuture.DONE;
	}

	/**
	 *  Flush the data.
	 */
	public void flush()
	{
	}
	
	/**
	 *  Wait until the connection is ready for the next write.
	 *  @return Calls future when next data can be written.
	 */
	public IFuture waitForReady()
	{
		Future ret = null;
		
		int allowed = ((LocalInputConnectionHandler)getConnectionHandler()).getAllowedSendSize();
		
//		System.out.println("allowed: "+allowed);
		
		if(allowed>0)
		{
			ret = new Future(Integer.valueOf(allowed));
		}
		else
		{
			if(readyfuture==null)
			{
				readyfuture = new Future();
			}
			ret = readyfuture;
		}
		
		return ret;
	}
	
	/**
	 *  Called by local input connection handler to signal
	 *  that user has read some data.
	 */
	public void ready(int available)
	{
		if(readyfuture!=null)
		{
//			System.out.println("ready: "+available);
			Future fut = readyfuture;
			readyfuture = null;
			fut.setResult(Integer.valueOf(available));
		}
	}
	
//	/**
//	 *  Test if stop is activated (too much data arrived).
//	 */
//	protected boolean isStop()
//	{
//		return getInputConnection().getStoredDataSize()>=maxstored;
//	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy