Package it.polimi.ingsw.Network
Class KeepAliveSocketWrapper
java.lang.Object
it.polimi.ingsw.Network.SocketWrapper
it.polimi.ingsw.Network.KeepAliveSocketWrapper
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 Summary
Modifier and TypeFieldDescriptionprivate final HeartBeatSender
private Timer
private final BlockingQueue<Message>
private final long
-
Constructor Summary
ConstructorDescriptionKeepAliveSocketWrapper
(Socket socket, long keepAlivePeriod, boolean activeHeartBeat) Wraps the socket around a keep alive wrapper. -
Method Summary
Modifier and TypeMethodDescriptionBlocks until a message is read or the heartbeat timeout expiresMethods inherited from class it.polimi.ingsw.Network.SocketWrapper
close, getInetAddress, isClosed, sendMessage
-
Field Details
-
inputQueue
-
heartBeatSender
-
keepAlivePeriod
private final long keepAlivePeriod -
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 aHeartBeatMessage
at constant intervals, the latter means the socket sends aHeartBeatMessage
for everyHeartBeatMessage
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 aroundkeepAlivePeriod
- 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 unreachableactiveHeartBeat
- if set to true, makes the wrapper send aHeartBeatMessage
every period, if set to false makes theHeartBeatMessage
be sent only in response to aHeartBeatMessage
in input.- Throws:
IOException
- if any issues occur while wrapping the socket.
-
-
Method Details
-
awaitMessage
Blocks until a message is read or the heartbeat timeout expires- Overrides:
awaitMessage
in classSocketWrapper
- Returns:
- the read message
- Throws:
IOException
- if the timeout runs out or if an error is encountered while fetching a message
-