Skip to content

Commit

Permalink
✨ MinecraftClientService
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Jan 17, 2020
1 parent 35fa75a commit c002c43
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/graversen/minecraft/rcon/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.time.Duration;

public final class Defaults {
public static final Duration RCON_TIMEOUT = Duration.ofSeconds(1);
public static final Duration CONNECTION_WATCHER_INTERVAL = Duration.ofSeconds(1);
public static final int DEFAULT_RCON_PORT = 25575;
public static final int RCON_PORT = 25575;

private Defaults() {

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/graversen/minecraft/rcon/MinecraftRcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.concurrent.TimeUnit;

public class MinecraftRcon implements IMinecraftRcon {
static final int DEFAULT_TIMEOUT = 5000;

private final IMinecraftClient rconClient;

public MinecraftRcon(IMinecraftClient rconClient) {
Expand All @@ -21,7 +19,7 @@ public RconResponse sendSync(ICommand command) {
final var responseFuture = sendAsync(command);

try {
return responseFuture.get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
return responseFuture.get(Defaults.RCON_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new RconCommandException(e, "Failed to receive response to command");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.graversen.minecraft.rcon.service;

import io.graversen.minecraft.rcon.IMinecraftClient;
import io.graversen.minecraft.rcon.MinecraftRcon;

import java.util.Optional;

public interface IMinecraftClientService {
public interface IMinecraftRconService {
void connect();

void disconnect();

boolean isConnected();

Optional<IMinecraftClient> minecraftClient();
Optional<MinecraftRcon> minecraftRcon();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.graversen.minecraft.rcon.service;

import io.graversen.minecraft.rcon.IMinecraftClient;
import io.graversen.minecraft.rcon.MinecraftRcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -10,20 +11,21 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MinecraftClientService implements IMinecraftClientService {
private static Logger LOG = LoggerFactory.getLogger(MinecraftClientService.class);
public class MinecraftRconService implements IMinecraftRconService {
private static Logger LOG = LoggerFactory.getLogger(MinecraftRconService.class);

private final RconDetails rconDetails;
private final ConnectOptions connectOptions;
private final ScheduledExecutorService executorService;

private IMinecraftClient minecraftClient;
private volatile IMinecraftClient minecraftClient;
private volatile MinecraftRcon minecraftRcon;

private volatile boolean shouldClose;
private volatile boolean shouldConnect;
private volatile boolean isConnected;

public MinecraftClientService(RconDetails rconDetails, ConnectOptions connectOptions) {
public MinecraftRconService(RconDetails rconDetails, ConnectOptions connectOptions) {
this.rconDetails = rconDetails;
this.connectOptions = connectOptions;
this.executorService = Executors.newScheduledThreadPool(2);
Expand All @@ -50,8 +52,8 @@ public boolean isConnected() {
}

@Override
public Optional<IMinecraftClient> minecraftClient() {
return Optional.ofNullable(minecraftClient);
public Optional<MinecraftRcon> minecraftRcon() {
return Optional.ofNullable(minecraftRcon);
}

private void safeClose(String reason) {
Expand Down Expand Up @@ -85,9 +87,10 @@ public boolean onTestConnection() {
public void onPingResult(PingResult pingResult) {
if (!pingResult.isSuccess() && shouldConnect) {
if (isConnected) {
LOG.warn("Connection broken - resetting");
isConnected = false;
minecraftClient = null;
LOG.warn("Connection broken - reconnecting");
minecraftRcon = null;
}
doConnect();
}
Expand All @@ -98,6 +101,7 @@ public void onPingResult(PingResult pingResult) {
private void doConnect() {
try {
minecraftClient = executorService.submit(new ConnectTask(connectOptions, rconDetails)).get();
minecraftRcon = new MinecraftRcon(minecraftClient);
isConnected = true;
} catch (Exception e) {
safeClose(e.getMessage());
Expand Down

0 comments on commit c002c43

Please sign in to comment.