Archived
0

MCSM: успешный вывод консоли сервера в браузере

This commit is contained in:
2017-05-18 15:17:05 +03:00
parent b1054aab89
commit 37b8f02b69
7 changed files with 33 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys'
version = '0.8.7-SNAPSHOT'
version = '0.8.8-SNAPSHOT'
apply plugin: 'osgi'

View File

@@ -50,7 +50,7 @@ public class Activator implements BundleActivator, ServiceListener {
port = config.getInt("asys.mcsmanager.webconsole.port", 8770);
logger.debug("Start webconsole server: {}:{}", host, port);
webconsoleServer = new asys.mcsmanager.websocket.Server();
webconsoleServer.start(host, port);
webconsoleServer.start(host, port, manager);
serviceConfigTracker.close();
}

View File

@@ -4,7 +4,10 @@
*/
package asys.mcsmanager;
import asys.mcsmanager.packets.CS_ConsoleMessage;
import asys.mcsmanager.packets.CS_Ping;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,6 +16,7 @@ import java.util.*;
public class Manager {
private Logger logger = LoggerFactory.getLogger(Manager.class);
private Map<String, ServerInfo> serversMap = new HashMap<>();
private List<Channel> webconsoleListener = new ArrayList<>();
/**
* Добавляем в список ClientID
@@ -47,4 +51,22 @@ public class Manager {
public ServerInfo getInfo(String clientId) {
return serversMap.get(clientId);
}
public void addWebConsoleListener(Channel channel) {
this.webconsoleListener.add(channel);
}
public void removeWebConsoleListener(Channel channel) {
this.webconsoleListener.remove(channel);
}
public void sendBroadcastToWebConsoleListeners(CS_ConsoleMessage message) {
for (Channel channel : this.webconsoleListener) {
channel.writeAndFlush(new TextWebSocketFrame(String.format(
"[L:%d] %s",
message.getLevel(),
message.getMessage()
)));
}
}
}

View File

@@ -11,7 +11,6 @@ import com.google.common.collect.ImmutableMap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import org.slf4j.LoggerFactory;
import java.util.Map;
@@ -98,6 +97,6 @@ class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacke
}
private void handleCSConsoleMessage(CS_ConsoleMessage packet) {
LoggerFactory.getLogger(getClass()).debug("[L:{}] {}", packet.getLevel(), packet.getMessage());
manager.sendBroadcastToWebConsoleListeners(packet);
}
}

View File

@@ -11,29 +11,20 @@ import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static asys.mcsmanager.websocket.Server.manager;
public class FrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
private final Logger logger = LoggerFactory.getLogger(FrameHandler.class);
private ScheduledFuture<?> sesFuture;
private int i = 1;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
sesFuture = ses.scheduleAtFixedRate(() -> ctx.channel().writeAndFlush(
new TextWebSocketFrame(String.format("<S:ping:%d>", i++))),
500L, 1000L, TimeUnit.MILLISECONDS);
manager.addWebConsoleListener(ctx.channel());
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
sesFuture.cancel(false);
manager.removeWebConsoleListener(ctx.channel());
}
@Override

View File

@@ -4,6 +4,7 @@
*/
package asys.mcsmanager.websocket;
import asys.mcsmanager.Manager;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
@@ -15,9 +16,11 @@ import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
public class Server {
static Manager manager;
private EventLoopGroup bossGroup, workerGroup;
public void start(String host, int port) {
public void start(String host, int port, Manager manager) {
Server.manager = manager;
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();

View File

@@ -81,7 +81,7 @@ var WebConsole = React.createClass({
this.ws.onclose = function(){ console.debug('WS: close...'); };
this.ws.onerror = function(e){ console.debug('WS: error'); console.error(e); };
this.ws.onmessage = function(event){
_this.setState({ lines: _this.state.lines.concat([event.data]) });
_this.setState({ lines: _this.state.lines.concat([event.data]) }); //TODO необходимо ограничить кол-во строк
};
},
disconnect: function() {