Class ProtobufTcpConnection

  • All Implemented Interfaces:
    ProtobufConnection

    public class ProtobufTcpConnection
    extends java.lang.Object
    implements ProtobufConnection
    The ProtobufClient is a client to communicate with a refbox via protobuf messages. You can send stream messages (TCP) by calling the enqueue method. To receive messages, register a ProtobufMessageHandler, incoming messages will be passed to your handler. To send and receive broadcast messages (UDP), use the ProtobufBroadcastPeer.
    • Constructor Summary

      Constructors 
      Constructor Description
      ProtobufTcpConnection​(java.lang.String hostname, int port)
      Instantiates a new ProtobufClient.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends com.google.protobuf.GeneratedMessageV3>
      void
      add_message​(java.lang.Class<T> c)
      Adds and registers a new protobuf message type.
      void connect()
      Tries to connect to the refbox at the IP address and port given in the contructor.
      void disconnect​(boolean wait)
      Disconnects from the refbox.
      void enqueue​(com.google.protobuf.GeneratedMessageV3 msg)  
      <T> void enqueue​(com.google.protobuf.GeneratedMessageV3 msg, java.lang.Class<T> c)  
      void enqueue​(ProtobufMessage msg)
      Puts a ProtobufMessage into the send queue in order to be sent out to the refbox.
      boolean is_connected()
      Checks if a connection to the refbox is established.
      void register_handler​(ProtobufMessageHandler handler)
      Registers a new ProtobufMessageHandler responsible for handling and deserializing incoming protobuf messages.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProtobufTcpConnection

        public ProtobufTcpConnection​(java.lang.String hostname,
                                     int port)
        Instantiates a new ProtobufClient. This method does not connect (see connect).
        Parameters:
        hostname - the IP address of the refbox
        port - the port to which to connect
        See Also:
        connect()
    • Method Detail

      • connect

        public void connect()
                     throws java.lang.InterruptedException,
                            java.io.IOException,
                            java.nio.channels.UnresolvedAddressException
        Tries to connect to the refbox at the IP address and port given in the contructor. Throws an UnknownHostException if the hostname defined in the contructor is not known. Throws an IOException if the connection cannot be established.
        Throws:
        java.io.IOException - Signals that the connection to the refbox cannot be established.
        java.lang.InterruptedException
        java.nio.channels.UnresolvedAddressException
      • disconnect

        public void disconnect​(boolean wait)
        Disconnects from the refbox.
      • is_connected

        public boolean is_connected()
        Checks if a connection to the refbox is established.
        Returns:
        true, if connection is established.
      • register_handler

        public void register_handler​(ProtobufMessageHandler handler)
        Registers a new ProtobufMessageHandler responsible for handling and deserializing incoming protobuf messages. Required if you want to access received messages. Only allows one registered handler at the same time.
        Parameters:
        handler - the ProtobufMessageHandler
        See Also:
        ProtobufMessageHandler
      • add_message

        public <T extends com.google.protobuf.GeneratedMessageV3> void add_message​(java.lang.Class<T> c)
        Adds and registers a new protobuf message type. This is required to instantiate the correct protobuf message object when a message is received from the refbox. For example, if you want the client to be able to receive and process a GameState message, call client.<GameState>add_message(GameState.class).
        Type Parameters:
        T - the type of the protobuf message to register, has to extend from GeneratedMessage
        Parameters:
        c - the class object of the same protobuf message
      • enqueue

        public void enqueue​(com.google.protobuf.GeneratedMessageV3 msg)
        Specified by:
        enqueue in interface ProtobufConnection
      • enqueue

        public <T> void enqueue​(com.google.protobuf.GeneratedMessageV3 msg,
                                java.lang.Class<T> c)