Package jadex.bridge.component.streams
Class AbstractConnectionHandler
- java.lang.Object
-
- jadex.bridge.component.streams.AbstractConnectionHandler
-
- All Implemented Interfaces:
IAbstractConnectionHandler
- Direct Known Subclasses:
InputConnectionHandler,OutputConnectionHandler
public class AbstractConnectionHandler extends java.lang.Object implements IAbstractConnectionHandler
Abstract base class for connection handlers. Is called from the message service and the connection. Ensures that all calls from threads other than the message service component are scheduled on this thread to avoid multithreading issues in this class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractConnectionHandler.SendInfoStruct class that holds send information.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringACKCLOSEAcknowledge the close message.static byteACKCLOSE_INPUT_INITIATORAck for close - from initiator .static byteACKCLOSE_OUTPUT_PARTICIPANTAck data/close - from participant .static java.lang.StringACKCLOSEREQAcknowledge the close request.static byteACKCLOSEREQ_INPUT_PARTICIPANTAck for close request - from initiator .static byteACKCLOSEREQ_OUTPUT_INITIATORAck for close request - from initiator .static java.lang.StringACKDATAAcknowledge data message.static byteACKDATA_INPUT_INITIATORAck data - from participant .static byteACKDATA_OUTPUT_PARTICIPANTAck data/close - from participant .static java.lang.StringACKINITAcknowledge init.static byteACKINIT_INPUT_PARTICIPANTAck the init - from participant.static byteACKINIT_OUTPUT_PARTICIPANTAck the init - from initiator.protected longacktimeoutThe max delay before an acknowledgement is received.static java.lang.StringALIVEThe alive message.static byteALIVE_INITIATORAlive message - from initiator.static byteALIVE_PARTICIPANTAlive message - from participant.protected longalivetimeThe latest alive time.static java.lang.StringCLOSEClose the connection.static byteCLOSE_INPUT_PARTICIPANTClose connection - from participant.static byteCLOSE_OUTPUT_INITIATORClose connection - from initiator.static java.lang.StringCLOSEREQClose request (from participant which cannot close itself).static byteCLOSEREQ_INPUT_INITIATORClose request connection - from initiator.static byteCLOSEREQ_OUTPUT_PARTICIPANTRequest close connection - from participant.protected IInternalAccesscomponentThe message service.protected AbstractConnectionconThe connection.static java.lang.StringDATASend data message.static byteDATA_INPUT_PARTICIPANTSend data - from participant.static byteDATA_OUTPUT_INITIATORSend data - from initiator.static byte[]EMPTY_BYTE_ARRAYstatic java.lang.StringINITInit a connection.static byteINIT_INPUT_INITIATORCreate virtual input connection - from initiator.static byteINIT_OUTPUT_INITIATORCreate virtual output connection - from initiator.protected longleasetimeThe lease time.protected intmaxresendsThe maximum number of resends.static byteMESSAGE_TYPE_STREAMThe message type for streams.static java.util.Map<Tuple,java.lang.Byte>MESSAGETYPESString type, boolean input, boolean initiator.protected java.util.Map<java.lang.String,java.lang.Object>nonfuncThe non-functional properties.protected java.util.TimertimerThe timer.protected java.util.Map<java.lang.Object,AbstractConnectionHandler.SendInfo>unackedThe unacknowledged messages.
-
Constructor Summary
Constructors Constructor Description AbstractConnectionHandler(IInternalAccess component, java.util.Map<java.lang.String,java.lang.Object> nonfunc)Create a new connection handler.AbstractConnectionHandler(IInternalAccess component, java.util.Map<java.lang.String,java.lang.Object> nonfunc, int maxresends, long acktimeout, long leasetime)Create a new connection handler.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidackReceived(java.lang.Object id, java.lang.Object content)Called when an ack was received.voidclose()Close the connection.protected java.util.TimerTaskcreateAckTimer(java.lang.Object id)Triggers resends of packets if no ack has been received in acktimeout.protected StreamPacketcreateTask(java.lang.String type, byte[] content, java.lang.Integer seqnumber, java.util.Map<java.lang.String,java.lang.Object> nonfunc)Create a new task.protected StreamPacketcreateTask(java.lang.String type, java.lang.Object content, boolean useserializer, java.lang.Integer seqnumber, java.util.Map<java.lang.String,java.lang.Object> nonfunc)Create a new task.IFuture<java.lang.Void>doClose()Called from connection.IInternalAccessgetComponent()Get the component.protected AbstractConnectiongetConnection()Get the connection.intgetConnectionId()Get the id.IExecutionFeaturegetExecutionFeature()Get the execution feature.protected longgetLeasetime()Get the leasetime.IMessageFeaturegetMessageFeature()Get the message feature.protected bytegetMessageType(java.lang.String type)Get the message type for a given type, e.g.static bytegetMessageType(java.lang.String type, boolean input, boolean initiator)Get the message type.java.util.Map<java.lang.String,java.lang.Object>getNonFunctionalProperties()Get the non-functional properties.voidinitReceived()Received the init message.booleanisClosed()Get the closed.booleanisConnectionAlive()Test if the connection is alive.voidnotifyInited()protected <E> IFuture<E>scheduleStep(IComponentStep<E> step)Schedule a step on the message service component.protected IFuture<java.lang.Object>sendAcknowledgedMessage(StreamPacket task, java.lang.Object id)Send a message and wait for an ack.IFuture<java.lang.Void>sendAlive()Send alive message.IFuture<java.lang.Void>sendInit()Send init message.protected IFuture<java.lang.Void>sendTask(StreamPacket task)Send a task.voidsetAliveTime(long alivetime)Set the alive time of the other connection side.voidsetConnection(AbstractConnection con)Set the connection (needed as connection and handler need each other).voidsetNonFunctionalProperties(java.util.Map<java.lang.String,java.lang.Object> nonfunc)Set the non-functional properties.java.util.TimerTaskwaitForRealDelay(long delay, IComponentStep<?> step)Wait for a time delay on the (real) system clock.
-
-
-
Field Detail
-
MESSAGE_TYPE_STREAM
public static final byte MESSAGE_TYPE_STREAM
The message type for streams.- See Also:
- Constant Field Values
-
INIT
public static final java.lang.String INIT
Init a connection.- See Also:
- Constant Field Values
-
ACKINIT
public static final java.lang.String ACKINIT
Acknowledge init.- See Also:
- Constant Field Values
-
DATA
public static final java.lang.String DATA
Send data message.- See Also:
- Constant Field Values
-
ACKDATA
public static final java.lang.String ACKDATA
Acknowledge data message.- See Also:
- Constant Field Values
-
CLOSE
public static final java.lang.String CLOSE
Close the connection.- See Also:
- Constant Field Values
-
ACKCLOSE
public static final java.lang.String ACKCLOSE
Acknowledge the close message.- See Also:
- Constant Field Values
-
CLOSEREQ
public static final java.lang.String CLOSEREQ
Close request (from participant which cannot close itself).- See Also:
- Constant Field Values
-
ACKCLOSEREQ
public static final java.lang.String ACKCLOSEREQ
Acknowledge the close request.- See Also:
- Constant Field Values
-
ALIVE
public static final java.lang.String ALIVE
The alive message.- See Also:
- Constant Field Values
-
INIT_OUTPUT_INITIATOR
public static final byte INIT_OUTPUT_INITIATOR
Create virtual output connection - from initiator.- See Also:
- Constant Field Values
-
ACKINIT_OUTPUT_PARTICIPANT
public static final byte ACKINIT_OUTPUT_PARTICIPANT
Ack the init - from initiator.- See Also:
- Constant Field Values
-
DATA_OUTPUT_INITIATOR
public static final byte DATA_OUTPUT_INITIATOR
Send data - from initiator.- See Also:
- Constant Field Values
-
ACKDATA_OUTPUT_PARTICIPANT
public static final byte ACKDATA_OUTPUT_PARTICIPANT
Ack data/close - from participant .- See Also:
- Constant Field Values
-
CLOSEREQ_OUTPUT_PARTICIPANT
public static final byte CLOSEREQ_OUTPUT_PARTICIPANT
Request close connection - from participant.- See Also:
- Constant Field Values
-
ACKCLOSEREQ_OUTPUT_INITIATOR
public static final byte ACKCLOSEREQ_OUTPUT_INITIATOR
Ack for close request - from initiator .- See Also:
- Constant Field Values
-
CLOSE_OUTPUT_INITIATOR
public static final byte CLOSE_OUTPUT_INITIATOR
Close connection - from initiator.- See Also:
- Constant Field Values
-
ACKCLOSE_OUTPUT_PARTICIPANT
public static final byte ACKCLOSE_OUTPUT_PARTICIPANT
Ack data/close - from participant .- See Also:
- Constant Field Values
-
INIT_INPUT_INITIATOR
public static final byte INIT_INPUT_INITIATOR
Create virtual input connection - from initiator.- See Also:
- Constant Field Values
-
ACKINIT_INPUT_PARTICIPANT
public static final byte ACKINIT_INPUT_PARTICIPANT
Ack the init - from participant.- See Also:
- Constant Field Values
-
DATA_INPUT_PARTICIPANT
public static final byte DATA_INPUT_PARTICIPANT
Send data - from participant.- See Also:
- Constant Field Values
-
ACKDATA_INPUT_INITIATOR
public static final byte ACKDATA_INPUT_INITIATOR
Ack data - from participant .- See Also:
- Constant Field Values
-
CLOSEREQ_INPUT_INITIATOR
public static final byte CLOSEREQ_INPUT_INITIATOR
Close request connection - from initiator.- See Also:
- Constant Field Values
-
ACKCLOSEREQ_INPUT_PARTICIPANT
public static final byte ACKCLOSEREQ_INPUT_PARTICIPANT
Ack for close request - from initiator .- See Also:
- Constant Field Values
-
CLOSE_INPUT_PARTICIPANT
public static final byte CLOSE_INPUT_PARTICIPANT
Close connection - from participant.- See Also:
- Constant Field Values
-
ACKCLOSE_INPUT_INITIATOR
public static final byte ACKCLOSE_INPUT_INITIATOR
Ack for close - from initiator .- See Also:
- Constant Field Values
-
ALIVE_INITIATOR
public static final byte ALIVE_INITIATOR
Alive message - from initiator.- See Also:
- Constant Field Values
-
ALIVE_PARTICIPANT
public static final byte ALIVE_PARTICIPANT
Alive message - from participant.- See Also:
- Constant Field Values
-
MESSAGETYPES
public static final java.util.Map<Tuple,java.lang.Byte> MESSAGETYPES
String type, boolean input, boolean initiator.
-
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY
-
component
protected IInternalAccess component
The message service.
-
con
protected AbstractConnection con
The connection.
-
alivetime
protected long alivetime
The latest alive time.
-
leasetime
protected long leasetime
The lease time.
-
unacked
protected java.util.Map<java.lang.Object,AbstractConnectionHandler.SendInfo> unacked
The unacknowledged messages.
-
maxresends
protected int maxresends
The maximum number of resends.
-
acktimeout
protected long acktimeout
The max delay before an acknowledgement is received.
-
nonfunc
protected java.util.Map<java.lang.String,java.lang.Object> nonfunc
The non-functional properties.
-
timer
protected java.util.Timer timer
The timer.
-
-
Constructor Detail
-
AbstractConnectionHandler
public AbstractConnectionHandler(IInternalAccess component, java.util.Map<java.lang.String,java.lang.Object> nonfunc)
Create a new connection handler.
-
AbstractConnectionHandler
public AbstractConnectionHandler(IInternalAccess component, java.util.Map<java.lang.String,java.lang.Object> nonfunc, int maxresends, long acktimeout, long leasetime)
Create a new connection handler.
-
-
Method Detail
-
getMessageType
public static byte getMessageType(java.lang.String type, boolean input, boolean initiator)Get the message type.- Parameters:
type- The type.input- Flag if in input connection.initiator- Flag if is initiator side.
-
setConnection
public void setConnection(AbstractConnection con)
Set the connection (needed as connection and handler need each other). The connections uses this method to set itself as connection in their constructor.- Specified by:
setConnectionin interfaceIAbstractConnectionHandler
-
initReceived
public void initReceived()
Received the init message.
-
ackReceived
public void ackReceived(java.lang.Object id, java.lang.Object content)Called when an ack was received. The id is used to identify the original message task. This will cancel the timer (resending) task and call the future of sendAcknowledgedMessage().
-
close
public void close()
Close the connection. Notifies the other side that the connection has been closed.
-
setAliveTime
public void setAliveTime(long alivetime)
Set the alive time of the other connection side.- Parameters:
alivetime- The alive time.
-
isConnectionAlive
public boolean isConnectionAlive()
Test if the connection is alive.- Returns:
- True, if is alive.
-
isClosed
public boolean isClosed()
Get the closed.- Returns:
- The closed.
-
getConnectionId
public int getConnectionId()
Get the id.- Returns:
- the id.
-
doClose
public IFuture<java.lang.Void> doClose()
Called from connection. Initiates closing procedure (is different for initiator and participant).- Specified by:
doClosein interfaceIAbstractConnectionHandler
-
sendInit
public IFuture<java.lang.Void> sendInit()
Send init message.- Specified by:
sendInitin interfaceIAbstractConnectionHandler
-
sendAlive
public IFuture<java.lang.Void> sendAlive()
Send alive message.
-
notifyInited
public void notifyInited()
- Specified by:
notifyInitedin interfaceIAbstractConnectionHandler
-
getConnection
protected AbstractConnection getConnection()
Get the connection.- Returns:
- The connection.
-
getNonFunctionalProperties
public java.util.Map<java.lang.String,java.lang.Object> getNonFunctionalProperties()
Get the non-functional properties.- Specified by:
getNonFunctionalPropertiesin interfaceIAbstractConnectionHandler- Returns:
- The non-functional properties.
-
setNonFunctionalProperties
public void setNonFunctionalProperties(java.util.Map<java.lang.String,java.lang.Object> nonfunc)
Set the non-functional properties.- Parameters:
nonfunc- The non-functional properties.
-
getMessageType
protected byte getMessageType(java.lang.String type)
Get the message type for a given type, e.g. init, close (defined in StreamSendTask).- Parameters:
type- The message type.- Returns:
- The message type for message sending.
-
createTask
protected StreamPacket createTask(java.lang.String type, byte[] content, java.lang.Integer seqnumber, java.util.Map<java.lang.String,java.lang.Object> nonfunc)
Create a new task.- Parameters:
type- The message type.content- The content.seqnumber- The sequence number.- Returns:
- The task for sending the message.
-
createTask
protected StreamPacket createTask(java.lang.String type, java.lang.Object content, boolean useserializer, java.lang.Integer seqnumber, java.util.Map<java.lang.String,java.lang.Object> nonfunc)
Create a new task.- Parameters:
type- The message type.content- The content.usecodecs- Flag if codecs should be used to encode the content.seqnumber- The sequence number.- Returns:
- The task for sending the message.
-
sendTask
protected IFuture<java.lang.Void> sendTask(StreamPacket task)
Send a task.- Parameters:
task- The task.
-
sendAcknowledgedMessage
protected IFuture<java.lang.Object> sendAcknowledgedMessage(StreamPacket task, java.lang.Object id)
Send a message and wait for an ack. Automatically resends messages () if not is ack is received within acktimeout.- Parameters:
task- The send task.id- The id of the call (e.g. sequence number).- Returns:
- Exception if sending and resending failed.
-
getLeasetime
protected long getLeasetime()
Get the leasetime.- Returns:
- the leasetime.
-
scheduleStep
protected <E> IFuture<E> scheduleStep(IComponentStep<E> step)
Schedule a step on the message service component.
-
createAckTimer
protected java.util.TimerTask createAckTimer(java.lang.Object id)
Triggers resends of packets if no ack has been received in acktimeout.- Parameters:
id- The message id.- Returns:
- The timer.
-
waitForRealDelay
public java.util.TimerTask waitForRealDelay(long delay, IComponentStep<?> step)Wait for a time delay on the (real) system clock.
-
getComponent
public IInternalAccess getComponent()
Get the component.- Returns:
- the component
-
getMessageFeature
public IMessageFeature getMessageFeature()
Get the message feature.
-
getExecutionFeature
public IExecutionFeature getExecutionFeature()
Get the execution feature.
-
-