Skip to content

Commit

Permalink
Replace the SetupServer with the Rust version
Browse files Browse the repository at this point in the history
This now means that Rust is used practically within
the mod now, and all access restrictions are gone!
  • Loading branch information
Pixaurora committed Sep 7, 2024
1 parent ce6d514 commit 209429f
Show file tree
Hide file tree
Showing 14 changed files with 1,630 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.pixaurora.kitten_heart.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
Expand All @@ -20,7 +19,6 @@
import net.pixaurora.kitten_heart.impl.service.MinecraftUICompat;
import net.pixaurora.kitten_heart.impl.service.ServiceLoading;
import net.pixaurora.kitten_thoughts.impl.KittenThoughts;
import net.pixaurora.kitten_thoughts.impl.http.server.Server;

public class KitTunes {
public static final Logger LOGGER = LoggerFactory.getLogger(Constants.MOD_ID);
Expand All @@ -46,18 +44,6 @@ public static void init() {
MusicMetadataLoader.trackFiles());

KittenThoughts.init();

Server server = Server.create("token");

try {
String token = server.runServer();

KitTunes.LOGGER.info(token);
} catch (IOException e) {
KitTunes.LOGGER.info("Failed to run native code!", e);
} finally {
server.close();
}
}

public static void tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
import net.pixaurora.kitten_heart.impl.error.UnhandledKitTunesException;

public class HttpHelper {
@SuppressWarnings("resource")
public static InputStream get(String endpoint, Map<String, String> queryParameters)
throws UnhandledKitTunesException {
return UnhandledKitTunesException.runOrThrow(() -> handleRequest("GET", endpoint, queryParameters));
}

@SuppressWarnings("resource")
public static InputStream post(String endpoint, Map<String, String> queryParameters)
throws UnhandledKitTunesException {
return UnhandledKitTunesException.runOrThrow(() -> handleRequest("POST", endpoint, queryParameters));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LastFMScrobbler implements Scrobbler {
public static final String SETUP_URL = "https://last.fm/api/auth?api_key=" + API_KEY;

public static final ScrobblerType<LastFMScrobbler> TYPE = new ScrobblerType<>("last.fm", LastFMScrobbler.class,
SETUP_URL, "token=", LastFMScrobbler::setup);
SETUP_URL, LastFMScrobbler::setup);

private final LastFMSession session;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ public interface Scrobbler extends SimpleScrobbler, SpecifiesType<Scrobbler> {
public static final DispatchGroup<Scrobbler, ScrobblerType<? extends Scrobbler>> TYPES = new DispatchGroup<>(
"scrobbler", Arrays.asList(LastFMScrobbler.TYPE));

public static final int SETUP_PORT = 19686;

public String username() throws KitTunesException;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package net.pixaurora.kitten_heart.impl.scrobble;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import net.pixaurora.kitten_heart.impl.network.SetupServer;
import net.pixaurora.kitten_thoughts.impl.http.server.Server;

public class ScrobblerSetup<T extends Scrobbler> {
private final SetupServer server;
public class ScrobblerSetup<T extends Scrobbler> implements Closeable {
private final Server server;
private final CompletableFuture<T> awaitedScrobbler;

public ScrobblerSetup(SetupServer server, ScrobblerType<T> scrobblerType, long timeout, TimeUnit unit) {
public ScrobblerSetup(Server server, ScrobblerType<T> scrobblerType, long timeout, TimeUnit unit) {
this.server = server;
this.awaitedScrobbler = server.awaitedToken().whenComplete((token, error) -> server.cleanup())
this.awaitedScrobbler = CompletableFuture.supplyAsync(this::run).whenComplete((token, error) -> server.close())
.thenApply(token -> {
return scrobblerType.setupMethod().createScrobbler(token);
});
}

private String run() {
try {
return this.server.runServer();
} catch (IOException e) {
throw new RuntimeException("Couldn't finish running server!", e);
}
}

public boolean isComplete() {
return this.awaitedScrobbler.isDone();
}
Expand All @@ -27,7 +37,12 @@ public T get() throws ExecutionException, InterruptedException {
}

public void cancel() {
this.close();
this.awaitedScrobbler.cancel(false);
this.server.cleanup();
}

@Override
public void close() {
this.server.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@

import net.pixaurora.kitten_heart.impl.config.dispatch.DispatchType;
import net.pixaurora.kitten_heart.impl.error.KitTunesException;
import net.pixaurora.kitten_heart.impl.network.SetupServer;
import net.pixaurora.kitten_thoughts.impl.http.server.Server;

public class ScrobblerType<T extends Scrobbler> implements DispatchType<Scrobbler> {
private final String name;
private final Class<T> targetClass;
private final String setupURL;
private final String tokenPrefix;
private final SetupMethod<T> setupMethod;

public ScrobblerType(String name, Class<T> targetClass, String setupURL, String tokenPrefix,
SetupMethod<T> setupMethod) {
public ScrobblerType(String name, Class<T> targetClass, String setupURL, SetupMethod<T> setupMethod) {
super();
this.name = name;
this.targetClass = targetClass;
this.setupURL = setupURL;
this.tokenPrefix = tokenPrefix;
this.setupMethod = setupMethod;
}

public ScrobblerSetup<T> setup(long timeout, TimeUnit unit) throws IOException {
SetupServer server = SetupServer.create(this.tokenPrefix);
Server server = Server.create();

return new ScrobblerSetup<>(server, this, timeout, unit);
}
Expand All @@ -44,10 +41,6 @@ public String setupURL() {
return setupURL;
}

public String tokenPrefix() {
return tokenPrefix;
}

public SetupMethod<T> setupMethod() {
return setupMethod;
}
Expand Down
Loading

0 comments on commit 209429f

Please sign in to comment.