All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.godmao.mqbroker.handler.BindMessageHandler Maven / Gradle / Ivy

There is a newer version: 0.2.7.RELEASE
Show newest version
package com.godmao.mqbroker.handler;

import com.godmao.mqbroker.Broker;
import com.godmao.mqbroker.Static;
import com.godmao.mqbroker.message.BindMessage;
import com.godmao.mqbroker.message.TopicSyncMessage;
import com.godmao.netty.channel.ChannelService;
import com.godmao.netty.common.NetUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class BindMessageHandler extends SimpleChannelInboundHandler {
    public final  Logger log = LoggerFactory.getLogger(this.getClass());
    private final Broker connect;

    public BindMessageHandler(Broker connect) {
        this.connect = connect;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BindMessage bindMessage) {
        log.info("[绑定成功]-[{}-{}]: {}", bindMessage.getName(), ctx.channel().id().asShortText(), bindMessage.getAddress());
        Channel channel = ctx.channel();
        channel.attr(Static.AUTHORIZATION).set(true);
        connect.getChannelService().add(channel, bindMessage.getName());
        channel.attr(ChannelService.CHANNELADDRESS).set(bindMessage.getAddress());
        //
        ctx.writeAndFlush(new BindMessage(connect.getName(), NetUtil.LOCAL_IP + ":" + connect.getPort()));
        //
        final Map topicCount = connect.getTopicCount();
        ctx.writeAndFlush(new TopicSyncMessage(Static.TOPIC_VERSION.get(), true, topicCount));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy