package cd.casic.module.terminal.websocket;

import cd.casic.framework.websocket.core.listener.WebSocketMessageListener;
import cd.casic.module.terminal.host.transfer.ITransferHandler;
import cd.casic.module.terminal.host.transfer.TerminalTransferManager;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;


/**
 * sftp 传输消息处理器 接收二进制消息
 *
 * @author Yuru Pu
 * @version 1.0
 * @since 2025/7/4 16:28
 */
@Slf4j
@Component
public class SFTPWebSocketMessageListener implements WebSocketMessageListener<BinaryMessage>{

    @Resource
    private TerminalTransferManager terminalTransferManager;


    @Override
    public void onMessage(WebSocketSession session, BinaryMessage message) {
        handleBinaryMessage(session,message);
    }

    @Override
    public String getType() {
        return "SFTP-access-message-send";
    }


    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) {//3
        // 获取处理器
        ITransferHandler handler = terminalTransferManager.getHandler(session.getId());
        // 添加数据
        handler.handleMessage(message.getPayload().array());
    }

}
