package com.yfd.platform.component.iec104.server.master;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.util.HashMap;
import java.util.Map;

public class EventLoopGroupManager {
    private static final Map<String, EventLoopGroup> eventLoopGroups = new HashMap<>();

    public static EventLoopGroup createEventLoopGroup(String key) {
        EventLoopGroup group = new NioEventLoopGroup();
        eventLoopGroups.put(key, group);
        return group;
    }

    public static EventLoopGroup getEventLoopGroup(String key) {
        EventLoopGroup group = eventLoopGroups.get(key);
        return group;

    }

    public static void shutdownEventLoopGroup(String key) {
        EventLoopGroup group = eventLoopGroups.get(key);
        if (group != null) {
            group.shutdownGracefully();
            eventLoopGroups.remove(key);
        }
    }

    // 如果需要手动关闭所有的EventLoopGroup
    public static void shutdownAllEventLoopGroups() {
        for (EventLoopGroup group : eventLoopGroups.values()) {
            group.shutdownGracefully();
        }
        eventLoopGroups.clear();
    }
}

