public abstract class ChannelAccessServerConnection extends AbstractSocketChannelConnection
Base class for a connection with a Channel Access server.
This class takes care of establishing the connection, negotiating the Channel Access protocol version, and monitoring the connection, sending echo requests when needed. If the server does not support Channel Access version 4.4 or newer, this class immediately destroys the connection.
With the exception of the AbstractSocketChannelConnection.destroy()
, getVersion()
, and
sendMessage(ChannelAccessMessage)
methods, this class is not safe
for use by multiple threads and its method may only be called within the
communication controller's thread.
Constructor and Description |
---|
ChannelAccessServerConnection(BeaconDetector beaconDetector,
java.nio.charset.Charset charset,
CommunicationController communicationController,
ErrorHandler errorHandler,
int maxReceiveSize,
int maxSendSize,
java.net.InetSocketAddress serverAddress,
ChannelAccessVersion version)
Creates a connection.
|
Modifier and Type | Method and Description |
---|---|
protected ChannelAccessVersion |
getVersion()
Returns the Channel Access protocol version supported by the server.
|
protected boolean |
isSendQueueEmpty()
Tells whether the send queue is empty.
|
protected void |
onConnect()
Called when the socket channel is connected.
|
protected void |
onConnectionResponsive()
Called when this connection becomes responsive.
|
protected void |
onConnectionUnresponsive()
Called when this connection becomes unresponsive.
|
protected void |
onDestroy()
Called when this connection has been destroyed.
|
protected abstract void |
onMessageReceived(ChannelAccessMessage message)
Called when a Channel Access message is received.
|
protected long |
onReceive()
Called when data is received.
|
protected void |
onReceiveFinished(boolean moreDataAvailable)
Called when a data receive operation is finished.
|
protected void |
onSend()
Called whenever data has been sent.
|
protected void |
sendMessage(ChannelAccessMessage message)
Adds a message to the queue of messages to be sent.
|
protected void |
verifyResponsePayloadSize(ChannelAccessValueType dataType,
int count)
Checks whether a value of the specified type and with the specified
number of elements can be received without the maximum payload size for
received messages.
|
destroy, getReceiveSource, getSendQueueSizeInBytes, getSendSink, isDestroyed, tryReceive, trySend
public ChannelAccessServerConnection(BeaconDetector beaconDetector, java.nio.charset.Charset charset, CommunicationController communicationController, ErrorHandler errorHandler, int maxReceiveSize, int maxSendSize, java.net.InetSocketAddress serverAddress, ChannelAccessVersion version) throws java.io.IOException
beaconDetector
- beacon detector for receiving beacons from the server. Those
beacons are used to assess the connection's health. If
null
, beacons are not used at all.charset
- character set to be used for encoding and decoding messages
(never null
).communicationController
- communication controller with which the socket channel is
registered (never null
). This is the
communication controller that drives all I/O and timer based
processing of the connection.errorHandler
- error handler for logging unexpected errors (never
null
).maxReceiveSize
- maximum size of a received Channel Access message in bytes
(including its headers). Must be at least 16400 bytes. When a
larger message is received, the connection is destroyed.maxSendSize
- maximum size of a sent Channel Access message in bytes
(including its headers). Must be at least 16400 bytes.serverAddress
- IP address and port number of the server to connect to (never
null
).version
- version of the Channel Access protocol to assume until the
version is received from the server (never null
).
Must be at least Channel Access version 4.4. When the server
sends a version that is older than Channel Access version 4.4,
the connection is destroyed immediately.java.io.IOException
- if the channel for the connection cannot be created.java.lang.IllegalArgumentException
- if the specified version
is too old or
maxReceiveSize
or maxSendSize
are
too small.protected void onConnect()
AbstractSocketChannelConnection
onConnect
in class AbstractSocketChannelConnection
protected void onConnectionResponsive()
onConnectionUnresponsive()
has been called
before. This method is guaranteed to be called in the communication
controller's thread. The implementation in the base class does nothing.
Derived classes can override it if they want to perform actions based on
the connection's responsiveness.protected void onConnectionUnresponsive()
onConnectionResponsive()
is called. This
method is guaranteed to be called in the communication controller's
thread. The implementation in the base class does nothing. Derived
classes can override it if they want to perform actions based on the
connection's responsiveness.protected void onDestroy()
AbstractSocketChannelConnection
AbstractSocketChannelConnection.destroy()
method is called explicitly. This
method will never be called more than once. This method is called in the
thread which calls the AbstractSocketChannelConnection.destroy()
method.onDestroy
in class AbstractSocketChannelConnection
protected long onReceive()
AbstractSocketChannelConnection
AbstractSocketChannelConnection.getReceiveSource()
. If this method returns a number greater than
zero, it will only be called again when at least this number of bytes
have been received (in addition to the bytes that already have been
received and are available from the byte source). This method is
guaranteed to be called in the communication controller's thread.onReceive
in class AbstractSocketChannelConnection
protected abstract void onMessageReceived(ChannelAccessMessage message)
message
- message that has been received from the server (never
null
).protected void onReceiveFinished(boolean moreDataAvailable)
AbstractSocketChannelConnection
onReceiveFinished
in class AbstractSocketChannelConnection
moreDataAvailable
- true
if more data is available from the channel
but the read operation has been interrupted because the limit
for the maximum number of contiguous bytes read has been
reached. false
if currently there is no more data
available from the channel.protected void onSend()
AbstractSocketChannelConnection
AbstractSocketChannelConnection.getSendQueueSizeInBytes()
. This method is
guaranteed to be called in the communication controller's thread.onSend
in class AbstractSocketChannelConnection
protected void sendMessage(ChannelAccessMessage message)
message
- message to be sent to the server (never null
).java.lang.IllegalArgumentException
- if this message is of a type that cannot be sent from a
client to a server via TCP.SerializedPayloadTooLargeException
- if the message cannot be sent because the payload would be
larger than the configured maximum payload send size or the
specified protocol version is older than Channel Access
version 4.9 and the payload would be larger than 65528 bytes
or the count exceeds 65535 elements.UnsupportedInThisVersionException
- if this message is not support in the protocol version
supported by the server.java.lang.UnsupportedOperationException
- if this message has been constructed by deserializing it from
headers only and thus it is incomplete.protected boolean isSendQueueEmpty()
true
if the send queue is empty, false
if there is still data to be sent.protected ChannelAccessVersion getVersion()
null
).protected void verifyResponsePayloadSize(ChannelAccessValueType dataType, int count)
dataType
- type of the value that shall be received.count
- number of elements that shall be received.ChannelAccessException
- with a status of
ECA_TOLARGE
if the payload size would exceed the configured
maximum payload size for received messages.Copyright © 2014–2017 aquenos GmbH. All rights reserved.