Package convex.net

Class Connection

java.lang.Object
convex.net.Connection

public class Connection extends Object

Class representing a Connection between network participants.

Sent messages are sent asynchronously via the shared client selector.

Received messages are read by the shared client selector, converted into Message instances, and passed to a Consumer for handling.

A Connection "owns" the ByteChannel associated with this Peer connection

  • Method Details

    • create

      public static Connection create(ByteChannel channel, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey) throws IOException
      Create a PeerConnection using an existing channel. Does not perform any connection initialisation: channel should already be connected.
      Parameters:
      channel - Byte channel to wrap
      receiveAction - Consumer to be called when a Message is received
      store - Store to use when receiving messages.
      trustedPeerKey - Trusted peer account key if this is a trusted connection, if not then null*
      Returns:
      New Connection instance
      Throws:
      IOException - If IO error occurs
    • getCounter

      public static long getCounter()
      Gets the global message ID counter
      Returns:
      Message ID counter for last message sent
    • connect

      public static Connection connect(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store) throws IOException, TimeoutException
      Create a PeerConnection by connecting to a remote address
      Parameters:
      hostAddress - Internet Address to connect to
      receiveAction - A callback Consumer to be called for any received messages on this connection
      store - Store to use for this Connection
      Returns:
      New Connection instance
      Throws:
      IOException - If connection fails because of any IO problem
      TimeoutException - If connection cannot be established within an acceptable time (~5s)
    • connect

      public static Connection connect(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey) throws IOException, TimeoutException
      Create a Connection by connecting to a remote address
      Parameters:
      hostAddress - Internet Address to connect to
      receiveAction - A callback Consumer to be called for any received messages on this connection
      store - Store to use for this Connection
      trustedPeerKey - Trusted peer account key if this is a trusted connection, if not then null
      Returns:
      New Connection instance
      Throws:
      IOException - If connection fails because of any IO problem
      TimeoutException - If the connection cannot be established within the timeout period
    • connect

      public static Connection connect(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey, int sendBufferSize, int receiveBufferSize) throws IOException, TimeoutException
      Create a Connection by connecting to a remote address
      Parameters:
      hostAddress - Internet Address to connect to
      receiveAction - A callback Consumer to be called for any received messages on this connection
      store - Store to use for this Connection
      trustedPeerKey - Trusted peer account key if this is a trusted connection, if not then null
      sendBufferSize - Size of connection send buffer in bytes
      receiveBufferSize - Size of connection receive buffer in bytes
      Returns:
      New Connection instance
      Throws:
      IOException - If connection fails because of any IO problem
      TimeoutException - If the connection cannot be established within the timeout period
    • getReceivedCount

      public long getReceivedCount()
    • getRemoteAddress

      public InetSocketAddress getRemoteAddress()
      Returns the remote SocketAddress associated with this connection, or null if not available
      Returns:
      An InetSocketAddress if associated, otherwise null
    • getStore

      public AStore getStore()
      Gets the store associated with this Connection
      Returns:
      Store instance
    • getLocalAddress

      public InetSocketAddress getLocalAddress()
      Returns the local SocketAddress associated with this connection, or null if not available
      Returns:
      A SocketAddress if associated, otherwise null
    • sendData

      public boolean sendData(ACell value) throws IOException
      Sends a DATA Message on this connection.
      Parameters:
      value - Any data object, which will be encoded and sent as a single cell
      Returns:
      true if buffered successfully, false otherwise (not sent)
      Throws:
      IOException - If IO error occurs
    • sendMissingData

      public boolean sendMissingData(Hash value) throws IOException
      Sends a DATA Message on this connection.
      Parameters:
      value - Any data object
      Returns:
      true if buffered successfully, false otherwise (not sent)
      Throws:
      IOException - If IO error occurs
    • sendQuery

      public long sendQuery(ACell form) throws IOException
      Sends a QUERY Message on this connection with a null Address
      Parameters:
      form - A data object representing the query form
      Returns:
      The ID of the message sent, or -1 if send buffer is full.
      Throws:
      IOException - If IO error occurs
    • sendQuery

      public long sendQuery(ACell form, Address address) throws IOException
      Sends a QUERY Message on this connection.
      Parameters:
      form - A data object representing the query form
      address - The address with which to run the query, which may be null
      Returns:
      The ID of the message sent, or -1 if send buffer is full.
      Throws:
      IOException - If IO error occurs
    • sendStatusRequest

      public long sendStatusRequest() throws IOException
      Sends a STATUS Request Message on this connection.
      Returns:
      The ID of the message sent, or -1 if send buffer is full.
      Throws:
      IOException - If IO error occurs
    • sendChallenge

      public long sendChallenge(SignedData<ACell> challenge) throws IOException
      Sends a CHALLENGE Request Message on this connection.
      Parameters:
      challenge - Challenge a Vector that has been signed by the sending peer.
      Returns:
      The ID of the message sent, or -1 if the message cannot be sent.
      Throws:
      IOException - If IO error occurs
    • sendResponse

      public long sendResponse(SignedData<ACell> response) throws IOException
      Sends a RESPONSE Request Message on this connection.
      Parameters:
      response - Signed response for the remote peer
      Returns:
      The ID of the message sent, or -1 if the message cannot be sent.
      Throws:
      IOException - If IO error occurs
    • sendTransaction

      public long sendTransaction(SignedData<ATransaction> signed) throws IOException
      Sends a transaction if possible, returning the message ID (greater than zero) if successful. Uses the configured CLIENT_STORE to store the transaction, so that any missing data requests from the server can be honoured. Returns -1 if the message could not be sent because of a full buffer.
      Parameters:
      signed - Signed transaction
      Returns:
      Message ID of the transaction request, or -1 if send buffer is full.
      Throws:
      IOException - In the event of an IO error, e.g. closed connection
    • sendResult

      public boolean sendResult(CVMLong id, ACell value) throws IOException
      Sends a RESULT Message on this connection with no error code (i.e. a success)
      Parameters:
      id - ID for result message
      value - Any data object
      Returns:
      True if buffered for sending successfully, false otherwise
      Throws:
      IOException - If IO error occurs
    • sendResult

      public boolean sendResult(CVMLong id, ACell value, ACell errorCode) throws IOException
      Sends a RESULT Message on this connection.
      Parameters:
      id - ID for result message
      value - Any data object
      errorCode - Error code for this result. May be null to indicate success
      Returns:
      True if buffered for sending successfully, false otherwise
      Throws:
      IOException - In case of IO Error
    • sendResult

      public boolean sendResult(Result result) throws IOException
      Sends a RESULT Message on this connection.
      Parameters:
      result - Result data structure
      Returns:
      true if message queued successfully, false otherwise
      Throws:
      IOException - If IO error occurs
    • sendMessage

      public boolean sendMessage(Message msg) throws IOException
      Sends a message over this connection
      Parameters:
      msg - Message to send
      Returns:
      true if message buffered successfully, false if failed
      Throws:
      IOException - If IO error occurs
    • sendObject

      public boolean sendObject(MessageType type, ACell payload) throws IOException
      Sends a payload for the given message type. Should be called on the thread that responds to missing data messages from the destination.
      Parameters:
      type - Type of message
      payload - Payload value for message
      Returns:
      true if message queued successfully, false otherwise
      Throws:
      IOException - If IO error occurs
    • close

      public void close()
    • isClosed

      public boolean isClosed()
      Checks if this connection is closed (i.e. the underlying channel is closed)
      Returns:
      true if the channel is closed, false otherwise.
    • wakeUp

      public void wakeUp()
    • selectRead

      protected static void selectRead(SelectionKey key) throws IOException
      Handles channel reads from a SelectionKey for the client listener SECURITY: Called on Connection Selector Thread
      Parameters:
      key -
      Throws:
      IOException
    • handleChannelRecieve

      public int handleChannelRecieve() throws IOException, BadFormatException
      Handles receipt of bytes from the channel on this Connection. Will switch the current store to the Connection-specific store if required. SECURITY: Called on NIO Thread (Server or client Connection)
      Returns:
      The number of bytes read from channel
      Throws:
      IOException - If IO error occurs
      BadFormatException - If there is an encoding error
    • flushBytes

      public boolean flushBytes() throws IOException
      Sends bytes buffered into the underlying channel.
      Returns:
      True if all bytes are sent, false otherwise
      Throws:
      IOException - If an IO Exception occurs
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getTrustedPeerKey

      public AccountKey getTrustedPeerKey()
    • setTrustedPeerKey

      public void setTrustedPeerKey(AccountKey value)
    • isTrusted

      public boolean isTrusted()