jadex.platform.service.message.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 Show documentation
Show all versions of jadex-platform Show documentation
The Jadex platform package contains implementations of platform services as well as the platform component itself.
package jadex.platform.service.message.streams;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
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;
import jadex.commons.gui.PropertiesPanel;
import jadex.platform.service.message.MessageService;
/**
* 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(MessageService ms, Map nonfunc)
{
super(ms, 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(StreamSendTask.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(StreamSendTask.CLOSEREQ, null, null, nonfunc), StreamSendTask.CLOSEREQ)
.addResultListener(new ExceptionDelegationResultListener