Class BinaryMsgIface
java.lang.Object
com.tccc.kos.commons.core.service.blink.binarymsg.BinaryMsgIface
- All Implemented Interfaces:
Ready
Base class for a messaging interface over BinaryMsgSession. An interface
provides a standard collection of api's that can then be attached to a
session for use as the transport. The session will send the interface list to
the adapter and the adapter will send the interface list it supports to java.
This allows the interfaces to be dynamically mapped to interface numbers in
the protocol so that each interface remains isolated and allows each
interface to define up to 256 api's.
By breaking functionality out from sessions to interfaces it is possible to have different hardware support standard api's by simply linking the interface object to the session, allowing a single interface (such as embedded usb bootloader protocol) to be easily reused.
- Since:
- 1.0
- Version:
- 2022-08-31
-
Constructor Summary
ConstructorsConstructorDescriptionBinaryMsgIface(String name, BinaryMsgSession session, BinaryMsgIfaceListener<?> listener) Create a new interface for the specified session. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRequestHandler(int apiNum, BinaryMsgListener listener) Add a listener for a given request message identified by apiNum.intReturn the client input buffer size.intReturn the client output buffer size.intReturn the name of the interface.Return the nodeId the connection is from.getReady()Fetches the associated ready indicator.Return the remote address of the connection.Return the underlying session.intReturn iface protocol versionbooleanReturn true if the interface is connected to the client.booleanReturn true if this originates from the primary node.msg(int apiNum) Create an request message with the specified apiNum.voidCalled once the parent session is fully initialized and the iface is ready to be used.voidCalled when the underlying session has been disconnected but before the factory that created the iface is notified.voidSend a message to the attached client without waiting for a response.sendAndRecv(BinaryMsg msg) Send a message to the attached client and wait for a response using the default timeout.sendAndRecv(BinaryMsg msg, int timeout) Send a message to the attached client and wait for a response using the specified timeout.voidsendAndRecv(BinaryMsg msg, int timeout, BinaryMsgListener listener) Send a message to the attached client and send the response to the specified listener using the specified timeout.voidsendAndRecv(BinaryMsg msg, BinaryMsgListener listener) Send a message to the attached client and send the response to the specified listener with the default timeout.intsendAndRecvErr(BinaryMsg msg) Send a message to the attached client and wait for a response using the default timeout.intsendAndRecvErr(BinaryMsg msg, int timeout) Send a message to the attached client and wait for a response using the specified timeout.intsendAndRecvInt(BinaryMsg msg) Send a message to the attached client and wait for a response using the default timeout.intsendAndRecvInt(BinaryMsg msg, int timeout) Send a message to the attached client and wait for a response using the specified timeout.voidsetDefaultTimeout(int defaultTimeout) voidsetIfaceNum(int ifaceNum) voidsetVersion(int version)
-
Constructor Details
-
BinaryMsgIface
Create a new interface for the specified session.- Parameters:
name- the name of the interfacesession- the session this interface runs overlistener- optional listener to process connect / disconnect events
-
-
Method Details
-
getClientOutputBufSize
public int getClientOutputBufSize()Return the client output buffer size. This is the max amount of data, including message headers, that the client can send in a single message. When designing an api that can request variable size data from the client, this can be used to either limit the request size, or to break a large request into multiple smaller requests that will fit within the client buffer space. -
getClientInputBufSize
public int getClientInputBufSize()Return the client input buffer size. This is the max amount of data, including message headers, that the client can receive in a single message. When designing an api that can send variable size data to the client, this can be used to either limit the size of the send buffer, or to break a large send buffer into multiple smaller requests that will fit within the client receive space. -
getNodeId
Return the nodeId the connection is from.- Returns:
- the nodeId the connection is from
-
getVersion
public int getVersion()Return iface protocol version -
isPrimary
public boolean isPrimary()Return true if this originates from the primary node. -
getInterfaceName
Return the name of the interface.- Returns:
- the name of the interface
-
getSession
Return the underlying session. -
addRequestHandler
Add a listener for a given request message identified by apiNum. Only one listener can be added per apiNum. -
getRemoteAddr
Return the remote address of the connection. -
isConnected
public boolean isConnected()Return true if the interface is connected to the client. -
onConnect
Called once the parent session is fully initialized and the iface is ready to be used. This is called before any attached listener. Override as needed.- Throws:
Exception
-
onDisconnect
Called when the underlying session has been disconnected but before the factory that created the iface is notified. This is called before any attached listener. Override as needed.- Throws:
Exception
-
getReady
Description copied from interface:ReadyFetches the associated ready indicator. -
msg
Create an request message with the specified apiNum. This can be sent via send() or sendAndRecv().- Parameters:
apiNum- the api number- Throws:
IOException
-
send
Send a message to the attached client without waiting for a response.- Parameters:
msg- the message to send- Throws:
IOException
-
sendAndRecv
Send a message to the attached client and wait for a response using the default timeout.- Parameters:
msg- the message to send- Returns:
- the response message
- Throws:
TimeoutBinaryMsgExceptionIOException
-
sendAndRecv
Send a message to the attached client and wait for a response using the specified timeout.- Parameters:
msg- the message to sendtimeout- the timeout for the receive- Returns:
- the response message
- Throws:
TimeoutBinaryMsgExceptionIOException
-
sendAndRecv
Send a message to the attached client and send the response to the specified listener with the default timeout. If the timeout is reached, the listener will be called with null.- Parameters:
msg- the message to sendlistener- the listener to pass the response to- Throws:
IOException
-
sendAndRecv
Send a message to the attached client and send the response to the specified listener using the specified timeout. If the timeout is reached, the listener will be called with null.- Parameters:
msg- the message to sendtimeout- the timeout to use for receivelistener- the listener to pass the response to- Throws:
IOException
-
sendAndRecvInt
Send a message to the attached client and wait for a response using the default timeout. Extract an int from the response and return the value.- Parameters:
msg- the message to send- Returns:
- int value from the response input stream
- Throws:
TimeoutBinaryMsgExceptionIOException
-
sendAndRecvInt
Send a message to the attached client and wait for a response using the specified timeout. Extract an int from the response and return the value.- Parameters:
msg- the message to sendtimeout- the timeout for the receive- Returns:
- int value from the response input stream
- Throws:
TimeoutBinaryMsgExceptionIOException
-
sendAndRecvErr
Send a message to the attached client and wait for a response using the default timeout. Return the value of the error field from the response. By convention a value of zero means no error, negative values are indication of an error.- Parameters:
msg- the message to send- Returns:
- the error field from the response
- Throws:
TimeoutBinaryMsgExceptionIOException
-
sendAndRecvErr
Send a message to the attached client and wait for a response using the specified timeout. Return the value of the error field from the response. By convention a value of zero means no error, negative value are indication of an error.- Parameters:
msg- the message to sendtimeout- the timeout for the receive- Returns:
- the error field from the response
- Throws:
TimeoutBinaryMsgExceptionIOException
-
setIfaceNum
public void setIfaceNum(int ifaceNum) -
setVersion
public void setVersion(int version) -
getDefaultTimeout
public int getDefaultTimeout() -
setDefaultTimeout
public void setDefaultTimeout(int defaultTimeout)
-