java.lang.Object
com.tccc.kos.commons.core.service.blink.binarymsg.BinaryMsgIface
All Implemented Interfaces:
Ready

public abstract class BinaryMsgIface extends Object implements 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 Details

    • BinaryMsgIface

      public BinaryMsgIface(String name, BinaryMsgSession session, BinaryMsgIfaceListener<?> listener)
      Create a new interface for the specified session.
      Parameters:
      name - the name of the interface
      session - the session this interface runs over
      listener - 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

      public NodeId 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

      public String getInterfaceName()
      Return the name of the interface.
      Returns:
      the name of the interface
    • getSession

      public BinaryMsgSession getSession()
      Return the underlying session.
    • addRequestHandler

      public void addRequestHandler(int apiNum, BinaryMsgListener listener)
      Add a listener for a given request message identified by apiNum. Only one listener can be added per apiNum.
    • getRemoteAddr

      public InetSocketAddress getRemoteAddr()
      Return the remote address of the connection.
    • isConnected

      public boolean isConnected()
      Return true if the interface is connected to the client.
    • onConnect

      public void onConnect() throws Exception
      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

      public void onDisconnect() throws Exception
      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

      public ReadyIndicator getReady()
      Description copied from interface: Ready
      Fetches the associated ready indicator.
      Specified by:
      getReady in interface Ready
      Returns:
      the associated ReadyIndicator object
    • msg

      public BinaryMsg msg(int apiNum) throws IOException
      Create an request message with the specified apiNum. This can be sent via send() or sendAndRecv().
      Parameters:
      apiNum - the api number
      Throws:
      IOException
    • send

      public void send(BinaryMsg msg) throws IOException
      Send a message to the attached client without waiting for a response.
      Parameters:
      msg - the message to send
      Throws:
      IOException
    • sendAndRecv

      public BinaryMsg sendAndRecv(BinaryMsg msg) throws IOException
      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:
      TimeoutBinaryMsgException
      IOException
    • sendAndRecv

      public BinaryMsg sendAndRecv(BinaryMsg msg, int timeout) throws IOException
      Send a message to the attached client and wait for a response using the specified timeout.
      Parameters:
      msg - the message to send
      timeout - the timeout for the receive
      Returns:
      the response message
      Throws:
      TimeoutBinaryMsgException
      IOException
    • sendAndRecv

      public void sendAndRecv(BinaryMsg msg, BinaryMsgListener listener) throws IOException
      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 send
      listener - the listener to pass the response to
      Throws:
      IOException
    • sendAndRecv

      public void sendAndRecv(BinaryMsg msg, int timeout, BinaryMsgListener listener) throws IOException
      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 send
      timeout - the timeout to use for receive
      listener - the listener to pass the response to
      Throws:
      IOException
    • sendAndRecvInt

      public int sendAndRecvInt(BinaryMsg msg) throws IOException
      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:
      TimeoutBinaryMsgException
      IOException
    • sendAndRecvInt

      public int sendAndRecvInt(BinaryMsg msg, int timeout) throws IOException
      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 send
      timeout - the timeout for the receive
      Returns:
      int value from the response input stream
      Throws:
      TimeoutBinaryMsgException
      IOException
    • sendAndRecvErr

      public int sendAndRecvErr(BinaryMsg msg) throws IOException
      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:
      TimeoutBinaryMsgException
      IOException
    • sendAndRecvErr

      public int sendAndRecvErr(BinaryMsg msg, int timeout) throws IOException
      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 send
      timeout - the timeout for the receive
      Returns:
      the error field from the response
      Throws:
      TimeoutBinaryMsgException
      IOException
    • setIfaceNum

      public void setIfaceNum(int ifaceNum)
    • setVersion

      public void setVersion(int version)
    • getDefaultTimeout

      public int getDefaultTimeout()
    • setDefaultTimeout

      public void setDefaultTimeout(int defaultTimeout)