Package convex.net
Class Connection
java.lang.Object
convex.net.Connection
Class representing a low-level 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 Summary
Modifier and TypeMethodDescriptionvoid
close()
static Connection
connect
(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store) Create a PeerConnection by connecting to a remote addressstatic Connection
connect
(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey) Create a Connection by connecting to a remote addressstatic Connection
connect
(InetSocketAddress hostAddress, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey, int sendBufferSize, int receiveBufferSize) Create a Connection by connecting to a remote addressstatic Connection
create
(ByteChannel channel, Consumer<Message> receiveAction, AStore store, AccountKey trustedPeerKey) Create a PeerConnection using an existing channel.boolean
Sends bytes buffered into the underlying channel.static long
Gets the global message ID counterReturns the local SocketAddress associated with this connection, or null if not availablelong
Returns the remote SocketAddress associated with this connection, or null if not availablegetStore()
Gets the store associated with this Connectionint
Handles receipt of bytes from the channel on this Connection.boolean
isClosed()
Checks if this connection is closed (i.e.boolean
protected static void
selectRead
(SelectionKey key) Handles channel reads from a SelectionKey for the client listener SECURITY: Called on Connection Selector Threadlong
sendChallenge
(SignedData<ACell> challenge) Sends a CHALLENGE Request Message on this connection.boolean
Sends a DATA Message on this connection.boolean
sendMessage
(Message msg) Sends a message over this connectionboolean
sendMissingData
(Hash value) Sends a DATA Message on this connection.boolean
sendObject
(MessageType type, ACell payload) Sends a payload for the given message type.long
Sends a QUERY Message on this connection with a null Addresslong
Sends a QUERY Message on this connection.long
sendResponse
(SignedData<ACell> response) Sends a RESPONSE Request Message on this connection.boolean
sendResult
(CVMLong id, ACell value) Sends a RESULT Message on this connection with no error code (i.e.boolean
sendResult
(CVMLong id, ACell value, ACell errorCode) Sends a RESULT Message on this connection.boolean
sendResult
(Result result) Sends a RESULT Message on this connection.long
Sends a STATUS Request Message on this connection.long
sendTransaction
(SignedData<ATransaction> signed) Sends a transaction if possible, returning the message ID (greater than zero) if successful.void
setTrustedPeerKey
(AccountKey value) toString()
-
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 wrapreceiveAction
- Consumer to be called when a Message is receivedstore
- 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 toreceiveAction
- A callback Consumer to be called for any received messages on this connectionstore
- Store to use for this Connection- Returns:
- New Connection instance
- Throws:
IOException
- If connection fails because of any IO problemTimeoutException
- 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 toreceiveAction
- A callback Consumer to be called for any received messages on this connectionstore
- Store to use for this ConnectiontrustedPeerKey
- 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 problemTimeoutException
- 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 toreceiveAction
- A callback Consumer to be called for any received messages on this connectionstore
- Store to use for this ConnectiontrustedPeerKey
- Trusted peer account key if this is a trusted connection, if not then nullsendBufferSize
- Size of connection send buffer in bytesreceiveBufferSize
- Size of connection receive buffer in bytes- Returns:
- New Connection instance
- Throws:
IOException
- If connection fails because of any IO problemTimeoutException
- If the connection cannot be established within the timeout period
-
getReceivedCount
public long getReceivedCount() -
getRemoteAddress
Returns the remote SocketAddress associated with this connection, or null if not available- Returns:
- An InetSocketAddress if associated, otherwise null
-
getStore
Gets the store associated with this Connection- Returns:
- Store instance
-
getLocalAddress
Returns the local SocketAddress associated with this connection, or null if not available- Returns:
- A SocketAddress if associated, otherwise null
-
sendData
Sends a DATA Message on this connection. Does not send embedded values.- 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
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
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
Sends a QUERY Message on this connection.- Parameters:
form
- A data object representing the query formaddress
- 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
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
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
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
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
Sends a RESULT Message on this connection with no error code (i.e. a success)- Parameters:
id
- ID for result messagevalue
- Any data object- Returns:
- True if buffered for sending successfully, false otherwise
- Throws:
IOException
- If IO error occurs
-
sendResult
Sends a RESULT Message on this connection.- Parameters:
id
- ID for result messagevalue
- Any data objecterrorCode
- 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
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
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
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 messagepayload
- 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.
-
selectRead
Handles channel reads from a SelectionKey for the client listener SECURITY: Called on Connection Selector Thread- Parameters:
key
-- Throws:
IOException
-
handleChannelRecieve
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 occursBadFormatException
- If there is an encoding error
-
flushBytes
Sends bytes buffered into the underlying channel.- Returns:
- True if all bytes are sent, false otherwise
- Throws:
IOException
- If an IO Exception occurs
-
toString
-
getTrustedPeerKey
-
setTrustedPeerKey
-
isTrusted
public boolean isTrusted()
-