java.lang.Object
de.hamstersimulator.objectsfirst.server.input.RemoteInputInterface
All Implemented Interfaces:
InputInterface

public class RemoteInputInterface extends Object implements 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.
  • Property Details

    • message

      public ReadOnlyObjectProperty<Optional<InputMessage>> 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
  • Constructor Details

    • RemoteInputInterface

      public RemoteInputInterface()
  • Method Details

    • readInteger

      public Optional<Integer> readInteger(String message)
      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 interface InputInterface
      Parameters:
      message - The message used in the prompt for the number.
      Returns:
      The integer value read or an empty optional, if aborted.
    • readString

      public Optional<String> readString(String message)
      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 interface InputInterface
      Parameters:
      message - The message used in the prompt for the string.
      Returns:
      The string value read or an empty optional, if aborted.
    • confirmAlert

      public void confirmAlert(Throwable throwable)
      Informs a user about an abnormal execution aborting. This blocks until it returns or is aborted This is thread safe
      Specified by:
      confirmAlert in interface InputInterface
      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 interface InputInterface
    • setResult

      public void setResult(String result, int id)
      Sets the result from a remote source
      Parameters:
      result - the result of the request normally, this should not be null, however it can be null
      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
    • 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

      public InputMode getInputMode()
      Gets the current input mode
      Returns:
      the current input mode
    • messageProperty

      public ReadOnlyObjectProperty<Optional<InputMessage>> 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
      Use with care! Do not spend too much time in a critical region. Make sure to leave the critical region after the necessary part is done.
    • leaveCriticalRegion

      public void leaveCriticalRegion()
      leaves a critical region
      Throws:
      IllegalMonitorStateException - if the executing thread did not enter a critical region