Skip to content

Commit

Permalink
Add annotization based config declaring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Martin committed Sep 5, 2024
1 parent 3d216b9 commit 101f19c
Show file tree
Hide file tree
Showing 31 changed files with 492 additions and 141 deletions.
6 changes: 4 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ dependencies {
shadow "net.kyori:adventure-text-minimessage:${project.minimessage_version}"
shadow "com.google.code.gson:gson:${project.gson_version}"

implementation "io.javalin:javalin:6.2.0"
implementation "com.fasterxml.jackson.core:jackson-databind:2.17.0"
implementation 'org.spongepowered:configurate-yaml:4.0.0'

implementation "io.javalin:javalin:6.2.0" // REST API
implementation "com.fasterxml.jackson.core:jackson-databind:2.17.0" // API HTTP response code exceptions

implementation "org.java-websocket:Java-WebSocket:${project.websocket_version}"

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/group/aelysium/rustyconnector/RC.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ static Optional<Server> Server(UUID uuid) throws NoSuchElementException {
static Optional<Player> Player(UUID uuid) throws NoSuchElementException {
return RustyConnector.Toolkit.Proxy().orElseThrow().orElseThrow().Players().orElseThrow().fetch(uuid);
}
static Optional<Player> Player(String username) throws NoSuchElementException {
return RustyConnector.Toolkit.Proxy().orElseThrow().orElseThrow().Players().orElseThrow().fetch(username);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class RustyConnector {
public static class Toolkit {
private static Particle.Flux<ServerFlame> serverKernel = null;
private static Particle.Flux<ProxyFlame> velocityKernel = null;
private static Particle.Flux<ProxyFlame> proxyKernel = null;

/**
* Fetches the Server API for RustyConnector.
Expand All @@ -25,19 +25,30 @@ public static Optional<Particle.Flux<ServerFlame>> Server() throws IllegalAccess
* @return {@link ProxyFlame}
*/
public static Optional<Particle.Flux<ProxyFlame>> Proxy() throws IllegalAccessError {
return Optional.ofNullable(velocityKernel);
return Optional.ofNullable(proxyKernel);
}

public static void registerServerKernel(@NotNull Particle.Flux<ServerFlame> kernel) {
public static void registerServerKernel(@NotNull Particle.Flux<ServerFlame> kernel) throws IllegalAccessError {
if(serverKernel != null) throw new IllegalAccessError("The server kernel has already been established.");
if(proxyKernel != null) throw new IllegalAccessError("A proxy kernel has already been established.");
serverKernel = kernel;
}
public static void registerProxyKernel(@NotNull Particle.Flux<ProxyFlame> kernel) {
velocityKernel = kernel;

public static void registerProxyKernel(@NotNull Particle.Flux<ProxyFlame> kernel) throws IllegalAccessError {
if(proxyKernel != null) throw new IllegalAccessError("The proxy kernel has already been established.");
if(serverKernel != null) throw new IllegalAccessError("A server kernel has already been established.");
proxyKernel = kernel;
}

public static void unregister() {
serverKernel = null;
velocityKernel = null;
public static void unregister() throws Exception {
if(serverKernel != null) {
serverKernel.close();
serverKernel = null;
}
if(proxyKernel != null) {
proxyKernel.close();
proxyKernel = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package group.aelysium.rustyconnector.common;

/**
* Enforces a closure on the inheriting entity.
*/
public interface Closure {
/**
* Closes the inheriting entity releasing all of its resources.
* @throws Exception If there's an issue during the closure.
*/
void close() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package group.aelysium.rustyconnector.common.absolute_redundancy;

import group.aelysium.rustyconnector.common.Closure;
import group.aelysium.rustyconnector.proxy.util.LiquidTimestamp;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -15,7 +16,7 @@
* Particles are the backbone of the Absolute Redundancy Architecture.
* By leveraging {@link Flux}, Particles are able to exist in a state of super-positioning.
*/
public interface Particle extends AutoCloseable {
public interface Particle extends Closure {
/**
* Tinder is the configuration point for a {@link Particle}.
* Via a {@link Flux}, a single Tinder be deterministic of the {@link Particle} produced.
Expand Down Expand Up @@ -43,7 +44,7 @@ public final Flux<P> flux() {
* {@link Particle.Flux} exists to manage this state.
* @param <P> The underlying Particle that exists within this flux.
*/
class Flux<P extends Particle> implements AutoCloseable {
class Flux<P extends Particle> implements Closure {
private static final ExecutorService executor = Executors.newCachedThreadPool();
private @Nullable CompletableFuture<P> resolvable = null;
private @NotNull Tinder<P> tinder;
Expand Down Expand Up @@ -283,12 +284,13 @@ public Tinder<P> tinder() {
return this.tinder;
}

public void close() throws Exception {
public void close() {
if(this.resolvable == null) return;
if(this.resolvable.isDone()) {
this.resolvable.get().close();
return;
}
if(this.resolvable.isDone())
try {
this.resolvable.get().close();
return;
} catch (Exception ignore) {}
this.resolvable.completeExceptionally(new InterruptedException("Particle boot was interrupted by Hypervisor closing!"));
}

Expand All @@ -314,7 +316,7 @@ public int hashCode() {
* Capacitor is a collection of Flux.
* As long as the keys are unique, you can store as much Flux as you want here.
*/
public static class Capacitor implements AutoCloseable {
public static class Capacitor implements Closure {
private final Map<String, Flux<? extends Particle>> flux = new ConcurrentHashMap<>();

public Capacitor() {}
Expand All @@ -334,7 +336,7 @@ public Optional<Flux<? extends Particle>> fetch(String key) {
}

@Override
public void close() throws Exception {
public void close() {
this.flux.values().forEach(f -> {
try {
f.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package group.aelysium.rustyconnector.common.cache;

import group.aelysium.rustyconnector.common.Closure;
import group.aelysium.rustyconnector.common.crypt.Snowflake;
import group.aelysium.rustyconnector.common.magic_link.packet.Packet;

import java.io.Closeable;
import java.io.IOException;
import java.util.*;

public class MessageCache implements Closeable {
public class MessageCache implements Closure {
private final Snowflake snowflakeGenerator = new Snowflake();
private final List<Packet.Status> ignoredStatuses;
private final List<Packet.Identification> ignoredTypes;
Expand Down Expand Up @@ -113,7 +112,7 @@ public List<CacheableMessage> fetchMessagesPage(int pageNumber) {
public void empty() { this.messages.clear(); }

@Override
public void close() throws IOException {
public void close() {
this.messages.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package group.aelysium.rustyconnector.common.cache;

import java.io.Closeable;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -11,9 +9,10 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import group.aelysium.rustyconnector.common.Closure;
import group.aelysium.rustyconnector.proxy.util.LiquidTimestamp;

public class TimeoutCache<K, V> implements Closeable, Map<K, V> {
public class TimeoutCache<K, V> implements Closure, Map<K, V> {
private final Map<K, TimedValue<V>> map = new ConcurrentHashMap<>();
private final LiquidTimestamp expiration;
private final ScheduledExecutorService clock = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -44,7 +43,7 @@ public void onTimeout(Consumer<V> consumer) {
}

@Override
public void close() throws IOException {
public void close() {
this.shutdown = true;
this.onTimeout.clear();
this.clock.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package group.aelysium.rustyconnector.common.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
/**
* Allows you to programmatically add YAML comments to your {@link Config}.
*/
public @interface Comment {
/**
* The order of this entry.
* This value manages how the elements will appear in the config.
*/
int order() default 0;

/**
* The comment to show.
* Each new array element will be put on a newline.
* If a entry is provided without a leading # character, one will be added.
*/
String[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package group.aelysium.rustyconnector.common.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Config {
/**
* The location of the config.
* Points to the config's physical path on the machine.
* If the config exists, it'll load the details.
* If it doesn't exist, it'll be created.
*/
String value();
}
Loading

0 comments on commit 101f19c

Please sign in to comment.