This commit is contained in:
kento2 2026-06-03 12:13:18 +02:00
parent feafe2af62
commit 72b4edf48b
8 changed files with 83 additions and 29 deletions

View file

@ -8,7 +8,7 @@ plugins {
}
group = "de.kentoj.scrow"
version = "0.3"
version = "0.4"
repositories {
mavenCentral()

View file

@ -4,14 +4,17 @@ import io.nats.client.Message;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bson.Document;
import org.jetbrains.annotations.ApiStatus;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DocumentRepository {
@ApiStatus.Obsolete
public static Document fromMessage(Message message) {
return Document.parse(new String(message.getData()));
}
@ApiStatus.Obsolete
public static byte[] toBytes(Document document) {
return document.toJson().getBytes();
}

View file

@ -32,11 +32,11 @@ public class NatsRepository {
return Mono.fromRunnable(() -> nats.publish(subject, data));
}
public Flux<@NotNull Message> subscribe(String subject) {
public Flux<@NotNull SubscriptionContext> subscribe(String subject) {
return Flux.create(sink -> {
var handler = dispatcher.subscribe(subject, msg -> {
try {
sink.next(msg);
sink.next(new SubscriptionContext(msg));
} catch (Throwable e) {
sink.error(e);
}
@ -46,11 +46,11 @@ public class NatsRepository {
});
}
public Mono<@NotNull Message> request(String subject, Document document) {
public Mono<@NotNull Message> requestRaw(String subject, Document document) {
return Mono.fromFuture(nats.request(subject, DocumentRepository.toBytes(document)));
}
public Mono<@NotNull Message> request(String subject, byte[] data) {
public Mono<@NotNull Message> requestRaw(String subject, byte[] data) {
return Mono.fromFuture(nats.request(subject, data));
}
}

View file

@ -0,0 +1,18 @@
package de.kentoj.scrowlib.messaging;
import de.kentoj.scrowlib.messaging.respose.SafeResult;
import io.nats.client.Message;
import lombok.AllArgsConstructor;
import lombok.Value;
import org.bson.Document;
@Value
@AllArgsConstructor
public class SubscriptionContext {
Message msg;
Document document;
public SubscriptionContext(Message msg) {
this(msg, SafeResult.fromMessage(msg).unwrap());
}
}

View file

@ -1,7 +1,6 @@
package de.kentoj.scrowlib.servermanager;
import com.google.common.base.Preconditions;
import de.kentoj.scrowlib.messaging.DocumentRepository;
import de.kentoj.scrowlib.messaging.NatsRepository;
import de.kentoj.scrowlib.messaging.respose.SafeResult;
import io.nats.client.Connection;
@ -22,9 +21,8 @@ public class ServerManagerImpl implements ServerManager {
@Override
public Mono<@NotNull ServerInstance> deployServer(String template) {
var payload = DocumentRepository.toBytes(new Document("template", template));
return natsRepo.request("crown.deploy-server", payload)
.map(DocumentRepository::fromMessage)
return natsRepo.requestRaw("crown.deploy-server", new Document("template", template))
.map(SafeResult::unwrap)
.map(doc -> new ServerInstance(
template,
doc.getString("handle"),
@ -34,9 +32,8 @@ public class ServerManagerImpl implements ServerManager {
@Override
public Flux<@NotNull ServerInstance> getRunningServers() {
var payload = DocumentRepository.toBytes(new Document());
return natsRepo.request("crown.list-servers", payload)
.map(DocumentRepository::fromMessage)
return natsRepo.requestRaw("crown.list-servers", new Document())
.map(SafeResult::unwrap)
.map(doc ->
doc.getList("instances", Document.class)
)
@ -51,16 +48,15 @@ public class ServerManagerImpl implements ServerManager {
@Override
public Mono<@NotNull Void> destroyServer(String handle) {
var payload = DocumentRepository.toBytes(new Document("handle", handle));
return natsRepo.request("crown.destroy-server", payload)
return natsRepo.requestRaw("crown.destroy-server", new Document("handle", handle))
.map(SafeResult::unwrap)
.then();
}
@Override
public Mono<@NotNull ServerInstance> getInfo(String handle) {
var payload = DocumentRepository.toBytes(new Document("handle", handle));
return natsRepo.request("crown.get-info", payload)
.map(DocumentRepository::fromMessage)
return natsRepo.requestRaw("crown.get-info", new Document("handle", handle))
.map(SafeResult::unwrap)
.map(doc -> new ServerInstance(
doc.getString("templateName"),
handle,
@ -72,19 +68,20 @@ public class ServerManagerImpl implements ServerManager {
@Override
public Mono<@NotNull Void> setState(String handle, String state) {
Preconditions.checkArgument(!state.contains(" "), "state may not contain spaces");
var payload = DocumentRepository.toBytes(new Document()
var payload = new Document()
.append("handle", handle)
.append("state", state));
return natsRepo.request("crown.set-state", payload)
.append("state", state);
return natsRepo.requestRaw("crown.set-state", payload)
.map(SafeResult::unwrap)
.then();
}
@Override
public Mono<@NotNull Void> sendPlayer(UUID playerId, String serverHandle) {
var payload = DocumentRepository.toBytes(new Document()
var payload = new Document()
.append("playerId", playerId.toString())
.append("handle", serverHandle));
return natsRepo.request("proxy.send-player", payload)
.append("handle", serverHandle);
return natsRepo.requestRaw("proxy.send-player", payload)
.map(SafeResult::unwrap)
.then();
}