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 class
AbstractConnectionHandler.SendInfo
Struct class that holds send information.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ACKCLOSE
Acknowledge the close message.static byte
ACKCLOSE_INPUT_INITIATOR
Ack for close - from initiator .static byte
ACKCLOSE_OUTPUT_PARTICIPANT
Ack data/close - from participant .static java.lang.String
ACKCLOSEREQ
Acknowledge the close request.static byte
ACKCLOSEREQ_INPUT_PARTICIPANT
Ack for close request - from initiator .static byte
ACKCLOSEREQ_OUTPUT_INITIATOR
Ack for close request - from initiator .static java.lang.String
ACKDATA
Acknowledge data message.static byte
ACKDATA_INPUT_INITIATOR
Ack data - from participant .static byte
ACKDATA_OUTPUT_PARTICIPANT
Ack data/close - from participant .static java.lang.String
ACKINIT
Acknowledge init.static byte
ACKINIT_INPUT_PARTICIPANT
Ack the init - from participant.static byte
ACKINIT_OUTPUT_PARTICIPANT
Ack the init - from initiator.protected long
acktimeout
The max delay before an acknowledgement is received.static java.lang.String
ALIVE
The alive message.static byte
ALIVE_INITIATOR
Alive message - from initiator.static byte
ALIVE_PARTICIPANT
Alive message - from participant.protected long
alivetime
The latest alive time.static java.lang.String
CLOSE
Close the connection.static byte
CLOSE_INPUT_PARTICIPANT
Close connection - from participant.static byte
CLOSE_OUTPUT_INITIATOR
Close connection - from initiator.static java.lang.String
CLOSEREQ
Close request (from participant which cannot close itself).static byte
CLOSEREQ_INPUT_INITIATOR
Close request connection - from initiator.static byte
CLOSEREQ_OUTPUT_PARTICIPANT
Request close connection - from participant.protected IInternalAccess
component
The message service.protected AbstractConnection
con
The connection.static java.lang.String
DATA
Send data message.static byte
DATA_INPUT_PARTICIPANT
Send data - from participant.static byte
DATA_OUTPUT_INITIATOR
Send data - from initiator.static byte[]
EMPTY_BYTE_ARRAY
static java.lang.String
INIT
Init a connection.static byte
INIT_INPUT_INITIATOR
Create virtual input connection - from initiator.static byte
INIT_OUTPUT_INITIATOR
Create virtual output connection - from initiator.protected long
leasetime
The lease time.protected int
maxresends
The maximum number of resends.static byte
MESSAGE_TYPE_STREAM
The message type for streams.static java.util.Map<Tuple,java.lang.Byte>
MESSAGETYPES
String type, boolean input, boolean initiator.protected java.util.Map<java.lang.String,java.lang.Object>
nonfunc
The non-functional properties.protected java.util.Timer
timer
The timer.protected java.util.Map<java.lang.Object,AbstractConnectionHandler.SendInfo>
unacked
The 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 void
ackReceived(java.lang.Object id, java.lang.Object content)
Called when an ack was received.void
close()
Close the connection.protected java.util.TimerTask
createAckTimer(java.lang.Object id)
Triggers resends of packets if no ack has been received in acktimeout.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.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.IFuture<java.lang.Void>
doClose()
Called from connection.IInternalAccess
getComponent()
Get the component.protected AbstractConnection
getConnection()
Get the connection.int
getConnectionId()
Get the id.IExecutionFeature
getExecutionFeature()
Get the execution feature.protected long
getLeasetime()
Get the leasetime.IMessageFeature
getMessageFeature()
Get the message feature.protected byte
getMessageType(java.lang.String type)
Get the message type for a given type, e.g.static byte
getMessageType(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.void
initReceived()
Received the init message.boolean
isClosed()
Get the closed.boolean
isConnectionAlive()
Test if the connection is alive.void
notifyInited()
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.void
setAliveTime(long alivetime)
Set the alive time of the other connection side.void
setConnection(AbstractConnection con)
Set the connection (needed as connection and handler need each other).void
setNonFunctionalProperties(java.util.Map<java.lang.String,java.lang.Object> nonfunc)
Set the non-functional properties.java.util.TimerTask
waitForRealDelay(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:
setConnection
in 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:
doClose
in interfaceIAbstractConnectionHandler
-
sendInit
public IFuture<java.lang.Void> sendInit()
Send init message.- Specified by:
sendInit
in interfaceIAbstractConnectionHandler
-
sendAlive
public IFuture<java.lang.Void> sendAlive()
Send alive message.
-
notifyInited
public void notifyInited()
- Specified by:
notifyInited
in 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:
getNonFunctionalProperties
in 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.
-
-