Class Communicator

java.lang.Object
ch.autumo.beetroot.server.communication.Communicator
Direct Known Subclasses:
ClientCommunicator

public class Communicator extends Object
Client/Server communication.
  • Field Details

    • LOG

      protected static final org.slf4j.Logger LOG
    • MAX_MSG_SIZE

      public static final int MAX_MSG_SIZE
      Max. message size: 512 kBytes
      See Also:
    • TIMEOUT

      public static final int TIMEOUT
      Connection timeout in seconds
      See Also:
    • CMD_STOP

      public static final String CMD_STOP
      Stop command
      See Also:
    • CMD_HEALTH

      public static final String CMD_HEALTH
      Health command
      See Also:
    • CMD_FILE_DELETE

      public static final String CMD_FILE_DELETE
      File delete
      See Also:
    • CMD_FILE_REQUEST

      public static final String CMD_FILE_REQUEST
      File request for download
      See Also:
    • CMD_FILE_RECEIVE_REQUEST

      public static final String CMD_FILE_RECEIVE_REQUEST
      File receive request for upload
      See Also:
    • USER_AGENT

      public static final String USER_AGENT
      User agents used for tunneled server commands.
      See Also:
    • HTTP_HEADER_ACCEPT_JSON

      public static final String[] HTTP_HEADER_ACCEPT_JSON
      HTTP Header accept with 'application/json'.
    • HTTP_HEADER_CONTENTTYPE_JSON_UTF8

      public static final String[] HTTP_HEADER_CONTENTTYPE_JSON_UTF8
      HTTP Header accept with 'application/json; charset=utf-8'.
  • Constructor Details

    • Communicator

      public Communicator()
  • Method Details

    • writeAnswer

      public static void writeAnswer(ClientAnswer answer, DataOutputStream out) throws IOException
      Write/send client answer from server to client.
      Parameters:
      answer - client answer
      out - output stream
      Throws:
      IOException - IO exception
    • readCommand

      public static ServerCommand readCommand(DataInputStream in) throws IOException
      Read a server command server side.
      Parameters:
      in - input stream
      Returns:
      server command or null, if command received was invalid
      Throws:
      IOException - IO exception
    • readJsonCommand

      public static ServerCommand readJsonCommand(InputStream in, int length) throws IOException
      Read a JSON server command server side.
      Parameters:
      in - (body) input stream from HTTP/HTTPS request
      length - length of content
      Returns:
      server command or null, if command received was invalid
      Throws:
      IOException - IO exception
    • read

      protected static String read(DataInputStream in) throws IOException
      Server- or client-side read.
      Parameters:
      in - input stream
      Returns:
      unparsed data
      Throws:
      IOException - IO exception
    • safeClose

      public static final void safeClose(Object closeable)
      Safe close for closeable object (e.g. stream, socket).
      Parameters:
      closeable - closeable object
    • checkHttpResponse

      public static void checkHttpResponse(org.apache.http.client.methods.CloseableHttpResponse resp) throws Exception
      Check closeable HTTP response with HttpResponse.
      Parameters:
      resp - HTTP response
      Throws:
      Exception - exception
    • isInternalCommand

      protected static boolean isInternalCommand(ServerCommand command)
      Is it an internal command?
      Parameters:
      command - server command
      Returns:
      true if so