.
This commit is contained in:
parent
5a255aeac0
commit
e8a2a45178
7 changed files with 110 additions and 1 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue