Browse Source

websocket support v3 fix 3 threads

master
gsd 1 year ago
parent
commit
5d87446bf3
  1. 15
      src/main/java/app/annotations/impl/UpdatePlayersAspect.java
  2. 24
      src/main/java/app/entities/server/Server.java
  3. 53
      src/main/java/app/updates/PlayersUpdater.java
  4. 10
      src/main/java/app/websocket/handlers/ServersHandler.java

15
src/main/java/app/annotations/impl/UpdatePlayersAspect.java

@ -11,21 +11,20 @@ import java.time.Instant;
@Aspect @Aspect
@Configuration @Configuration
public class UpdatePlayersAspect { public class UpdatePlayersAspect {
PlayersUpdater playersUpdater; /*PlayersUpdater;
private long last_burst_update = 0; private long last_burst_update = 0;
private int burst_timeout = 5; private int burst_timeout = 5;
@Autowired //@Autowired
public UpdatePlayersAspect(PlayersUpdater playersUpdater) { //public UpdatePlayersAspect(PlayersUpdater playersUpdater) {
this.playersUpdater = playersUpdater; // this.playersUpdater = playersUpdater;
} //}
@Before("@annotation(app.annotations.interfaces.BurstUpdatePlayers) && args(..)") @Before("@annotation(app.annotations.interfaces.BurstUpdatePlayers) && args(..)")
public void before() { public void before() {
if (Instant.now().getEpochSecond() - last_burst_update < burst_timeout) { /*if (Instant.now().getEpochSecond() - last_burst_update < burst_timeout) {
return; return;
} }
playersUpdater.burstUpdater(); playersUpdater.burstUpdater();
last_burst_update = Instant.now().getEpochSecond(); last_burst_update = Instant.now().getEpochSecond();*/
}
} }

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

@ -44,25 +44,7 @@ public class Server extends ExternalValveClient {
double[] city_pos = {0.0, 0.0}; double[] city_pos = {0.0, 0.0};
public Server() { public Server() {}
if (System.getenv("A2S") != null && System.getenv("A2S").equals("false")) {
logger.warn("Create cache last time updater: {} server", name);
CreateTaskUpdater(() -> {
logger.info("Update players cache from: {} server", name);
RefreshLastCheck(60);
return null;
}, 15000);
} else if (System.getenv("A2S") != null && System.getenv("A2S").equals("true")) {
logger.warn("Create a2s updater: {} server", name);
CreateTaskUpdater(() -> {
logger.info("Update players from: {} server", name);
RefreshServerA2SData();
return null;
}, 60000);
} else {
logger.warn("{} server not be update", name);
}
}
@JsonIgnore @JsonIgnore
public String getMapCleared() { public String getMapCleared() {
@ -93,10 +75,12 @@ public class Server extends ExternalValveClient {
last_update = Instant.now().getEpochSecond(); last_update = Instant.now().getEpochSecond();
} }
public void RefreshLastCheck(int sec) { public boolean RefreshLastCheck(int sec) {
if (Instant.now().getEpochSecond() - last_update > sec) { if (Instant.now().getEpochSecond() - last_update > sec) {
SetDownStatus(); SetDownStatus();
return true;
} }
return false;
} }
public void RefreshServerFromRequest(ServerRequestBody serverRequestBody) { public void RefreshServerFromRequest(ServerRequestBody serverRequestBody) {

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

@ -1,62 +1,61 @@
package app.updates; package app.updates;
import app.entities.Stats; import app.entities.Stats;
import app.websocket.handlers.ServersHandler;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@Component @Component
public class PlayersUpdater extends BaseUpdater{ public class PlayersUpdater extends BaseUpdater{
Stats stats; private Stats stats;
private ServersHandler serversHandler;
@Value("${backend.updates.a2s}") @Value("${backend.updates.a2s}")
private boolean update = false; private boolean update = false;
private int timeout = 60 * 1000; private int timeout = 60 * 1000;
private final Logger logger = LoggerFactory.getLogger(PlayersUpdater.class); private final Logger logger = LoggerFactory.getLogger(PlayersUpdater.class);
@Autowired @Autowired
public PlayersUpdater(Stats stats) { public PlayersUpdater(Stats stats, ServersHandler serversHandler) {
this.stats = stats; this.stats = stats;
this.serversHandler = serversHandler;
} }
/* @PostConstruct
public void updateValues() { public void updateValuesInit() {
if (update) { logger.info("Players update: a2s = {}", System.getenv("A2S"));
logger.warn("Updater enabled"); stats.getServers().forEach((server_name, server) -> {
if (stats.getServers().size()==0) logger.error("Not found servers to update"); if (System.getenv("A2S") != null && System.getenv("A2S").equals("false")) {
stats.getServers().forEach((server_name, server) -> { logger.warn("Create cache last time updater: {} server", server_name);
CreateTaskUpdater(() -> { CreateTaskUpdater(() -> {
logger.info("Update players from: {} server", server_name); //logger.info("Update players cache from: {} server", server_name);
server.RefreshServerA2SData(); if (server.RefreshLastCheck(60)) {
stats.getUpdates().merge(server_name, Instant.now().getEpochSecond(), (x, y) -> y); serversHandler.pushServer(server_name, server);
}
return null; return null;
}, timeout); }, 15000);
}); } else if (System.getenv("A2S") != null && System.getenv("A2S").equals("true")) {
} else { logger.warn("Create a2s updater: {} server", server_name);
logger.warn("A2S Refresh disabled! Enable last timecheck");
if (stats.getServers().size()==0) logger.error("Not found servers to update");
stats.getServers().forEach((server_name, server) -> {
CreateTaskUpdater(() -> { CreateTaskUpdater(() -> {
logger.info("Update players from: {} server", server_name); logger.info("Update players from: {} server", server_name);
server.RefreshLastCheck(60); server.RefreshServerA2SData();
return null; return null;
}, 15000); }, 60000);
}); } else {
} logger.warn("{} server not be update", server_name);
}*/ }
});
}
public void burstUpdater() { public void burstUpdater() {
if (!update) return; if (!update) return;

10
src/main/java/app/websocket/handlers/ServersHandler.java

@ -43,7 +43,9 @@ public class ServersHandler extends TextWebSocketHandler implements BaseWebsocke
logger.info("Session {} open", session.getId()); logger.info("Session {} open", session.getId());
activeSessions.put(session.getId(), session); activeSessions.put(session.getId(), session);
super.afterConnectionEstablished(session); super.afterConnectionEstablished(session);
session.sendMessage(getServersPayload()); synchronized (session) {
session.sendMessage(getServersPayload());
}
} }
@Override @Override
@ -61,9 +63,11 @@ public class ServersHandler extends TextWebSocketHandler implements BaseWebsocke
private void push2All(TextMessage msg) throws IOException { private void push2All(TextMessage msg) throws IOException {
for (Map.Entry<String, WebSocketSession> otherSession : Collections.synchronizedSet(activeSessions.entrySet())) { for (Map.Entry<String, WebSocketSession> otherSession : Collections.synchronizedSet(activeSessions.entrySet())) {
try { try {
otherSession.getValue().sendMessage(msg); synchronized (otherSession) {
otherSession.getValue().sendMessage(msg);
}
} catch (Exception err) { } catch (Exception err) {
logger.error("Cannot send update servers status to session: {}", otherSession.getValue().getId()); logger.error("Cannot send update servers status to session: {}, error: {}", otherSession.getValue().getId(), err.toString());
} }
} }
} }

Loading…
Cancel
Save