Browse Source

ненавижу потоки

master
gsd 2 years ago
parent
commit
83b4b392fa
  1. 4
      src/main/java/app/configs/ProtocolA2S.java
  2. 28
      src/main/java/app/entities/Stats.java
  3. 21
      src/main/java/app/entities/server/Server.java
  4. 3
      src/main/java/app/updates/PlayersUpdater.java

4
src/main/java/app/configs/ProtocolA2S.java

@ -20,7 +20,7 @@ public class ProtocolA2S {
@Scope("prototype")
@Bean
public SourceQueryClient GetSourceQueryClient() {
ExecutorService customExecutor = Executors.newCachedThreadPool();
ExecutorService customExecutor = Executors.newFixedThreadPool(1);
SourceQueryOptions options = SourceQueryOptions.builder()
.option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST)
.option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor)
@ -31,7 +31,7 @@ public class ProtocolA2S {
@Scope("prototype")
@Bean
public SourceRconClient GetSourceRconClient() {
ExecutorService customExecutor = Executors.newCachedThreadPool();
ExecutorService customExecutor = Executors.newFixedThreadPool(1);
SourceRconOptions options = SourceRconOptions.builder()
//.option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST)
.option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor)

28
src/main/java/app/entities/Stats.java

@ -8,6 +8,7 @@ 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.exceptions.ReadTimeoutException;
import com.ibasco.agql.protocols.valve.source.query.SourceQueryClient;
import com.ibasco.agql.protocols.valve.source.query.info.SourceQueryInfoResponse;
import com.ibasco.agql.protocols.valve.source.query.players.SourceQueryPlayerResponse;
@ -69,14 +70,18 @@ public class Stats {
uniq.merge(key, value, (x,y) -> y);
}
public void RefreshServerA2SData(ApplicationContext context, String server_name){
public void RefreshServerA2SData(ApplicationContext context, String server_name) throws IOException {
try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) {
SourceQueryInfoResponse info = sourceQueryClient.getInfo(getServers().get(server_name).getInetAddress()).join();
getServers().get(server_name).UpdateStatusFromA2S(info);
} catch (IOException err_info) {
getServers().get(server_name).SetDownStatus();
return;
}
sourceQueryClient.getInfo(getServers().get(server_name).getInetAddress()).whenComplete((info, error) -> {
sourceQueryClient.getExecutor().shutdown();
if (error != null) {
getServers().get(server_name).SetDownStatus();
return;
}
getServers().get(server_name).UpdateStatusFromA2S(info);
}).join();
} catch (CompletionException err) {}
if (!getServers().get(server_name).isStatus() || getServers().get(server_name).getPlayer_count() < 1) {
return;
}
@ -84,10 +89,11 @@ public class Stats {
//If player count > 0 make base player request
////////////////////////////////////////////////////////////////////////
try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) {
SourceQueryPlayerResponse players = sourceQueryClient.getPlayers(getServers().get(server_name).getInetAddress()).join();
getServers().get(server_name).UpdatePlayersFromA2S(players);
} catch (IOException err_players) {
return;
sourceQueryClient.getPlayers(getServers().get(server_name).getInetAddress()).whenComplete((players, error) -> {
sourceQueryClient.getExecutor().shutdown();
if (error != null) return;
getServers().get(server_name).UpdatePlayersFromA2S(players);
}).join();
}
///////////////////////////////////////////////////////////////////////
//Extend current players of rcon result

21
src/main/java/app/entities/server/Server.java

@ -109,24 +109,21 @@ public class Server {
@JsonIgnore
public String ExecuteRCON(ApplicationContext context, String command) {
try(SourceRconClient rconClient = context.getBean(SourceRconClient.class)) {
try (SourceRconClient rconClient = context.getBean(SourceRconClient.class)) {
SourceRconAuthResponse response = rconClient.authenticate(getInetAddress(), getRcon_password().getBytes()).join();
if(!response.isAuthenticated()) {
if (!response.isAuthenticated()) {
rconClient.getExecutor().shutdown();
return null;
}
SourceRconCmdResponse res = rconClient.execute(getInetAddress(), command).join();
return res.getResult();
return rconClient.execute(getInetAddress(), command)
.thenApplyAsync(out -> {
rconClient.cleanup(true);
return out.getResult();
})
.join();
} catch (Exception err) {
return "";
}
/*SourceRconClient rconClient = context.getBean(SourceRconClient.class);
SourceRconAuthResponse response = rconClient.authenticate(getInetAddress(), getRcon_password().getBytes()).join();
if(!response.isAuthenticated()) {
return null;
}
SourceRconCmdResponse res = rconClient.execute(getInetAddress(), command).join();
rconClient.cleanup();
return res.getResult();*/
}
public void SetDownStatus() {

3
src/main/java/app/updates/PlayersUpdater.java

@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.time.Instant;
@Component
@ -40,7 +41,7 @@ public class PlayersUpdater {
}
@Job(name = "Update A2S data on: %0", retries = 0)
public void UpdatePlayersOnServer(String server_name) {
public void UpdatePlayersOnServer(String server_name) throws IOException {
stats.RefreshServerA2SData(context, server_name);
stats.getUpdates().merge("servers", Instant.now().getEpochSecond(), (x, y) -> y);
}

Loading…
Cancel
Save