Skip to content

Commit

Permalink
Initial work on implementing reactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Feb 24, 2024
1 parent 42a9b85 commit e412147
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ dependencies {
exclude("com.google.code.gson", "gson")
exclude("org.slf4j", "slf4j-api")
}
api(libs.bundles.reactor.netty)

// For class injection
api(libs.injector)
Expand Down
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ flatlaf = "3.4"
flatlaf-inter-version = "4.0"
flatlaf-jetbrains-mono-version = "2.304"
kyori = "4.16.0"
reactor = "1.1.16"

[plugins]
blossom = "net.kyori.blossom:2.1.0"
Expand Down Expand Up @@ -61,7 +62,7 @@ via-aprilfools = { module = "net.raphimc:ViaAprilFools", version.ref = "via-apri
via-loader = { module = "net.raphimc:ViaLoader", version.ref = "via-loader" }
via-bedrock = { module = "net.raphimc:ViaBedrock", version.ref = "via-bedrock" }
snakeyaml = "org.yaml:snakeyaml:2.2"
kyori-plain = { module = "net.kyori:adventure-text-serializer-plain", version.ref = "kyori"}
kyori-plain = { module = "net.kyori:adventure-text-serializer-plain", version.ref = "kyori" }
kyori-gson = { module = "net.kyori:adventure-text-serializer-gson", version.ref = "kyori" }
kyori-ansi = { module = "net.kyori:adventure-text-serializer-ansi", version.ref = "kyori" }
commons-validator = "commons-validator:commons-validator:1.8.0"
Expand All @@ -85,6 +86,8 @@ grpc-proto = { module = "io.grpc:grpc-protobuf", version.ref = "grpc" }
grpc-services = { module = "io.grpc:grpc-services", version.ref = "grpc" }
grpc-stub = { module = "io.grpc:grpc-stub", version.ref = "grpc" }
grpc-netty = { module = "io.grpc:grpc-netty", version.ref = "grpc" }
reactor-netty-core = { module = "io.projectreactor.netty:reactor-netty-core", version.ref = "reactor" }
reactor-netty-http = { module = "io.projectreactor.netty:reactor-netty-http", version.ref = "reactor" }
javax-annotations = "javax.annotation:javax.annotation-api:1.3.2"
junit = "org.junit.jupiter:junit-jupiter:5.10.2"

Expand All @@ -95,3 +98,4 @@ mixins = ["classtransform-mixinstranslator", "classtransform-mixinsdummy", "clas
flatlaf = ["flatlaf", "flatlaf-extras", "flatlaf-intellij-themes", "flatlaf-fonts-inter", "flatlaf-fonts-jetbrains-mono"]
kyori = ["kyori-plain", "kyori-gson", "kyori-ansi"]
ansi4j = ["ansi4j-core-api", "ansi4j-core-impl"]
reactor-netty = ["reactor-netty-core", "reactor-netty-http"]
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ dependencyResolutionManagement {
maven("https://jitpack.io/") {
name = "JitPack Repository"
}
maven("https://repo.spring.io/milestone") {
name = "Spring Milestone Repository"
}
mavenCentral()
}
versionCatalogs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public SoulFireServer(String host, int port) {
"SoulFire is outdated! Current version: {}, latest version: {}",
BuildData.VERSION,
newVersion.get());
} else {
log.info("SoulFire is up to date!");
}

registerInternalServerExtensions();
Expand Down
54 changes: 27 additions & 27 deletions src/main/java/net/pistonmaster/soulfire/util/SFUpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import javax.net.ssl.HttpsURLConnection;
import lombok.extern.slf4j.Slf4j;
import net.pistonmaster.soulfire.builddata.BuildData;
import net.pistonmaster.soulfire.server.util.VersionComparator;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

@Slf4j
public class SFUpdateChecker {
private static final URI UPDATE_URL = URI.create("https://api.github.com/repos/AlexProgrammerDE/SoulFire/releases/latest");
private static SFUpdateChecker instance;
private final String updateVersion;

Expand All @@ -56,34 +57,33 @@ private static String checkForUpdates() {
}

try {
var url =
URI.create("https://api.github.com/repos/AlexProgrammerDE/SoulFire/releases/latest")
.toURL();
var connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "SoulFire");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
return HttpClient.create()
.responseTimeout(Duration.ofSeconds(5))
.headers(h -> h.add("User-Agent", "SoulFire"))
.get()
.uri(UPDATE_URL)
.responseSingle((res, content) -> {
if (res.status().code() != 200) {
log.warn("Failed to check for updates: {}", res.status().code());
return Mono.empty();
}

if (connection.getResponseCode() != 200) {
log.warn("Failed to check for updates: {}", connection.getResponseCode());
return null;
}
return content.asString().mapNotNull(s -> {
var responseObject = new Gson().fromJson(s, JsonObject.class);

JsonObject response;
try (var stream = connection.getInputStream()) {
response = new Gson().fromJson(new InputStreamReader(stream), JsonObject.class);
}

var latestVersion = response.get("tag_name").getAsString();
if (VersionComparator.isNewer(BuildData.VERSION, latestVersion)) {
return latestVersion;
}
} catch (IOException e) {
var latestVersion = responseObject.get("tag_name").getAsString();
if (VersionComparator.isNewer(BuildData.VERSION, latestVersion)) {
return latestVersion;
} else {
return null;
}
});
})
.block();
} catch (Exception e) {
log.warn("Failed to check for updates", e);
return null;
}

return null;
}

public Optional<String> getUpdateVersion() {
Expand Down

0 comments on commit e412147

Please sign in to comment.