Class SpallocConnection
java.lang.Object
uk.ac.manchester.spinnaker.spalloc.SpallocConnection
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
SpallocClient
public abstract class SpallocConnection extends Object implements Closeable
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
Fields Modifier and Type Field Description protected Queue<Notification>
notifications
A queue of unprocessed notifications. -
Constructor Summary
Constructors Modifier Constructor Description protected
SpallocConnection(String hostname, int port, Integer timeout)
Define a new connection. -
Method Summary
Modifier and Type Method Description protected String
call(Command<?> command, Integer timeout)
Send a command to the server and return the reply.void
close()
Disconnect from the server.void
connect()
(Re)connect to the server.void
connect(Integer timeout)
(Re)connect to the server.protected abstract String
formatRequest(Command<?> command)
Format a request to be ready to go to the server.protected abstract Response
parseResponse(String line)
Parse a response line from the server.protected Response
receiveResponse(Integer timeout)
Receive a line from the server with a response.protected void
sendCommand(Command<?> command, Integer timeout)
Attempt to send a command as a line to the server.String
toString()
AutoCloseable
withConnection()
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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if anything goes wrong
-
receiveResponse
protected Response receiveResponse(Integer timeout) throws SpallocProtocolTimeoutException, IOException, InterruptedExceptionReceive a line from the server with a response.- Parameters:
timeout
- The number of milliseconds to wait before timing out ornull
if 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, IOExceptionAttempt 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 ornull
if 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, InterruptedExceptionSend 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 ornull
if 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
-