Class SpallocConnection
java.lang.Object
uk.ac.manchester.spinnaker.spalloc.SpallocConnection
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
SpallocClient
The basic communications layer of the spalloc client. This client assumes
that the protocol is line-oriented, but does not assume anything about the
formatting of the contents of the lines.
- Author:
- Donal Fellows
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Queue<Notification>A queue of unprocessed notifications. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSpallocConnection(String hostname, int port, Integer timeout) Define a new connection. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringSend a command to the server and return the reply.voidclose()Disconnect from the server.voidconnect()(Re)connect to the server.void(Re)connect to the server.protected abstract StringformatRequest(Command<?> command) Format a request to be ready to go to the server.protected abstract ResponseparseResponse(String line) Parse a response line from the server.protected ResponsereceiveResponse(Integer timeout) Receive a line from the server with a response.protected voidsendCommand(Command<?> command, Integer timeout) Attempt to send a command as a line to the server.toString()Context adapter.
-
Field Details
-
notifications
A queue of unprocessed notifications.
-
-
Constructor Details
-
SpallocConnection
Define a new connection. NB: Does not connect to the server untilconnect()is called.- Parameters:
hostname- The hostname of the server.port- The port to use.timeout- The default timeout.
-
-
Method Details
-
withConnection
Context adapter. Allows this code to be used like this:try (var c = client.withConnection()) { ... }- Returns:
- the auto-closeable context.
- Throws:
IOException- If the connect to the spalloc server fails.
-
connect
(Re)connect to the server.- Throws:
IOException- If a connection failure occurs.
-
connect
(Re)connect to the server.- Parameters:
timeout- How long to spend (re)connecting.- Throws:
IOException- If a connection failure occurs.
-
close
Disconnect from the server.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if anything goes wrong
-
receiveResponse
protected Response receiveResponse(Integer timeout) throws SpallocProtocolTimeoutException, IOException, InterruptedException Receive a line from the server with a response.- Parameters:
timeout- The number of milliseconds to wait before timing out ornullif this function should try again forever.- Returns:
- The unpacked response from the line received.
- Throws:
SpallocProtocolTimeoutException- If a timeout occurs.SpallocProtocolException- If the socket gets an empty response.IOException- If the socket is unusable or becomes disconnected.InterruptedException- If interrupted, eventually
-
sendCommand
protected void sendCommand(Command<?> command, Integer timeout) throws SpallocProtocolTimeoutException, IOException Attempt to send a command as a line to the server.- Parameters:
command- The command to serialise.timeout- The number of milliseconds to wait before timing out ornullif this function should try again forever.- Throws:
SpallocProtocolTimeoutException- If a timeout occurs.IOException- If the socket is unusable or becomes disconnected.
-
formatRequest
Format a request to be ready to go to the server.- Parameters:
command- The request to format for sending. Notnull.- Returns:
- The text to send to the server. Will have a newline added.
- Throws:
IOException- If formatting goes wrong.
-
parseResponse
Parse a response line from the server.- Parameters:
line- The line to parse. Notnull. Has the terminating newline removed.- Returns:
- The parsed response.
- Throws:
IOException- If parsing completely fails.SpallocProtocolException- If an unexpected valid JSON message is returned (e.g.,null).
-
call
protected String call(Command<?> command, Integer timeout) throws SpallocServerException, SpallocProtocolTimeoutException, SpallocProtocolException, InterruptedException Send a command to the server and return the reply.- Parameters:
command- The command to send.timeout- The number of milliseconds to wait before timing out ornullif this function should wait forever.- Returns:
- The result string returned by the server.
- Throws:
SpallocServerException- If the server sends an error.SpallocProtocolTimeoutException- If a timeout occurs.SpallocProtocolException- If the connection is unavailable or is closed.InterruptedException- If interrupted, eventually
-
toString
-