MCSM: запрос информации о сервере
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.8.2-SNAPSHOT'
|
||||
version = '0.8.3-SNAPSHOT'
|
||||
|
||||
apply plugin: 'osgi'
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-05-02
|
||||
*/
|
||||
package asys.mcsmanager;
|
||||
|
||||
import asys.mcsmanager.packets.CS_Ping;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class CSPingJsonSerializer implements JsonSerializer<CS_Ping> {
|
||||
@Override
|
||||
public JsonElement serialize(CS_Ping pingPacket, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject resultJson = new JsonObject();
|
||||
|
||||
resultJson.addProperty("time", pingPacket.getTime());
|
||||
resultJson.addProperty("tps", pingPacket.getTps());
|
||||
resultJson.addProperty("online", pingPacket.getCountPlayers());
|
||||
|
||||
return resultJson;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,10 @@
|
||||
*/
|
||||
package asys.mcsmanager;
|
||||
|
||||
import asys.mcsmanager.packets.CS_Ping;
|
||||
import asys.webinterface.api.WebModule;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
@@ -17,11 +19,14 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MCSM_WebModule extends WebModule {
|
||||
private static final Gson GSON = new Gson();
|
||||
private Manager manager;
|
||||
private static final Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(ServerInfo.class, new ServerInfoSerializer())
|
||||
.registerTypeAdapter(CS_Ping.class, new CSPingJsonSerializer())
|
||||
.create();
|
||||
private final String MODULE_NAME = "mcsmanager";
|
||||
private final String MODULE_URL = "/"+MODULE_NAME;
|
||||
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
|
||||
private Manager manager;
|
||||
|
||||
MCSM_WebModule(Manager manager) {
|
||||
this.manager = manager;
|
||||
@@ -67,7 +72,18 @@ public class MCSM_WebModule extends WebModule {
|
||||
}
|
||||
|
||||
private boolean handleServersJson(HttpExchange httpExchange) throws IOException {
|
||||
if (httpExchange.getRequestURI().getQuery() != null &&
|
||||
!httpExchange.getRequestURI().getQuery().isEmpty()) {
|
||||
Map<String, String> query = this.queryToMap(httpExchange.getRequestURI().getQuery());
|
||||
ServerInfo serverInfo = manager.getInfo(query.get("clientid"));
|
||||
if (serverInfo == null) {
|
||||
this.sendJson(httpExchange, "{}");
|
||||
} else {
|
||||
this.sendJson(httpExchange, GSON.toJson(serverInfo));
|
||||
}
|
||||
} else {
|
||||
this.sendJson(httpExchange, serverList());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.*;
|
||||
|
||||
public class Manager {
|
||||
private Logger logger = LoggerFactory.getLogger(Manager.class);
|
||||
private Map<String, Deque<CS_Ping>> serversMap = new HashMap<>();
|
||||
private Map<String, ServerInfo> serversMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Добавляем в список ClientID
|
||||
@@ -25,7 +25,7 @@ public class Manager {
|
||||
}
|
||||
|
||||
logger.debug("addClientId: {}", clientId);
|
||||
serversMap.put(clientId, new LinkedList<>());
|
||||
serversMap.put(clientId, new ServerInfo(clientId));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -37,20 +37,14 @@ public class Manager {
|
||||
*/
|
||||
public void putInfo(String clientId, CS_Ping pingPacket) {
|
||||
if (!serversMap.containsKey(clientId)) return;
|
||||
|
||||
Deque<CS_Ping> list = serversMap.get(clientId);
|
||||
if (list.size() == 720) {
|
||||
list.removeFirst();
|
||||
}
|
||||
list.addLast(pingPacket);
|
||||
logger.debug("putInfo: {} [{}]", clientId, pingPacket.getTime());
|
||||
serversMap.get(clientId).putPing(pingPacket);
|
||||
}
|
||||
|
||||
public Set<String> getServerList() {
|
||||
return serversMap.keySet();
|
||||
}
|
||||
|
||||
public Deque<CS_Ping> getInfo(String clientId) {
|
||||
public ServerInfo getInfo(String clientId) {
|
||||
return serversMap.get(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-05-02
|
||||
*/
|
||||
package asys.mcsmanager;
|
||||
|
||||
import asys.mcsmanager.packets.CS_Ping;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class ServerInfo {
|
||||
private final Logger logger = LoggerFactory.getLogger(ServerInfo.class);
|
||||
private final String name;
|
||||
private int lastOnline;
|
||||
private Deque<CS_Ping> pingDeque;
|
||||
|
||||
public ServerInfo(String name) {
|
||||
this.name = name;
|
||||
this.pingDeque = new LinkedList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getLastOnline() {
|
||||
return lastOnline;
|
||||
}
|
||||
|
||||
public void setLastOnline(int lastOnline) {
|
||||
this.lastOnline = lastOnline;
|
||||
}
|
||||
|
||||
public Deque<CS_Ping> getPingDeque() {
|
||||
return pingDeque;
|
||||
}
|
||||
|
||||
public void putPing(CS_Ping ping) {
|
||||
if (pingDeque.size() == 720) {
|
||||
pingDeque.removeFirst();
|
||||
}
|
||||
pingDeque.addLast(ping);
|
||||
logger.debug("putPing: {} [{}]", name, ping.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-05-02
|
||||
*/
|
||||
package asys.mcsmanager;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ServerInfoSerializer implements JsonSerializer<ServerInfo> {
|
||||
@Override
|
||||
public JsonElement serialize(ServerInfo serverInfo, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject resultJson = new JsonObject();
|
||||
|
||||
resultJson.addProperty("name", serverInfo.getName());
|
||||
resultJson.addProperty("lastOnline", serverInfo.getLastOnline());
|
||||
resultJson.add("pingList", context.serialize(serverInfo.getPingDeque()));
|
||||
|
||||
return resultJson;
|
||||
}
|
||||
}
|
||||
@@ -71,9 +71,21 @@ var ServerInfo = React.createClass({
|
||||
});
|
||||
|
||||
var ServerListItem = React.createClass({
|
||||
requestServerInfo: function() {
|
||||
fetch('/mcsmanager/servers.json?clientid='+this.props.title)
|
||||
.then(function(response){
|
||||
response.json().then(function(data){
|
||||
console.debug(data);
|
||||
});
|
||||
})
|
||||
.catch(function(err){
|
||||
console.error(err);
|
||||
});
|
||||
},
|
||||
render: function(){
|
||||
return(
|
||||
ce('a', {className: 'list-group-item clearfix' + (this.props.active ? ' active' : ''), href: '#'},
|
||||
ce('a', {className: 'list-group-item clearfix' + (this.props.active ? ' active' : ''), href: '#',
|
||||
onClick: this.requestServerInfo},
|
||||
ce('span', {style: {'padding-top': '15px'}},
|
||||
ce('span', {className: 'glyphicon glyphicon-tasks'}),
|
||||
nbsp,
|
||||
|
||||
Reference in New Issue
Block a user