jadex.bridge.component.streams.InputConnectionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-bridge Show documentation
Show all versions of jadex-platform-bridge Show documentation
Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.
package jadex.bridge.component.streams;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.annotation.Timeout;
import jadex.commons.Tuple2;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
/**
* Handler that sits between connection and message service.
* Is used by connection to forward user requests.
* Is used by the message service to signal arrived messages.
*/
public class InputConnectionHandler extends AbstractConnectionHandler implements IInputConnectionHandler
{
//-------- attributes --------
/** The last in order received sequence number. */
protected int rseqno;
/** The highest yet (may be out of order) received sequence number. */
protected int maxseqno;
/** The highest yet (may be out of order) acknowledged sequence number (only used to trigger new acks every x messages). */
protected int maxackseqno;
/** The maximum buffer size for out of order packets. */
protected int maxbuf;
/** The maximum bytes of data that can be stored in connection (without being consumed). */
protected int maxstored;
/** The data (stored here only as long as it is out of order or incomplete).
Ready data will be forwarded to the connection. Also remembers if an acknowledgement has been sent.*/
protected Map> data;
/** The last in order sequence number acknowledged. */
protected int lastack;
/** The number of received elements after which an ack is sent. */
protected int ackcnt;
/** The current timer. */
protected TimerTask datatimer;
/** The last sequence number. */
protected int lastseqno;
//-------- constructors --------
/**
* Create a new input connection handler.
*/
public InputConnectionHandler(IInternalAccess component, Map nonfunc)
{
super(component, nonfunc);
this.rseqno = 0;
this.maxseqno = 0;
this.maxbuf = 10000;
this.maxstored = 10000;
this.data = new HashMap>();
this.ackcnt = 10;
this.lastack = -1;
this.lastseqno = -1;
}
//-------- methods called from message service --------
/**
* From initiator.
*
* Called when a close message was received.
* participant acks and closes
*
* @param seqno The last data packet.
*/
public void closeReceived(final int seqno)
{
scheduleStep(new IComponentStep()
{
public IFuture execute(IInternalAccess ia)
{
// Remember that close message was received, close the connection and send an ack.
// System.out.println("close received: "+seqno+" "+rseqno+" "+getConnectionId());
sendTask(createTask(ACKCLOSE, null, null, nonfunc));
if(seqno==rseqno)
{
sendDataAck(); // send missing acks to speedup closing
if(!con.isClosed())
con.setClosed();
}
else
{
// closes later unilaterally
lastseqno = seqno;
}
return IFuture.DONE;
}
});
}
/**
* Called from connection.
* Initiates closing procedure (is different for initiator and participant).
*/
public IFuture doClose()
{
return scheduleStep(new IComponentStep()
{
public IFuture execute(IInternalAccess ia)
{
final Future ret = new Future();
// Send a close request
// System.out.println("do close input side");
try
{
// Needs nothing to do with ack response.
sendAcknowledgedMessage(createTask(CLOSEREQ, null, null, nonfunc), CLOSEREQ)
.addResultListener(new ExceptionDelegationResultListener