Class KeepAliveSocketWrapper

java.lang.Object
it.polimi.ingsw.Network.SocketWrapper
it.polimi.ingsw.Network.KeepAliveSocketWrapper

public class KeepAliveSocketWrapper extends SocketWrapper
A SocketWrapper that also takes care of sending or responding to HeartBeatMessage, keeping the connection active so long as the endpoint proves to be active
  • Field Details

    • inputQueue

      private final BlockingQueue<Message> inputQueue
    • heartBeatSender

      private final HeartBeatSender heartBeatSender
    • keepAlivePeriod

      private final long keepAlivePeriod
    • heartBeatTimer

      private Timer heartBeatTimer
  • Constructor Details

    • KeepAliveSocketWrapper

      public KeepAliveSocketWrapper(Socket socket, long keepAlivePeriod, boolean activeHeartBeat) throws IOException
      Wraps the socket around a keep alive wrapper. The socket can be active or passive, the former means the socket actively sends a HeartBeatMessage at constant intervals, the latter means the socket sends a HeartBeatMessage for every HeartBeatMessage received.
      The sockets will automatically close after a timeout, the timeout is 3 times the fixed duration used by an active wrapper to send its heartbeats.
      Note: server used wrappers are encouraged to use the passive wrapper while clients are encouraged to use the active version instead.
      Note: Assuming both ends of the connection use this wrapper keeping at least one active wrapper either as the client or server will keep the connection alive until an error occurs without the need to manually send the heartbeat. The same can be said if both ends of the connection are active wrappers. The same cannot be said if both ends are passive.
      Parameters:
      socket - the socket to wrap around
      keepAlivePeriod - the time (in milliseconds) it takes for an active wrapper to send a heartbeat and a third of the timeout required to close the connection and deem the endpoint unreachable
      activeHeartBeat - if set to true, makes the wrapper send a HeartBeatMessage every period, if set to false makes the HeartBeatMessage be sent only in response to a HeartBeatMessage in input.
      Throws:
      IOException - if any issues occur while wrapping the socket.
  • Method Details

    • awaitMessage

      public Message awaitMessage() throws IOException
      Blocks until a message is read or the heartbeat timeout expires
      Overrides:
      awaitMessage in class SocketWrapper
      Returns:
      the read message
      Throws:
      IOException - if the timeout runs out or if an error is encountered while fetching a message