package cd.casic.module.terminal.host.terminal.handler;

import cd.casic.module.terminal.host.terminal.enums.OutputTypeEnum;
import cd.casic.module.terminal.host.terminal.model.TerminalBasePayload;
import cd.casic.module.terminal.host.terminal.session.ITerminalSession;
import cn.orionsec.kit.lang.utils.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import java.util.Map;

/**
 * ping 处理器
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2023/12/29 15:32
 */
@Slf4j
@Component
public class TerminalPingHandler extends AbstractTerminalHandler<TerminalBasePayload> {

    @Override
    public void handle(WebSocketSession channel, TerminalBasePayload payload) {
        // 发送 pong
        this.send(channel, OutputTypeEnum.PONG.getType());
        // 活跃 terminal
        Map<String, ITerminalSession> sessions = terminalManager.getSession(channel.getId());
        if (!Maps.isEmpty(sessions)) {
            for (ITerminalSession session : sessions.values()) {
                session.keepAlive();
            }
        }
    }

}
