package com.yonge.netty.common.message;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;

public class MessageDispatcher extends SimpleChannelInboundHandler<Message<?>> {

	@Autowired
	private MessageHandlerContainer messageHandlerContainer;

	private final ExecutorService executor = Executors.newFixedThreadPool(200);

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Message<?> message) {
		// 获得 type 对应的 MessageHandler 处理器
		MessageHandler<?> messageHandler = messageHandlerContainer.getMessageHandler(message.getType());
		// 获得 MessageHandler 处理器 的消息类
		// Class<? extends Message> messageClass = MessageHandlerContainer.getMessageClass(messageHandler);
		// 执行逻辑
		executor.submit(() -> messageHandler.execute(ctx.channel(), message.getData()));
	}
}
