Class RemoteInputInterface
java.lang.Object
de.hamstersimulator.objectsfirst.server.input.RemoteInputInterface
- All Implemented Interfaces:
InputInterface
The input interface that handles remote requests.
This class is able to deal with concurrent results.
This class is inherently multithreaded: if single threaded execution is necessary (e.g. a setResult after a getInputId), use enterCriticalRegion and leaveCriticalRegion.
This class is inherently multithreaded: if single threaded execution is necessary (e.g. a setResult after a getInputId), use enterCriticalRegion and leaveCriticalRegion.
-
Property Summary
TypePropertyDescriptionGets the message the UI should display
If the input mode is READ_STRING or READ_INT, it is just a simple string. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Aborts readInteger, readString or showAlert.void
confirmAlert(Throwable throwable)
Informs a user about an abnormal execution aborting.void
Enters a critical region and waits if necessary
In a critical region, the following things are guaranteed: the input mode does not change the message does not change the input id does not change Use with care! Do not spend too much time in a critical region.int
Gets the id of the current input request This id must be provided in setResult to ensure that a previous request does not set the current resultGets the current input modevoid
leaves a critical regionGets the message the UI should display
If the input mode is READ_STRING or READ_INT, it is just a simple string.readInteger(String message)
Read an integer value from a user.readString(String message)
Read a string value from a user.void
Sets the result from a remote sourcevoid
setResultNoInput(int id)
Completes the input with no input
-
Property Details
-
message
Gets the message the UI should display
If the input mode is READ_STRING or READ_INT, it is just a simple string. If the input mode is SHOW_ALERT, it is a json with the fields type, message and stacktrace.- Returns:
- the current message or an empty optional if there is none because the input mode is none
-
-
Constructor Details
-
RemoteInputInterface
public RemoteInputInterface()
-
-
Method Details
-
readInteger
Read an integer value from a user. This blocks until there is an integer to return or it is aborted. This is thread safe- Specified by:
readInteger
in interfaceInputInterface
- Parameters:
message
- The message used in the prompt for the number.- Returns:
- The integer value read or an empty optional, if aborted.
-
readString
Read a string value from a user. This blocks until there is a String to return or it is aborted This is thread safe- Specified by:
readString
in interfaceInputInterface
- Parameters:
message
- The message used in the prompt for the string.- Returns:
- The string value read or an empty optional, if aborted.
-
confirmAlert
Informs a user about an abnormal execution aborting. This blocks until it returns or is aborted This is thread safe- Specified by:
confirmAlert
in interfaceInputInterface
- Parameters:
throwable
- The throwable which lead to aborting the program.
-
abort
public void abort()Aborts readInteger, readString or showAlert. This is thread-safe. May be called multiple times, even if no input is pending- Specified by:
abort
in interfaceInputInterface
-
setResult
Sets the result from a remote source- Parameters:
result
- the result of the request normally, this should not be null, however it can be nullid
- the id of the request (if the id does not match, this has no effect)- Throws:
IllegalArgumentException
- if the result comes from an outdated request
-
setResultNoInput
public void setResultNoInput(int id)Completes the input with no input- Parameters:
id
- the id of the request (if the id does not match, this has no effect)- Throws:
IllegalArgumentException
- if the result comes from an outdated request
-
getInputID
public int getInputID()Gets the id of the current input request This id must be provided in setResult to ensure that a previous request does not set the current result- Returns:
- the id
-
getInputMode
Gets the current input mode- Returns:
- the current input mode
-
messageProperty
Gets the message the UI should display
If the input mode is READ_STRING or READ_INT, it is just a simple string. If the input mode is SHOW_ALERT, it is a json with the fields type, message and stacktrace.- Returns:
- the current message or an empty optional if there is none because the input mode is none
-
enterCriticalRegion
public void enterCriticalRegion()Enters a critical region and waits if necessary
In a critical region, the following things are guaranteed:- the input mode does not change
- the message does not change
- the input id does not change
-
leaveCriticalRegion
public void leaveCriticalRegion()leaves a critical region- Throws:
IllegalMonitorStateException
- if the executing thread did not enter a critical region
-