REC6000ProtocolFactory.java
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
}
}
}