Search in sources :

Example 1 with Message

use of org.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.

the class Server method onHeartBeat.

public void onHeartBeat(ChannelHandlerContext ctx, Message message) {
    String content = "1";
    try {
        content = new String(message.getData(), "utf-8");
    } catch (UnsupportedEncodingException e) {
        logger.error("心跳包无法解析");
    } catch (Exception e) {
        logger.error("心跳包异常");
    }
    if (content.equals("0")) {
        Message response = new Message();
        response.setSeq(message.getSeq());
        response.setResult(0);
        response.setType((short) 0x13);
        response.setData("1".getBytes());
        ByteBuf out = ctx.alloc().buffer();
        response.writeHeader(out);
        response.writeExtra(out);
        ctx.writeAndFlush(out);
    } else if (content.equals("1")) {
    }
}
Also used : Message(org.bcos.channel.handler.Message) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBuf(io.netty.buffer.ByteBuf) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with Message

use of org.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.

the class Server method broadcastTopic.

public void broadcastTopic(ChannelHandlerContext ctx) {
    try {
        Message message = new Message();
        message.setResult(0);
        // topic设置topic消息0x32
        message.setType((short) 0x32);
        message.setSeq(UUID.randomUUID().toString().replaceAll("-", ""));
        // 综合所有的topics
        Set<String> allTopics = new HashSet<String>();
        for (ConnectionInfo connectionInfo : localConnections.getConnections()) {
            // 有效的连接,才增加到全局topic
            ChannelHandlerContext localCtx = localConnections.getNetworkConnectionByHost(connectionInfo.getHost(), connectionInfo.getPort());
            if (localCtx != null && localCtx.channel().isActive()) {
                logger.debug("节点:{}:{} 关注topics: {}", connectionInfo.getHost(), connectionInfo.getPort(), connectionInfo.getTopics());
                allTopics.addAll(connectionInfo.getTopics());
            }
        }
        message.setData(objectMapper.writeValueAsBytes(allTopics.toArray()));
        logger.debug("全部topics: {}", new String(message.getData()));
        if (ctx == null) {
            // 广播到所有远端节点
            for (String key : remoteConnections.getNetworkConnections().keySet()) {
                ChannelHandlerContext remoteCtx = remoteConnections.getNetworkConnections().get(key);
                if (remoteCtx != null && remoteCtx.channel().isActive()) {
                    ByteBuf out = remoteCtx.alloc().buffer();
                    message.writeHeader(out);
                    message.writeExtra(out);
                    if (remoteCtx != null && remoteCtx.channel().isActive()) {
                        logger.debug("topic广播至 {}:{}", ((SocketChannel) remoteCtx.channel()).remoteAddress().getAddress().getHostAddress(), ((SocketChannel) remoteCtx.channel()).remoteAddress().getPort());
                        remoteCtx.writeAndFlush(out);
                    }
                }
            }
        } else {
            // 发送到指定远端节点
            logger.debug("topic发送至 {}:{}", ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(), ((SocketChannel) ctx.channel()).remoteAddress().getPort());
            ByteBuf out = ctx.alloc().buffer();
            message.writeHeader(out);
            message.writeExtra(out);
            ctx.writeAndFlush(out);
        }
    } catch (Exception e) {
        logger.error("错误", e);
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Message(org.bcos.channel.handler.Message) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ConnectionInfo(org.bcos.channel.handler.ConnectionInfo) ByteBuf(io.netty.buffer.ByteBuf) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HashSet(java.util.HashSet)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Message (org.bcos.channel.handler.Message)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 HashSet (java.util.HashSet)1 ConnectionInfo (org.bcos.channel.handler.ConnectionInfo)1