.
This commit is contained in:
parent
5a255aeac0
commit
e8a2a45178
7 changed files with 110 additions and 1 deletions
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
|
|
@ -12,6 +12,7 @@
|
|||
<option value="$PROJECT_DIR$/core-bukkit-api" />
|
||||
<option value="$PROJECT_DIR$/core-bukkit-impl" />
|
||||
<option value="$PROJECT_DIR$/core-lib" />
|
||||
<option value="$PROJECT_DIR$/core-velocity" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
TODO
|
||||
- timeout/error handling with reactive
|
||||
- unregister server on shutdown
|
||||
- infrastructure scripts/glue code w/ daemontools
|
||||
- djb's multilogd for logging
|
||||
|
||||
environment
|
||||
-----------
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.print.Doc;
|
||||
|
||||
public class CrownServerManager implements ServerManager {
|
||||
|
||||
private final NatsRepository natsRepo;
|
||||
|
|
@ -60,6 +62,14 @@ public class CrownServerManager implements ServerManager {
|
|||
.map(doc -> doc.getString("state"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Integer> getPort(String handle) {
|
||||
var payload = DocumentRepository.toBytes(new Document("handle", handle));
|
||||
return natsRepo.request("crown.get-port", payload)
|
||||
.map(DocumentRepository::fromMessage)
|
||||
.map(doc -> doc.getInteger("port"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Void> setState(String handle, String state) {
|
||||
Preconditions.checkArgument(!state.contains(" "), "state may not contain spaces");
|
||||
|
|
|
|||
|
|
@ -23,5 +23,7 @@ public interface ServerManager {
|
|||
|
||||
Mono<@NotNull String> getState(String handle);
|
||||
|
||||
Mono<@NotNull Integer> getPort(String handle);
|
||||
|
||||
Mono<@NotNull Void> setState(String handle, String state);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package de.kentoj.scrow.corevelocity;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.scrowlib.servermanager.CrownServerManager;
|
||||
import de.kentoj.scrowlib.servermanager.ServerManager;
|
||||
import io.nats.client.Connection;
|
||||
import io.nats.client.Nats;
|
||||
import io.nats.client.Options;
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Plugin(
|
||||
id = "corevelocity",
|
||||
name = "CoreVelocity",
|
||||
version = "0.1"
|
||||
)
|
||||
@Log
|
||||
public class CoreVelocity {
|
||||
|
||||
private final ProxyServer server;
|
||||
private final Connection nats;
|
||||
private final ServerManager serverManager;
|
||||
private final ServerRegisterWatchdog registerWatchdog;
|
||||
|
||||
@Inject
|
||||
public CoreVelocity(ProxyServer server) {
|
||||
this.server = server;
|
||||
|
||||
try {
|
||||
var natsHost = System.getenv("HOST_NATS");
|
||||
nats = Nats.connect(Options.builder()
|
||||
.pedantic()
|
||||
.server(natsHost)
|
||||
.build());
|
||||
serverManager = new CrownServerManager(nats);
|
||||
registerWatchdog = new ServerRegisterWatchdog(nats, serverManager, server);
|
||||
|
||||
log.info("listening on " + natsHost + " for server registrations");
|
||||
registerWatchdog.listen();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.kentoj.scrow.corevelocity;
|
||||
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.ServerInfo;
|
||||
import de.kentoj.scrowlib.messaging.DocumentRepository;
|
||||
import de.kentoj.scrowlib.messaging.NatsRepository;
|
||||
import de.kentoj.scrowlib.servermanager.ServerManager;
|
||||
import io.nats.client.Connection;
|
||||
import lombok.extern.java.Log;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@Log
|
||||
public class ServerRegisterWatchdog {
|
||||
|
||||
private final NatsRepository natsRepo;
|
||||
private final ServerManager serverManager;
|
||||
private final ProxyServer server;
|
||||
|
||||
public ServerRegisterWatchdog(Connection nats, ServerManager serverManager, ProxyServer server) {
|
||||
this.natsRepo = new NatsRepository(nats);
|
||||
this.serverManager = serverManager;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void listen() {
|
||||
natsRepo.subscribe("crown.set-state")
|
||||
.map(DocumentRepository::fromMessage)
|
||||
.flatMap(doc -> {
|
||||
var state = doc.getString("state");
|
||||
if (!"UP".equals(state)) return Mono.empty();
|
||||
|
||||
var handle = doc.getString("handle");
|
||||
return serverManager.getPort(handle)
|
||||
.map(port -> {
|
||||
log.info("registering " + handle);
|
||||
return server.registerServer(new ServerInfo(handle, new InetSocketAddress(port)));
|
||||
});
|
||||
})
|
||||
.subscribe();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,4 +9,5 @@ plugins {
|
|||
rootProject.name = "core"
|
||||
include("core-bukkit-api")
|
||||
include("core-bukkit-impl")
|
||||
include("core-lib")
|
||||
include("core-lib")
|
||||
include("core-velocity")
|
||||
Loading…
Add table
Add a link
Reference in a new issue