Coverage Summary for Class: HeartBeatTimeoutTask (it.polimi.ingsw.Network)
Class |
Class, %
|
Method, %
|
Line, %
|
HeartBeatTimeoutTask |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/9)
|
1 package it.polimi.ingsw.Network;
2
3 import java.io.IOException;
4 import java.util.Timer;
5 import java.util.TimerTask;
6
7 /**
8 * Timeout timer task waiting to close the socket in case it is not cancelled in time.
9 */
10 public class HeartBeatTimeoutTask extends TimerTask {
11 private final SocketWrapper sock;
12
13 /**
14 * Private constructor setting up the attributes for the timeout task
15 *
16 * @param sock the socket to close in case of timeout
17 */
18 private HeartBeatTimeoutTask(SocketWrapper sock) {
19 this.sock = sock;
20 }
21
22 /**
23 * Sets up a single, non repeating timer counting down to the timeout.
24 *
25 * @param socketWrapper the socket to close if the timer runs out
26 * @param timeoutDuration the timeout duration in milliseconds
27 * @return the timer that is counting down the timeout, calling {@link Timer#cancel()} on it will prevent the
28 * timeout task from running, effectively resetting the timeout.
29 */
30 public static Timer startAndGetTimer(SocketWrapper socketWrapper, long timeoutDuration) {
31 Timer timeoutTimer = new Timer();
32 timeoutTimer.schedule(new HeartBeatTimeoutTask(socketWrapper), timeoutDuration);
33 return timeoutTimer;
34 }
35
36 public void run() {
37 try {
38 sock.close();
39 } catch (IOException e) {
40 throw new RuntimeException(e);
41 }
42 }
43 }