Skip to content

Commit

Permalink
Defer creation of ServiceAnnouncer until start
Browse files Browse the repository at this point in the history
This adds extra complexity from the ServiceAnnouncer not always existing
but enables later flexibility in supporting binding to an emphemeral
port instead of having to preconfigure it.
  • Loading branch information
fishface60 committed Dec 27, 2024
1 parent 39bdbfa commit 0aa48ed
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/net/rptools/maptool/server/MapToolServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.SwingUtilities;
import net.rptools.clientserver.ConnectionFactory;
Expand Down Expand Up @@ -67,6 +68,7 @@ public enum State {
Stopped
}

@Nonnull private final String serviceIdentifier;
private final Server server;
private final MessageHandler messageHandler;
private final Router router;
Expand All @@ -81,7 +83,7 @@ public enum State {
private final AssetProducerThread assetProducerThread;

private final boolean useUPnP;
private final ServiceAnnouncer announcer;
@Nullable private ServiceAnnouncer announcer;
private Campaign campaign;
private ServerPolicy policy;
private HeartbeatThread heartbeatThread;
Expand All @@ -97,16 +99,12 @@ public MapToolServer(
boolean useUPnP,
ServerPolicy policy,
ServerSidePlayerDatabase playerDb) {
this.serviceIdentifier = id;
this.config = config;
this.useUPnP = useUPnP;
this.policy = new ServerPolicy(policy);
this.playerDatabase = playerDb;

this.announcer =
config == null || id == null
? null
: new ServiceAnnouncer(id, config.getPort(), AppConstants.SERVICE_GROUP);

server = ConnectionFactory.getInstance().createServer(this.config);
messageHandler = new ServerMessageHandler(this);
this.router = new Router();
Expand Down Expand Up @@ -354,6 +352,7 @@ public void stop() {

if (announcer != null) {
announcer.stop();
announcer = null;
}

// Unregister ourselves
Expand Down Expand Up @@ -418,7 +417,8 @@ public void start() throws IOException {
}
}

if (announcer != null) {
if (serviceIdentifier != null && config != null) {
announcer = new ServiceAnnouncer(serviceIdentifier, config.getPort(), AppConstants.SERVICE_GROUP);
announcer.start();
}

Expand Down

0 comments on commit 0aa48ed

Please sign in to comment.