REC6000ProtocolFactory.java 3.36 KB
package com.example.mina.client.box.rec6000;

import com.example.mina.client.base.MatrixCommand;
import com.example.mina.client.base.MatrixResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.*;
import org.apache.mina.http.api.DefaultHttpResponse;
import org.apache.mina.http.api.HttpResponse;
import org.apache.mina.http.api.HttpStatus;
import org.apache.mina.http.api.HttpVersion;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class REC6000ProtocolFactory implements ProtocolCodecFactory {

    @Override
    public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
        return new REC6000ProtocolEncoder();
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
        return new REC6000ProtocolDecoder();
    }

    public static class REC6000ProtocolEncoder extends ProtocolEncoderAdapter {

        @Override
        public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
                throws JsonProcessingException {

            if (!(msg instanceof MatrixCommand)) {
                log.error("error msg, msg is: {}", msg);
                return;
            }

            MatrixCommand mc = (MatrixCommand) msg;
            log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
                    mc.getAttn(),
                    mc.getCol(),
                    mc.getCommand(),
                    mc.getRow(),
                    mc.getMatrixId()
            );

            // REC6000 rest  开关
            //	设置 row/col的开关
            //	读取 row/col的开关

            // 响应HTML
            String responseJson = new ObjectMapper().writeValueAsString(mc);
            byte[] responseBytes = responseJson.getBytes(StandardCharsets.UTF_8);
            int contentLength = responseBytes.length;

            // 构造HttpResponse对象,HttpResponse只包含响应的status line和header部分
            Map<String, String> headers = new HashMap<>(8);
            headers.put("Content-Type", "application/json");
            headers.put("Content-Length", Integer.toString(contentLength));
            HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);

            // 响应BODY
            IoBuffer responseIoBuffer = IoBuffer.allocate(contentLength);
            responseIoBuffer.put(responseBytes);
            responseIoBuffer.flip();

            // 响应的status line和header部分
            ioSession.write(response);
            // 响应body部分
            ioSession.write(responseIoBuffer);

        }

    }

    public static class REC6000ProtocolDecoder extends ProtocolDecoderAdapter {

        @Override
        public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput) {
            log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
            int cmd = ioBuffer.get();
            MatrixResponse response = new MatrixResponse();  // todo

            protocolDecoderOutput.write(ioBuffer);
        }

    }

}