Skip to content

Commit

Permalink
Merge pull request #5263 from kwvanderlinde/refactor/5262-better-mana…
Browse files Browse the repository at this point in the history
…ge-ServerHeartBeatThread

Move heart beat task into MapToolClient
  • Loading branch information
cwisniew authored Feb 15, 2025
2 parents 5c84544 + 0ec7cc1 commit d9f4146
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
26 changes: 0 additions & 26 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,6 @@ private static void initialize() {
chatAutoSave = new ChatAutoSave();
chatAutoSave.setTimeout(AppPreferences.chatAutoSaveTimeInMinutes.get());
AppPreferences.chatAutoSaveTimeInMinutes.onChange(chatAutoSave::setTimeout);

new ServerHeartBeatThread().start();
}

public static NoteFrame getProfilingNoteFrame() {
Expand Down Expand Up @@ -1393,30 +1391,6 @@ public static String getClientId() {
return clientId;
}

private static class ServerHeartBeatThread extends Thread {

public ServerHeartBeatThread() {
super("MapTool.ServerHeartBeatThread");
}

@Override
public void run() {

// This should always run, so we should be able to safely
// loop forever
while (true) {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
log.warn("Heartbeat thread interrupted", e);
}

ServerCommand command = client.getServerCommand();
command.heartbeat(getPlayer().getName());
}
}
}

/**
* Search for command line arguments for options. Expecting arguments specified as
* -parameter=value pair and returns a string.
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/rptools/maptool/client/MapToolClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import net.rptools.clientserver.simple.connection.Connection;
import net.rptools.maptool.client.events.PlayerConnected;
Expand Down Expand Up @@ -50,6 +54,9 @@
*/
public class MapToolClient {
private static final Logger log = LogManager.getLogger(MapToolClient.class);
private static final ScheduledExecutorService periodTaskExecutor =
Executors.newSingleThreadScheduledExecutor();
private static final long HEARTBEAT_SECONDS = 20;

public enum State {
New,
Expand All @@ -71,6 +78,8 @@ public enum State {
private final ServerCommandClientImpl serverCommand;
private State currentState = State.New;

private @Nullable ScheduledFuture<?> heartBeatHandle;

private MapToolClient(
@Nullable MapToolServer localServer,
Campaign campaign,
Expand Down Expand Up @@ -187,11 +196,20 @@ public void start() throws IOException {
serverCommand.stop();
throw e;
}

var task = new HeartBeatTask(serverCommand, conn.getId());
heartBeatHandle =
periodTaskExecutor.scheduleAtFixedRate(
task, HEARTBEAT_SECONDS, HEARTBEAT_SECONDS, TimeUnit.SECONDS);
}
}

public void close() {
if (transitionToState(State.Closed)) {
if (heartBeatHandle != null) {
heartBeatHandle.cancel(true);
}

serverCommand.stop();
if (conn.isAlive()) {
conn.close();
Expand Down Expand Up @@ -317,4 +335,12 @@ private void onDisconnect(Connection connection) {
});
}
}

private record HeartBeatTask(ServerCommand serverCommand, String name) implements Runnable {
@Override
public void run() {
log.debug("HeartBeatTask(…, {})#run()", name);
serverCommand.heartbeat(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public void addDisconnectHandler(DisconnectHandler serverDisconnectHandler) {
connection.addDisconnectHandler(serverDisconnectHandler);
}

public String getId() {
return connection.getId();
}

public boolean isAlive() {
return connection.isAlive();
}
Expand Down

0 comments on commit d9f4146

Please sign in to comment.