From ea8672e7e8367867c3318b50893e46a16ec14486 Mon Sep 17 00:00:00 2001 From: gsd Date: Fri, 24 Feb 2023 19:54:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B0=D0=B2=D0=B8=D0=B6?= =?UTF-8?q?=D1=83=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BA=D0=B8=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/configs/ProtocolA2S.java | 17 +++++---- src/main/java/app/entities/Stats.java | 38 ++++++++++++++----- src/main/java/app/entities/server/Server.java | 19 ++++++++-- src/main/java/app/services/ServerService.java | 2 +- src/main/java/app/services/StatsService.java | 2 +- src/main/java/app/updates/PlayersUpdater.java | 2 +- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/main/java/app/configs/ProtocolA2S.java b/src/main/java/app/configs/ProtocolA2S.java index fd3e42d..cba48ee 100644 --- a/src/main/java/app/configs/ProtocolA2S.java +++ b/src/main/java/app/configs/ProtocolA2S.java @@ -14,28 +14,29 @@ import org.springframework.context.annotation.Scope; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -@Configuration +//@Configuration public class ProtocolA2S { //https://ribasco.github.io/async-gamequery-lib/examples/source_query_example.html#blocking-example - @Scope("prototype") - @Bean + //@Scope("prototype") + /*@Bean public SourceQueryClient GetSourceQueryClient() { - ExecutorService customExecutor = Executors.newFixedThreadPool(1); + ExecutorService customExecutor = Executors.newCachedThreadPool(); SourceQueryOptions options = SourceQueryOptions.builder() .option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST) .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor) .build(); return new SourceQueryClient(options); } - - @Scope("prototype") +*/ + //@Scope("prototype") + /* @Bean public SourceRconClient GetSourceRconClient() { - ExecutorService customExecutor = Executors.newFixedThreadPool(1); + ExecutorService customExecutor = Executors.newCachedThreadPool(); SourceRconOptions options = SourceRconOptions.builder() //.option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST) .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor) .build(); return new SourceRconClient(options); - } + }*/ } \ No newline at end of file diff --git a/src/main/java/app/entities/Stats.java b/src/main/java/app/entities/Stats.java index 887581d..9ab41fa 100644 --- a/src/main/java/app/entities/Stats.java +++ b/src/main/java/app/entities/Stats.java @@ -8,8 +8,12 @@ import app.entities.server.players.RCONPlayer; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.ibasco.agql.core.enums.RateLimitType; import com.ibasco.agql.core.exceptions.ReadTimeoutException; +import com.ibasco.agql.core.util.FailsafeOptions; +import com.ibasco.agql.core.util.GeneralOptions; import com.ibasco.agql.protocols.valve.source.query.SourceQueryClient; +import com.ibasco.agql.protocols.valve.source.query.SourceQueryOptions; import com.ibasco.agql.protocols.valve.source.query.info.SourceQueryInfoResponse; import com.ibasco.agql.protocols.valve.source.query.players.SourceQueryPlayerResponse; import com.ibasco.agql.protocols.valve.source.query.rcon.exceptions.RconException; @@ -28,6 +32,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @Data @Component @@ -44,6 +50,7 @@ public class Stats { HashMap uniq = new HashMap<>(); HashMap updates = new HashMap<>(); Statistic statistic = new Statistic(); + @JsonGetter public Statistic getStatistic() { statistic.setTotal_servers(servers.size()); @@ -55,7 +62,7 @@ public class Stats { statistic.setPlayer_max(0); } - if(statistic.getPlayer_max() < statistic.getPlayer_now()) { + if (statistic.getPlayer_max() < statistic.getPlayer_now()) { statistic.setPlayer_max(statistic.getPlayer_now()); } return statistic; @@ -67,20 +74,22 @@ public class Stats { } public void UpdateUniq(String key, Long value) { - uniq.merge(key, value, (x,y) -> y); + uniq.merge(key, value, (x, y) -> y); } - public void RefreshServerA2SData(ApplicationContext context, String server_name) throws IOException { - try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { + public void RefreshServerA2SData(String server_name) throws IOException { + //try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { + try (SourceQueryClient sourceQueryClient = GetSourceQueryClient()) { sourceQueryClient.getInfo(getServers().get(server_name).getInetAddress()).whenComplete((info, error) -> { - sourceQueryClient.getExecutor().shutdown(); + if (!sourceQueryClient.getExecutor().isShutdown()) sourceQueryClient.getExecutor().shutdown(); if (error != null) { getServers().get(server_name).SetDownStatus(); return; } getServers().get(server_name).UpdateStatusFromA2S(info); }).join(); - } catch (CompletionException err) {} + } catch (CompletionException err) { + } if (!getServers().get(server_name).isStatus() || getServers().get(server_name).getPlayer_count() < 1) { return; @@ -88,9 +97,10 @@ public class Stats { //////////////////////////////////////////////////////////////////////// //If player count > 0 make base player request //////////////////////////////////////////////////////////////////////// - try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { + //try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { + try (SourceQueryClient sourceQueryClient = GetSourceQueryClient()) { sourceQueryClient.getPlayers(getServers().get(server_name).getInetAddress()).whenComplete((players, error) -> { - sourceQueryClient.getExecutor().shutdown(); + if (!sourceQueryClient.getExecutor().isShutdown()) sourceQueryClient.getExecutor().shutdown(); if (error != null) return; getServers().get(server_name).UpdatePlayersFromA2S(players); }).join(); @@ -99,10 +109,20 @@ public class Stats { //Extend current players of rcon result ////////////////////////////////////////////////////////////////////// try { - String response = getServers().get(server_name).ExecuteRCON(context,"status"); + String response = getServers().get(server_name).ExecuteRCON("status"); getServers().get(server_name).UpdatePlayersFromRCON(response); } catch (RconException | CompletionException err) { return; } } + + @JsonIgnore + public SourceQueryClient GetSourceQueryClient() { + ExecutorService customExecutor = Executors.newCachedThreadPool(); + SourceQueryOptions options = SourceQueryOptions.builder() + .option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST) + .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor) + .build(); + return new SourceQueryClient(options); + } } diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index c489035..7198fab 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -6,9 +6,11 @@ import app.entities.server.players.RCONPlayer; import app.entities.server.players.SourcePlayer; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.ibasco.agql.core.util.GeneralOptions; import com.ibasco.agql.protocols.valve.source.query.info.SourceQueryInfoResponse; import com.ibasco.agql.protocols.valve.source.query.players.SourceQueryPlayerResponse; import com.ibasco.agql.protocols.valve.source.query.rcon.SourceRconClient; +import com.ibasco.agql.protocols.valve.source.query.rcon.SourceRconOptions; import com.ibasco.agql.protocols.valve.source.query.rcon.message.SourceRconAuthResponse; import com.ibasco.agql.protocols.valve.source.query.rcon.message.SourceRconCmdResponse; import jakarta.persistence.criteria.CriteriaBuilder; @@ -21,6 +23,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @Data public class Server { @@ -108,11 +112,11 @@ public class Server { } @JsonIgnore - public String ExecuteRCON(ApplicationContext context, String command) { - try (SourceRconClient rconClient = context.getBean(SourceRconClient.class)) { + public String ExecuteRCON(String command) { + try (SourceRconClient rconClient = GetSourceRconClient()) { SourceRconAuthResponse response = rconClient.authenticate(getInetAddress(), getRcon_password().getBytes()).join(); if (!response.isAuthenticated()) { - rconClient.getExecutor().shutdown(); + if (!rconClient.getExecutor().isShutdown()) rconClient.getExecutor().shutdown(); return null; } return rconClient.execute(getInetAddress(), command) @@ -125,6 +129,15 @@ public class Server { return ""; } } + @JsonIgnore + public SourceRconClient GetSourceRconClient() { + ExecutorService customExecutor = Executors.newCachedThreadPool(); + SourceRconOptions options = SourceRconOptions.builder() + //.option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST) + .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor) + .build(); + return new SourceRconClient(options); + } public void SetDownStatus() { setStatus(false); diff --git a/src/main/java/app/services/ServerService.java b/src/main/java/app/services/ServerService.java index 0221318..3771bba 100644 --- a/src/main/java/app/services/ServerService.java +++ b/src/main/java/app/services/ServerService.java @@ -22,6 +22,6 @@ public class ServerService { public boolean kickPlayer(PlayerProfile playerProfile){ if (playerProfile == null || playerProfile.getPlay_on() == null) return false; return stats.getServers().get(playerProfile.getPlay_on().getServer_id()) - .ExecuteRCON(applicationContext, "sm_kick #%d kicked from backend".formatted(playerProfile.getPlay_on().getPlayer_id())).contains("kicked"); + .ExecuteRCON("sm_kick #%d kicked from backend".formatted(playerProfile.getPlay_on().getPlayer_id())).contains("kicked"); } } diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java index 3023114..ef257c2 100644 --- a/src/main/java/app/services/StatsService.java +++ b/src/main/java/app/services/StatsService.java @@ -51,6 +51,6 @@ public class StatsService { public String rconExecute(String server_name, String command) { if (!stats.getServers().containsKey(server_name)) return "Invalid server name"; - return stats.getServers().get(server_name).ExecuteRCON(applicationContext, command); + return stats.getServers().get(server_name).ExecuteRCON(command); } } diff --git a/src/main/java/app/updates/PlayersUpdater.java b/src/main/java/app/updates/PlayersUpdater.java index e1c941e..e9308fd 100644 --- a/src/main/java/app/updates/PlayersUpdater.java +++ b/src/main/java/app/updates/PlayersUpdater.java @@ -42,7 +42,7 @@ public class PlayersUpdater { @Job(name = "Update A2S data on: %0", retries = 0) public void UpdatePlayersOnServer(String server_name) throws IOException { - stats.RefreshServerA2SData(context, server_name); + stats.RefreshServerA2SData(server_name); stats.getUpdates().merge("servers", Instant.now().getEpochSecond(), (x, y) -> y); } }