MCSM: рефакторинг класса Manager
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
group = 'asys'
|
group = 'asys'
|
||||||
version = '0.8.13-SNAPSHOT'
|
version = '0.10-SNAPSHOT'
|
||||||
|
|
||||||
apply plugin: 'osgi'
|
apply plugin: 'osgi'
|
||||||
|
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ public class Activator implements BundleActivator, ServiceListener {
|
|||||||
Config config = serviceConfigTracker.getService();
|
Config config = serviceConfigTracker.getService();
|
||||||
if (config == null) throw new RuntimeException("Service 'Config' is not avalable!");
|
if (config == null) throw new RuntimeException("Service 'Config' is not avalable!");
|
||||||
|
|
||||||
Manager manager = new Manager();
|
module = new MCSM_WebModule();
|
||||||
|
|
||||||
module = new MCSM_WebModule(manager);
|
|
||||||
|
|
||||||
logger.debug("Get service: {}", Webinterface.class);
|
logger.debug("Get service: {}", Webinterface.class);
|
||||||
serviceTracker = new ServiceTracker<>(context, Webinterface.class, null);
|
serviceTracker = new ServiceTracker<>(context, Webinterface.class, null);
|
||||||
@@ -44,13 +42,13 @@ public class Activator implements BundleActivator, ServiceListener {
|
|||||||
String passcode = config.getString("asys.mcsmanager.passcode", "testpasscode");
|
String passcode = config.getString("asys.mcsmanager.passcode", "testpasscode");
|
||||||
logger.debug("Start server manager: {}:{}", host, port);
|
logger.debug("Start server manager: {}:{}", host, port);
|
||||||
serverManager = new asys.mcsmanager.server.Server();
|
serverManager = new asys.mcsmanager.server.Server();
|
||||||
serverManager.start(host, port, passcode, manager);
|
serverManager.start(host, port, passcode);
|
||||||
|
|
||||||
host = config.getString("asys.mcsmanager.webconsole.host", "127.0.0.1");
|
host = config.getString("asys.mcsmanager.webconsole.host", "127.0.0.1");
|
||||||
port = config.getInt("asys.mcsmanager.webconsole.port", 8770);
|
port = config.getInt("asys.mcsmanager.webconsole.port", 8770);
|
||||||
logger.debug("Start webconsole server: {}:{}", host, port);
|
logger.debug("Start webconsole server: {}:{}", host, port);
|
||||||
webconsoleServer = new asys.mcsmanager.websocket.Server();
|
webconsoleServer = new asys.mcsmanager.websocket.Server();
|
||||||
webconsoleServer.start(host, port, manager);
|
webconsoleServer.start(host, port);
|
||||||
|
|
||||||
serviceConfigTracker.close();
|
serviceConfigTracker.close();
|
||||||
}
|
}
|
||||||
@@ -82,7 +80,6 @@ public class Activator implements BundleActivator, ServiceListener {
|
|||||||
logger.debug("service not found =(");
|
logger.debug("service not found =(");
|
||||||
}
|
}
|
||||||
} catch (InterruptedException ignore) {
|
} catch (InterruptedException ignore) {
|
||||||
// ignore
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,11 +27,6 @@ public class MCSM_WebModule extends WebModule {
|
|||||||
private final String MODULE_URL = "/"+MODULE_NAME;
|
private final String MODULE_URL = "/"+MODULE_NAME;
|
||||||
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
|
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
|
||||||
private final Pattern URL_PATTERN_CSS = Pattern.compile(MODULE_URL+"/(\\w+)\\.css");
|
private final Pattern URL_PATTERN_CSS = Pattern.compile(MODULE_URL+"/(\\w+)\\.css");
|
||||||
private Manager manager;
|
|
||||||
|
|
||||||
MCSM_WebModule(Manager manager) {
|
|
||||||
this.manager = manager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -89,7 +84,7 @@ public class MCSM_WebModule extends WebModule {
|
|||||||
if (httpExchange.getRequestURI().getQuery() != null &&
|
if (httpExchange.getRequestURI().getQuery() != null &&
|
||||||
!httpExchange.getRequestURI().getQuery().isEmpty()) {
|
!httpExchange.getRequestURI().getQuery().isEmpty()) {
|
||||||
Map<String, String> query = this.queryToMap(httpExchange.getRequestURI().getQuery());
|
Map<String, String> query = this.queryToMap(httpExchange.getRequestURI().getQuery());
|
||||||
ServerInfo serverInfo = manager.getInfo(query.get("clientid"));
|
ServerInfo serverInfo = Manager.getInstance().getServer(query.get("clientid"));
|
||||||
if (serverInfo == null) {
|
if (serverInfo == null) {
|
||||||
this.sendJson(httpExchange, "{}");
|
this.sendJson(httpExchange, "{}");
|
||||||
} else {
|
} else {
|
||||||
@@ -102,6 +97,6 @@ public class MCSM_WebModule extends WebModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private JsonElement serverList() {
|
private JsonElement serverList() {
|
||||||
return GSON.toJsonTree(manager.getServerList());
|
return GSON.toJsonTree(Manager.getInstance().getServerList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,92 +5,73 @@
|
|||||||
package asys.mcsmanager;
|
package asys.mcsmanager;
|
||||||
|
|
||||||
import asys.mcsmanager.packets.CS_ConsoleMessage;
|
import asys.mcsmanager.packets.CS_ConsoleMessage;
|
||||||
import asys.mcsmanager.packets.CS_Ping;
|
|
||||||
import asys.mcsmanager.packets.SC_Command;
|
import asys.mcsmanager.packets.SC_Command;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Manager {
|
public class Manager {
|
||||||
private Logger logger = LoggerFactory.getLogger(Manager.class);
|
private static Manager instance = new Manager();
|
||||||
private Map<String, ServerInfo> serversMap = new HashMap<>();
|
private Map<String, ServerInfo> serverMap = new HashMap<>();
|
||||||
private Map<String, Channel> serverChannels = new HashMap<>();
|
private Map<String, List<Channel>> webconsoleListeners = new HashMap<>();
|
||||||
private List<Channel> webconsoleListener = new ArrayList<>();
|
|
||||||
|
|
||||||
/**
|
public static Manager getInstance() {
|
||||||
* Добавляем в список ClientID
|
return instance;
|
||||||
* @param clientId id сервера. Чувствителен к регистру
|
|
||||||
* @return <code>false</code>, если сервер с таким id уже имеется
|
|
||||||
*/
|
|
||||||
public boolean addClientId(String clientId, Channel channel) {
|
|
||||||
if (serversMap.containsKey(clientId)) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("addClientId: {}", clientId);
|
private Manager(){
|
||||||
serversMap.put(clientId, new ServerInfo(clientId));
|
|
||||||
serverChannels.put(clientId, channel);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeClientId(String clientId) {
|
public void addServer(String serverName, Channel channel) {
|
||||||
if (clientId != null) {
|
this.serverMap.put(serverName, new ServerInfo(serverName, channel));
|
||||||
serverChannels.remove(clientId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sendCommand(String clientId, String command) {
|
public boolean existsServer(String serverName) {
|
||||||
/*
|
return this.serverMap.containsKey(serverName);
|
||||||
if (serverChannels.containsKey(clientId)) {
|
|
||||||
serverChannels.get(clientId).writeAndFlush(new SC_Command(command));
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//FIXME временный костыль
|
|
||||||
Channel channel = serverChannels.entrySet().iterator().next().getValue();
|
|
||||||
channel.writeAndFlush(new SC_Command(command));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public ServerInfo getServer(String serverName) {
|
||||||
* Дополнить информация о сервере.
|
return this.serverMap.get(serverName);
|
||||||
* Если сервер отсутствует в списке, информация будет потеряна.
|
|
||||||
* @param clientId id сервера. Чувствителен к регистру
|
|
||||||
* @param pingPacket
|
|
||||||
*/
|
|
||||||
public void putInfo(String clientId, CS_Ping pingPacket) {
|
|
||||||
if (!serversMap.containsKey(clientId)) return;
|
|
||||||
serversMap.get(clientId).putPing(pingPacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getServerList() {
|
public Set<String> getServerList() {
|
||||||
return serversMap.keySet();
|
return this.serverMap.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerInfo getInfo(String clientId) {
|
public void removeServer(String serverName) {
|
||||||
return serversMap.get(clientId);
|
this.serverMap.remove(serverName);
|
||||||
|
if (this.webconsoleListeners.containsKey(serverName)) {
|
||||||
|
this.webconsoleListeners.get(serverName).forEach(Channel::close);
|
||||||
|
this.webconsoleListeners.remove(serverName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addWebConsoleListener(Channel channel) {
|
public void addWebconsoleListener(String serverName, Channel channel) {
|
||||||
this.webconsoleListener.add(channel);
|
this.webconsoleListeners
|
||||||
|
.computeIfAbsent(serverName, v -> new ArrayList<>())
|
||||||
|
.add(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeWebConsoleListener(Channel channel) {
|
public void removeWebconsoleListener(String serverName, Channel channel) {
|
||||||
this.webconsoleListener.remove(channel);
|
this.webconsoleListeners.get(serverName).remove(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendBroadcastToWebConsoleListeners(CS_ConsoleMessage message) {
|
public void broadcastConsoleMessage(String serverName, CS_ConsoleMessage packet) {
|
||||||
for (Channel channel : this.webconsoleListener) {
|
if (this.webconsoleListeners.containsKey(serverName)) {
|
||||||
|
this.webconsoleListeners.get(serverName).forEach(channel -> {
|
||||||
channel.writeAndFlush(new TextWebSocketFrame(String.format(
|
channel.writeAndFlush(new TextWebSocketFrame(String.format(
|
||||||
"[L:%d] %s",
|
"[L:%d] %s",
|
||||||
message.getLevel(),
|
packet.getLevel(),
|
||||||
message.getMessage()
|
packet.getMessage()
|
||||||
)));
|
)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendCommand(String serverName, String command) {
|
||||||
|
if (this.serverMap.containsKey(serverName)) {
|
||||||
|
this.serverMap.get(serverName).getChannel().writeAndFlush(new SC_Command(command));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package asys.mcsmanager;
|
package asys.mcsmanager;
|
||||||
|
|
||||||
import asys.mcsmanager.packets.CS_Ping;
|
import asys.mcsmanager.packets.CS_Ping;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -14,18 +15,23 @@ import java.util.LinkedList;
|
|||||||
public class ServerInfo {
|
public class ServerInfo {
|
||||||
private final Logger logger = LoggerFactory.getLogger(ServerInfo.class);
|
private final Logger logger = LoggerFactory.getLogger(ServerInfo.class);
|
||||||
private final String name;
|
private final String name;
|
||||||
|
private Channel channel;
|
||||||
private int lastOnline;
|
private int lastOnline;
|
||||||
private Deque<CS_Ping> pingDeque;
|
private Deque<CS_Ping> pingDeque = new LinkedList<>();
|
||||||
|
|
||||||
public ServerInfo(String name) {
|
public ServerInfo(String name, Channel channel) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.pingDeque = new LinkedList<>();
|
this.channel = channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Channel getChannel() {
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
public int getLastOnline() {
|
public int getLastOnline() {
|
||||||
return lastOnline;
|
return lastOnline;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
package asys.mcsmanager.server;
|
package asys.mcsmanager.server;
|
||||||
|
|
||||||
import asys.mcsmanager.Manager;
|
|
||||||
import asys.mcsmanager.packets.CS_Ping;
|
import asys.mcsmanager.packets.CS_Ping;
|
||||||
import asys.mcsmanager.packets.Packet;
|
import asys.mcsmanager.packets.Packet;
|
||||||
import asys.mcsmanager.packets.codec.PacketDecoder;
|
import asys.mcsmanager.packets.codec.PacketDecoder;
|
||||||
@@ -25,11 +24,9 @@ public class Server {
|
|||||||
);
|
);
|
||||||
private EventLoopGroup bossGroup, workerGroup;
|
private EventLoopGroup bossGroup, workerGroup;
|
||||||
static String passcode;
|
static String passcode;
|
||||||
static Manager manager;
|
|
||||||
|
|
||||||
public void start(String host, int port, String passcode, Manager manager) {
|
public void start(String host, int port, String passcode) {
|
||||||
Server.passcode = passcode;
|
Server.passcode = passcode;
|
||||||
Server.manager = manager;
|
|
||||||
bossGroup = new NioEventLoopGroup(1);
|
bossGroup = new NioEventLoopGroup(1);
|
||||||
workerGroup = new NioEventLoopGroup();
|
workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package asys.mcsmanager.server;
|
package asys.mcsmanager.server;
|
||||||
|
|
||||||
|
import asys.mcsmanager.Manager;
|
||||||
import asys.mcsmanager.packets.*;
|
import asys.mcsmanager.packets.*;
|
||||||
import com.google.common.collect.BiMap;
|
import com.google.common.collect.BiMap;
|
||||||
import com.google.common.collect.ImmutableBiMap;
|
import com.google.common.collect.ImmutableBiMap;
|
||||||
@@ -16,7 +17,6 @@ import java.util.Map;
|
|||||||
|
|
||||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_HANDLERS;
|
import static asys.mcsmanager.packets.codec.Params.KNOWN_HANDLERS;
|
||||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_PACKETS;
|
import static asys.mcsmanager.packets.codec.Params.KNOWN_PACKETS;
|
||||||
import static asys.mcsmanager.server.Server.manager;
|
|
||||||
|
|
||||||
class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacketHandler {
|
class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacketHandler {
|
||||||
private static final BiMap<Integer, Class<? extends Packet>> handshakePackets = ImmutableBiMap.of(
|
private static final BiMap<Integer, Class<? extends Packet>> handshakePackets = ImmutableBiMap.of(
|
||||||
@@ -58,7 +58,7 @@ class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacke
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext context) throws Exception {
|
public void channelInactive(ChannelHandlerContext context) throws Exception {
|
||||||
manager.removeClientId(context.channel().attr(CLIENTID).get());
|
Manager.getInstance().removeServer(context.channel().attr(CLIENTID).get());
|
||||||
super.channelInactive(context);
|
super.channelInactive(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacke
|
|||||||
} else if (packet.getClass() == CS_Ping.class) {
|
} else if (packet.getClass() == CS_Ping.class) {
|
||||||
handleCSPing((CS_Ping) packet, context);
|
handleCSPing((CS_Ping) packet, context);
|
||||||
} else if (packet.getClass() == CS_ConsoleMessage.class) {
|
} else if (packet.getClass() == CS_ConsoleMessage.class) {
|
||||||
handleCSConsoleMessage((CS_ConsoleMessage) packet);
|
handleCSConsoleMessage(context.channel().attr(CLIENTID).get(), (CS_ConsoleMessage) packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,18 +78,18 @@ class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacke
|
|||||||
try {
|
try {
|
||||||
context.channel().writeAndFlush(HandshakeResult.INVALID_PASSCODE).sync().channel().close();
|
context.channel().writeAndFlush(HandshakeResult.INVALID_PASSCODE).sync().channel().close();
|
||||||
} catch (InterruptedException ignore) {
|
} catch (InterruptedException ignore) {
|
||||||
// ignore
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!manager.addClientId(packet.getClientId(), context.channel())) {
|
if (Manager.getInstance().existsServer(packet.getClientId())) {
|
||||||
try {
|
try {
|
||||||
context.channel().writeAndFlush(HandshakeResult.CLIENTID_EXISTS).sync().channel().close();
|
context.channel().writeAndFlush(HandshakeResult.CLIENTID_EXISTS).sync().channel().close();
|
||||||
} catch (InterruptedException ignore) {
|
} catch (InterruptedException ignore) {
|
||||||
// ignore
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
Manager.getInstance().addServer(packet.getClientId(), context.channel());
|
||||||
}
|
}
|
||||||
|
|
||||||
context.channel().write(HandshakeResult.OK);
|
context.channel().write(HandshakeResult.OK);
|
||||||
@@ -100,10 +100,10 @@ class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacke
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleCSPing(CS_Ping packet, ChannelHandlerContext context) {
|
private void handleCSPing(CS_Ping packet, ChannelHandlerContext context) {
|
||||||
manager.putInfo(context.channel().attr(CLIENTID).get(), packet);
|
Manager.getInstance().getServer(context.channel().attr(CLIENTID).get()).putPing(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleCSConsoleMessage(CS_ConsoleMessage packet) {
|
private void handleCSConsoleMessage(String serverName, CS_ConsoleMessage packet) {
|
||||||
manager.sendBroadcastToWebConsoleListeners(packet);
|
Manager.getInstance().broadcastConsoleMessage(serverName, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,36 +4,35 @@
|
|||||||
*/
|
*/
|
||||||
package asys.mcsmanager.websocket;
|
package asys.mcsmanager.websocket;
|
||||||
|
|
||||||
|
import asys.mcsmanager.Manager;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||||
|
import io.netty.util.AttributeKey;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static asys.mcsmanager.websocket.Server.manager;
|
|
||||||
|
|
||||||
public class FrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
public class FrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
||||||
|
private static final AttributeKey<String> WC_SERVERNAME = AttributeKey.valueOf("WC_SERVERNAME");
|
||||||
private final Logger logger = LoggerFactory.getLogger(FrameHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(FrameHandler.class);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
manager.addWebConsoleListener(ctx.channel());
|
|
||||||
super.channelActive(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
manager.removeWebConsoleListener(ctx.channel());
|
Manager.getInstance().removeWebconsoleListener(ctx.channel().attr(WC_SERVERNAME).get(), ctx.channel());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
|
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
|
||||||
if (frame instanceof TextWebSocketFrame) {
|
if (frame instanceof TextWebSocketFrame) {
|
||||||
String requestText = ((TextWebSocketFrame)frame).text();
|
String requestText = ((TextWebSocketFrame)frame).text();
|
||||||
if (requestText.startsWith(":")) {
|
if (requestText.startsWith("]")) {
|
||||||
//FIXME убрать костыли
|
String serverName = requestText.substring(1);
|
||||||
manager.sendCommand(null, requestText.substring(1));
|
ctx.channel().attr(WC_SERVERNAME).set(serverName);
|
||||||
|
Manager.getInstance().addWebconsoleListener(serverName, ctx.channel());
|
||||||
|
} else if (requestText.startsWith(":")) {
|
||||||
|
String command = requestText.substring(1);
|
||||||
|
Manager.getInstance().sendCommand(ctx.channel().attr(WC_SERVERNAME).get(), command);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.warn("unsupport frame type: {}", frame.getClass().getName());
|
logger.warn("unsupport frame type: {}", frame.getClass().getName());
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
package asys.mcsmanager.websocket;
|
package asys.mcsmanager.websocket;
|
||||||
|
|
||||||
import asys.mcsmanager.Manager;
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
@@ -16,11 +15,9 @@ import io.netty.handler.codec.http.HttpServerCodec;
|
|||||||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
static Manager manager;
|
|
||||||
private EventLoopGroup bossGroup, workerGroup;
|
private EventLoopGroup bossGroup, workerGroup;
|
||||||
|
|
||||||
public void start(String host, int port, Manager manager) {
|
public void start(String host, int port) {
|
||||||
Server.manager = manager;
|
|
||||||
bossGroup = new NioEventLoopGroup(1);
|
bossGroup = new NioEventLoopGroup(1);
|
||||||
workerGroup = new NioEventLoopGroup();
|
workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
|||||||
@@ -142,12 +142,15 @@ var ScrollingContent = React.createClass({
|
|||||||
|
|
||||||
var WebConsole = React.createClass({
|
var WebConsole = React.createClass({
|
||||||
ws: null,
|
ws: null,
|
||||||
connect: function(){
|
connect: function(serverName){
|
||||||
if (this.ws !== null) return;
|
if (this.ws !== null) return;
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
this.ws = new WebSocket("ws://127.0.0.1:8770"); //FIXME указывать ip:port из настроек
|
this.ws = new WebSocket("ws://127.0.0.1:8770"); //FIXME указывать ip:port из настроек
|
||||||
this.ws.onopen = function(){ console.debug('WS: open...'); };
|
this.ws.onopen = function(){
|
||||||
|
console.debug('WS: open...');
|
||||||
|
_this.ws.send(']'+serverName);
|
||||||
|
};
|
||||||
this.ws.onclose = function(){ console.debug('WS: close...'); };
|
this.ws.onclose = function(){ console.debug('WS: close...'); };
|
||||||
this.ws.onerror = function(e){ console.debug('WS: error'); console.error(e); };
|
this.ws.onerror = function(e){ console.debug('WS: error'); console.error(e); };
|
||||||
this.ws.onmessage = function(event){
|
this.ws.onmessage = function(event){
|
||||||
@@ -195,7 +198,7 @@ var WebConsole = React.createClass({
|
|||||||
var ServerInfo = React.createClass({
|
var ServerInfo = React.createClass({
|
||||||
tabStateWebConsole: function(state) {
|
tabStateWebConsole: function(state) {
|
||||||
if (state === 1) {
|
if (state === 1) {
|
||||||
this.refs.webconsole.connect();
|
this.refs.webconsole.connect(this.state.title);
|
||||||
this.refs.webconsole.focusInput();
|
this.refs.webconsole.focusInput();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -211,7 +214,7 @@ var ServerInfo = React.createClass({
|
|||||||
return(
|
return(
|
||||||
ce('div', null,
|
ce('div', null,
|
||||||
ce('h2', {style: {'margin-top': '0px'}}, this.state.title),
|
ce('h2', {style: {'margin-top': '0px'}}, this.state.title),
|
||||||
ce(Tabs, {tabs: ['Онлайн', 'Консоль'], stateCallback: this.tabStateWebConsole},
|
ce(Tabs, {tabs: ['Онлайн', 'Консоль'], stateCallback: this.tabStateWebConsole, ref: 'tabs'},
|
||||||
ce(NvLineChart, {datum: [{
|
ce(NvLineChart, {datum: [{
|
||||||
key: 'Online players',
|
key: 'Online players',
|
||||||
color: '#37d668',
|
color: '#37d668',
|
||||||
@@ -223,6 +226,13 @@ var ServerInfo = React.createClass({
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
componentWillUpdate: function(nextProps, nextState) {
|
||||||
|
if (this.state.title === null) return;
|
||||||
|
if (this.state.title !== nextState.title) {
|
||||||
|
this.refs.webconsole.disconnect();
|
||||||
|
this.refs.tabs.setState({activeTab: 0});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user