Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shulker-server-agent): add watchGameServer method to api #833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}