Skip to content

Commit 536346c

Browse files
committed
Defer creation of ServiceAnnouncer until start
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.
1 parent 24b1dda commit 536346c

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/main/java/net/rptools/maptool/server/MapToolServer.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public enum State {
8383
private final AssetProducerThread assetProducerThread;
8484

8585
private final boolean useUPnP;
86-
private final ServiceAnnouncer announcer;
86+
@Nullable private ServiceAnnouncer announcer;
8787
private Campaign campaign;
8888
private ServerPolicy policy;
8989
private HeartbeatThread heartbeatThread;
@@ -105,11 +105,6 @@ public MapToolServer(
105105
this.policy = new ServerPolicy(policy);
106106
this.playerDatabase = playerDb;
107107

108-
this.announcer =
109-
config == null || id == null
110-
? null
111-
: new ServiceAnnouncer(id, config.getPort(), AppConstants.SERVICE_GROUP);
112-
113108
server = ConnectionFactory.getInstance().createServer(this.config);
114109
messageHandler = new ServerMessageHandler(this);
115110
this.router = new Router();
@@ -370,6 +365,7 @@ public void stop() {
370365

371366
if (announcer != null) {
372367
announcer.stop();
368+
announcer = null;
373369
}
374370

375371
// Unregister ourselves
@@ -434,7 +430,8 @@ public void start() throws IOException {
434430
}
435431
}
436432

437-
if (announcer != null) {
433+
if (serviceIdentifier != null && config != null) {
434+
announcer = new ServiceAnnouncer(serviceIdentifier, config.getPort(), AppConstants.SERVICE_GROUP);
438435
announcer.start();
439436
}
440437

0 commit comments

Comments
 (0)