Skip to content

Commit

Permalink
feat(shulker-server-agent): add watchGameServer method to api
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei965 committed Jan 24, 2025
1 parent d0dce98 commit 64f0269
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public interface AgonesSDK {
void destroy();
Expand All @@ -12,6 +13,7 @@ public interface AgonesSDK {
CompletableFuture<Void> setReady();
CompletableFuture<Void> setAllocated();
CompletableFuture<Void> setReserved(long seconds);
void watchGameServer(Consumer<GameServer> consumer);
void askShutdown();

void sendHealthcheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

public final class AgonesSDKImpl implements AgonesSDK {
private static final Empty EMPTY_PAYLOAD = Empty.getDefaultInstance();
Expand Down Expand Up @@ -75,6 +76,33 @@ public CompletableFuture<Void> setReserved(long seconds) {
).thenAccept((reply) -> {});
}

@Override
public void watchGameServer(Consumer<GameServer> consumer) {
// Create a StreamObserver that forwards each GameServer to the Consumer
StreamObserver<GameServer> responseObserver = new StreamObserver<>() {
@Override
public void onNext(GameServer value) {
// Forward the GameServer to the Consumer for handling
consumer.accept(value);
}

@Override
public void onError(Throwable t) {
// Optionally handle errors
System.err.println("Error during stream: " + t.getMessage());
}

@Override
public void onCompleted() {
// Optionally handle completion of the stream
System.out.println("Stream completed.");
}
};

// Make the gRPC call to start watching the GameServer stream
this.stub.watchGameServer(EMPTY_PAYLOAD, responseObserver);
}

@Override
public void askShutdown() {
try {
Expand Down
2 changes: 2 additions & 0 deletions packages/shulker-server-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dependencies {

// Agones
commonImplementation(project(":packages:google-agones-sdk"))

commonImplementation(libs.grpc.stub)
}

setOf("processPaperResources").forEach { taskName ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.shulkermc.serveragent.api

import com.agones.dev.sdk.GameServer
import io.shulkermc.serveragent.ShulkerServerAgentCommon
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer

class ShulkerServerAPIImpl(private val agent: ShulkerServerAgentCommon) : ShulkerServerAPI() {
override fun askShutdown() = this.agent.shutdown()

override fun setReady(): CompletableFuture<Void> = this.agent.agonesGateway.setReady().thenAccept {}
override fun watchGameServer(consumer: Consumer<GameServer>) {
return this.agent.agonesGateway.watchGameServer(consumer)
}

override fun setAllocated(): CompletableFuture<Void> = this.agent.agonesGateway.setAllocated().thenAccept {}

Expand Down
3 changes: 3 additions & 0 deletions packages/shulker-server-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
dependencies {
implementation(project(":packages:google-agones-sdk"))
}
configure<JavaPluginExtension> {
withJavadocJar()
withSourcesJar()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.shulkermc.serveragent.api;

import com.agones.dev.sdk.GameServer;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public abstract class ShulkerServerAPI {
public static ShulkerServerAPI INSTANCE;

abstract public void askShutdown();
abstract public CompletableFuture<Void> setReady();
abstract public void watchGameServer(Consumer<GameServer> consumer);
abstract public CompletableFuture<Void> setAllocated();
abstract public CompletableFuture<Void> setReserved(long seconds);
}

0 comments on commit 64f0269

Please sign in to comment.