Coverage Summary for Class: HeartBeatSender (it.polimi.ingsw.Network)

Class Class, % Method, % Line, %
HeartBeatSender 0% (0/1) 0% (0/3) 0% (0/12)


1 package it.polimi.ingsw.Network; 2  3 import it.polimi.ingsw.Server.Messages.HeartBeatMessage; 4  5 import java.io.IOException; 6 import java.util.Timer; 7 import java.util.TimerTask; 8  9 /** 10  * an auto sender of {@link HeartBeatMessage} at regular intervals 11  */ 12 public class HeartBeatSender extends TimerTask { 13  private final SocketWrapper sw; 14  private final Timer timer; 15  16  /** 17  * Creates the sender but does not activate it 18  * 19  * @param sw the socket to send heartbeats on 20  */ 21  public HeartBeatSender(SocketWrapper sw) { 22  this.sw = sw; 23  this.timer = new Timer(); 24  } 25  26  /** 27  * Activates the sender. The sender will stop running only if externally stopped or if the socketwrapper handling it 28  * has issues during delivery of a heartbeat. 29  * 30  * @param keepAlivePeriod the time, in milliseconds, between each {@link HeartBeatMessage} sent 31  */ 32  public void start(long keepAlivePeriod) { 33  this.timer.scheduleAtFixedRate(this, 0, keepAlivePeriod); 34  } 35  36  /** 37  * sends a {@link HeartBeatMessage} over the wrapper. In case of errors during the sending of a message, closes the 38  * wrapper and cancels the repeating task from the timer. 39  */ 40  public void run() { 41  try { 42  sw.sendMessage(new HeartBeatMessage()); 43  } catch (IOException e) { 44  try { 45  sw.close(); 46  } catch (IOException ex) { 47  throw new RuntimeException(ex); 48  } 49  timer.cancel(); 50  } 51  } 52 }