Skip to content

Commit

Permalink
feat(shulker-sdk): add convenient Java wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Oct 31, 2023
1 parent 13d142e commit 4ac0c8c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/google-agones-sdk/bindings/java/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
javaOnly=true
isLibrary=true
1 change: 1 addition & 0 deletions packages/shulker-proxy-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
isLibrary=true
2 changes: 2 additions & 0 deletions packages/shulker-sdk/bindings/java/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
javaOnly=true
isLibrary=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.shulkermc.sdk;

import java.util.concurrent.CompletableFuture;

public interface ShulkerSDK {
CompletableFuture<String> summonFromFleet(String namespace, String fleetName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.shulkermc.sdk;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.shulkermc.sdk.v1alpha1.MinecraftServerFleetServiceGrpc;
import io.shulkermc.sdk.v1alpha1.SummonFromFleetReply;
import io.shulkermc.sdk.v1alpha1.SummonFromFleetRequest;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class ShulkerSDKImpl implements ShulkerSDK {
private final ExecutorService executor = Executors.newCachedThreadPool();
private final MinecraftServerFleetServiceGrpc.MinecraftServerFleetServiceFutureStub minecraftServerFleetServiceFutureStub;

private ShulkerSDKImpl(ManagedChannel channel) {
this.minecraftServerFleetServiceFutureStub = MinecraftServerFleetServiceGrpc.newFutureStub(channel);
}

@Override
public CompletableFuture<String> summonFromFleet(String namespace, String fleetName) {
return this.wrapGrpcFuture(this.minecraftServerFleetServiceFutureStub.summonFromFleet(SummonFromFleetRequest.newBuilder()
.setNamespace(namespace)
.setName(fleetName)
.build()
)).thenApply(SummonFromFleetReply::getGameServerId);
}

private <T> CompletableFuture<T> wrapGrpcFuture(ListenableFuture<T> listenableFuture) {
CompletableFuture<T> future = new CompletableFuture<>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = listenableFuture.cancel(mayInterruptIfRunning);
super.cancel(cancelled);
return cancelled;
}
};

Futures.addCallback(
listenableFuture,
new FutureCallback<T>() {
@Override
public void onSuccess(T result) {
future.complete(result);
}

@Override
public void onFailure(Throwable t) {
future.completeExceptionally(t);
}
},
this.executor
);

return future;
}

public static ShulkerSDK createFromEnvironment() {
String hostString = System.getenv("SHULKER_SDK_GRPC_HOST");
if (hostString == null) {
throw new IllegalStateException("Missing SHULKER_SDK_GRPC_HOST environment variable");
}

String portString = System.getenv("SHULKER_SDK_GRPC_PORT");
if (portString == null) {
throw new IllegalStateException("Missing SHULKER_SDK_GRPC_PORT environment variable");
}

ManagedChannel channel = ManagedChannelBuilder.forAddress(hostString, Integer.parseInt(portString)).usePlaintext().build();
return new ShulkerSDKImpl(channel);
}
}
1 change: 1 addition & 0 deletions packages/shulker-server-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
isLibrary=true

0 comments on commit 4ac0c8c

Please sign in to comment.