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. 47
      src/main/java/app/updates/PlayersUpdater.java
  4. 6
      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
@Configuration
public class UpdatePlayersAspect {
PlayersUpdater playersUpdater;
/*PlayersUpdater;
private long last_burst_update = 0;
private int burst_timeout = 5;
@Autowired
public UpdatePlayersAspect(PlayersUpdater playersUpdater) {
this.playersUpdater = playersUpdater;
}
//@Autowired
//public UpdatePlayersAspect(PlayersUpdater playersUpdater) {
// this.playersUpdater = playersUpdater;
//}
@Before("@annotation(app.annotations.interfaces.BurstUpdatePlayers) && args(..)")
public void before() {
if (Instant.now().getEpochSecond() - last_burst_update < burst_timeout) {
/*if (Instant.now().getEpochSecond() - last_burst_update < burst_timeout) {
return;
}
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};
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);
}
}
public Server() {}
@JsonIgnore
public String getMapCleared() {
@ -93,10 +75,12 @@ public class Server extends ExternalValveClient {
last_update = Instant.now().getEpochSecond();
}
public void RefreshLastCheck(int sec) {
public boolean RefreshLastCheck(int sec) {
if (Instant.now().getEpochSecond() - last_update > sec) {
SetDownStatus();
return true;
}
return false;
}
public void RefreshServerFromRequest(ServerRequestBody serverRequestBody) {

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

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

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

@ -43,8 +43,10 @@ public class ServersHandler extends TextWebSocketHandler implements BaseWebsocke
logger.info("Session {} open", session.getId());
activeSessions.put(session.getId(), session);
super.afterConnectionEstablished(session);
synchronized (session) {
session.sendMessage(getServersPayload());
}
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
@ -61,9 +63,11 @@ public class ServersHandler extends TextWebSocketHandler implements BaseWebsocke
private void push2All(TextMessage msg) throws IOException {
for (Map.Entry<String, WebSocketSession> otherSession : Collections.synchronizedSet(activeSessions.entrySet())) {
try {
synchronized (otherSession) {
otherSession.getValue().sendMessage(msg);
}
} 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