Commit d031a58b8d8e019700170bfd2cd63fd96bcd636a

Authored by 杜云山
1 parent f364f7ae
Exists in develop

feat: 增加解码编码器, 增加message

src/main/java/com/example/mina/base/AbstractVirtualBoxHandler.java
... ... @@ -6,7 +6,8 @@ import org.apache.mina.core.service.IoHandlerAdapter;
6 6 * @author 杜云山
7 7 * @date 21/03/05
8 8 */
9   -public abstract class AbstractVirtualBoxHandler extends IoHandlerAdapter {
  9 +public abstract class AbstractVirtualBoxHandler<REQUEST extends BaseRequestMessage, RESPONSE extends BaseResponseMessage>
  10 + extends IoHandlerAdapter {
10 11  
11 12 /**
12 13 * 初始化矩阵以及该设备的一些参数
... ... @@ -16,10 +17,9 @@ public abstract class AbstractVirtualBoxHandler extends IoHandlerAdapter {
16 17 /**
17 18 * 处理消息
18 19 *
19   - * @param cmd 指令数据
20   - * @param len 数据长度
  20 + * @param requestMessage 请求
21 21 * @return 返回消息
22 22 */
23   - protected abstract byte[] handleMessage(byte[] cmd, int len);
  23 + protected abstract RESPONSE handleMessage(REQUEST requestMessage);
24 24  
25 25 }
... ...
src/main/java/com/example/mina/base/AbstractVirtualBoxServer.java 0 → 100644
... ... @@ -0,0 +1,62 @@
  1 +package com.example.mina.base;
  2 +
  3 +import com.example.mina.property.AbstractVirtualBoxProperties;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.apache.mina.filter.codec.ProtocolCodecFactory;
  6 +import org.apache.mina.filter.codec.ProtocolCodecFilter;
  7 +import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
  8 +import org.springframework.boot.ApplicationArguments;
  9 +import org.springframework.boot.ApplicationRunner;
  10 +
  11 +import java.net.InetSocketAddress;
  12 +
  13 +/**
  14 + * @author 杜云山
  15 + * @date 21/03/05
  16 + */
  17 +@Slf4j
  18 +public class AbstractVirtualBoxServer implements ApplicationRunner {
  19 +
  20 + private final AbstractVirtualBoxProperties abstractVirtualBoxProperties;
  21 +
  22 + private final AbstractVirtualBoxHandler abstractVirtualBoxHandler;
  23 +
  24 + private final ProtocolCodecFactory protocolCodecFactory;
  25 +
  26 + public AbstractVirtualBoxServer(AbstractVirtualBoxProperties abstractVirtualBoxProperties,
  27 + AbstractVirtualBoxHandler abstractVirtualBoxHandler,
  28 + ProtocolCodecFactory protocolCodecFactory) {
  29 + this.abstractVirtualBoxProperties = abstractVirtualBoxProperties;
  30 + this.abstractVirtualBoxHandler = abstractVirtualBoxHandler;
  31 + this.protocolCodecFactory = protocolCodecFactory;
  32 + }
  33 +
  34 + @Override
  35 + public void run(ApplicationArguments args) {
  36 +
  37 + String name = abstractVirtualBoxProperties.getName();
  38 + Integer port = abstractVirtualBoxProperties.getPort();
  39 +
  40 + if (!abstractVirtualBoxProperties.getEnable()) {
  41 + log.info("{}服务端 配置未开启", name);
  42 + return;
  43 + }
  44 +
  45 + if (port == null) {
  46 + log.info("{}服务端 端口未配置", name);
  47 + return;
  48 + }
  49 +
  50 + try {
  51 + NioSocketAcceptor acceptor = new NioSocketAcceptor();
  52 + acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(protocolCodecFactory));
  53 + acceptor.setHandler(abstractVirtualBoxHandler);
  54 + acceptor.setReuseAddress(true);
  55 + acceptor.bind(new InetSocketAddress(port));
  56 + log.info("{}服务端已经启动,监听端口: {}", name, port);
  57 + } catch (Exception e) {
  58 + log.error("无法启动{}服务端, {}", name, e.getMessage(), e);
  59 + }
  60 + }
  61 +
  62 +}
... ...
src/main/java/com/example/mina/base/BaseRequestMessage.java 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +package com.example.mina.base;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +import lombok.experimental.SuperBuilder;
  7 +
  8 +/**
  9 + * @author 杜云山
  10 + * @date 21/03/08
  11 + */
  12 +@SuperBuilder
  13 +@Data
  14 +@AllArgsConstructor
  15 +@NoArgsConstructor
  16 +public abstract class BaseRequestMessage {
  17 +
  18 + protected String cmd;
  19 +
  20 + protected int in;
  21 +
  22 + protected int out;
  23 +
  24 +}
... ...
src/main/java/com/example/mina/base/BaseResponseMessage.java 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +package com.example.mina.base;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +import lombok.experimental.SuperBuilder;
  7 +
  8 +/**
  9 + * @author 杜云山
  10 + * @date 21/03/08
  11 + */
  12 +@SuperBuilder
  13 +@Data
  14 +@AllArgsConstructor
  15 +@NoArgsConstructor
  16 +public abstract class BaseResponseMessage {
  17 +
  18 + protected byte[] result;
  19 +
  20 + protected int in;
  21 +
  22 + protected int out;
  23 +
  24 +}
... ...
src/main/java/com/example/mina/base/CommandHelper.java
... ... @@ -1,33 +0,0 @@
1   -package com.example.mina.base;
2   -
3   -public class CommandHelper {
4   - public static final byte[] BCD = new byte[]{
5   - 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
6   - 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
7   - 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,
8   - 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
9   - 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
10   - 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
11   - 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
12   - 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
13   - (byte)0x80,(byte)0x81,(byte)0x82,(byte)0x83,(byte)0x84,(byte)0x85,(byte)0x86,(byte)0x87,(byte)0x88,(byte)0x89,
14   - (byte)0x90,(byte)0x91,(byte)0x92,(byte)0x93,(byte)0x94,(byte)0x95,(byte)0x96,(byte)0x97,(byte)0x98,(byte)0x99
15   - };
16   -
17   - public static final int[] BCD_REV = new int[]{
18   - 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
19   - 10,11,12,13,14,15,16,17,18,19,-1,-1,-1,-1,-1,-1,
20   - 20,21,22,23,24,25,26,27,28,29,-1,-1,-1,-1,-1,-1,
21   - 30,31,32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,
22   - 40,41,42,43,44,45,46,47,48,49,-1,-1,-1,-1,-1,-1,
23   - 50,51,52,53,54,55,56,57,58,59,-1,-1,-1,-1,-1,-1,
24   - 60,61,62,63,64,65,66,67,68,69,-1,-1,-1,-1,-1,-1,
25   - 70,71,72,73,74,75,76,77,78,79,-1,-1,-1,-1,-1,-1,
26   - 80,81,82,83,84,85,86,87,88,89,-1,-1,-1,-1,-1,-1,
27   - 90,91,92,93,94,95,96,97,98,99,-1,-1,-1,-1,-1,-1,
28   - };
29   -
30   -
31   -
32   -
33   -}
src/main/java/com/example/mina/box/aeroflex/AeroflexCodecFactory.java 0 → 100644
... ... @@ -0,0 +1,68 @@
  1 +package com.example.mina.box.aeroflex;
  2 +
  3 +import org.apache.mina.core.buffer.IoBuffer;
  4 +import org.apache.mina.core.session.IoSession;
  5 +import org.apache.mina.filter.codec.*;
  6 +
  7 +/**
  8 + * @author 杜云山
  9 + * @date 20/07/07
  10 + */
  11 +public class AeroflexCodecFactory implements ProtocolCodecFactory {
  12 +
  13 + @Override
  14 + public ProtocolEncoder getEncoder(IoSession session) {
  15 + return new AeroflexMessageEncoder();
  16 + }
  17 +
  18 + @Override
  19 + public ProtocolDecoder getDecoder(IoSession session) {
  20 + return new AeroflexMessageDecoder();
  21 + }
  22 +
  23 + static class AeroflexMessageEncoder implements ProtocolEncoder {
  24 +
  25 + @Override
  26 + public void encode(IoSession session, Object message, ProtocolEncoderOutput out) {
  27 +
  28 + if (message instanceof AeroflexResponseMessage) {
  29 +
  30 + byte[] result = ((AeroflexResponseMessage) message).getResult();
  31 + session.write(IoBuffer.wrap(result));
  32 + }
  33 + }
  34 +
  35 + @Override
  36 + public void dispose(IoSession session) {
  37 + }
  38 +
  39 + }
  40 +
  41 + static class AeroflexMessageDecoder implements ProtocolDecoder {
  42 +
  43 + @Override
  44 + public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
  45 +
  46 + byte[] bytes = new byte[in.limit()];
  47 + in.get(bytes);
  48 +
  49 + AeroflexRequestMessage requestMessage = AeroflexRequestMessage.builder()
  50 + .cmd(new String(bytes))
  51 + .build();
  52 +
  53 + out.write(requestMessage);
  54 + }
  55 +
  56 + @Override
  57 + public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
  58 +
  59 + }
  60 +
  61 + @Override
  62 + public void dispose(IoSession session) throws Exception {
  63 +
  64 + }
  65 +
  66 + }
  67 +
  68 +}
... ...
src/main/java/com/example/mina/box/aeroflex/AeroflexRequestMessage.java 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +package com.example.mina.box.aeroflex;
  2 +
  3 +import com.example.mina.base.BaseRequestMessage;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +import lombok.experimental.SuperBuilder;
  8 +
  9 +/**
  10 + * @author 杜云山
  11 + * @date 21/03/08
  12 + */
  13 +@EqualsAndHashCode(callSuper = true)
  14 +@SuperBuilder
  15 +@Data
  16 +@AllArgsConstructor
  17 +public class AeroflexRequestMessage extends BaseRequestMessage {
  18 +
  19 +}
... ...
src/main/java/com/example/mina/box/aeroflex/AeroflexResponseMessage.java 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +package com.example.mina.box.aeroflex;
  2 +
  3 +import com.example.mina.base.BaseResponseMessage;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +import lombok.experimental.SuperBuilder;
  8 +
  9 +/**
  10 + * @author 杜云山
  11 + * @date 21/03/08
  12 + */
  13 +@EqualsAndHashCode(callSuper = true)
  14 +@SuperBuilder
  15 +@Data
  16 +@AllArgsConstructor
  17 +public class AeroflexResponseMessage extends BaseResponseMessage {
  18 +
  19 +}
... ...
src/main/java/com/example/mina/box/aeroflex/AeroflexVirtualBoxHandler.java 0 → 100644
... ... @@ -0,0 +1,137 @@
  1 +package com.example.mina.box.aeroflex;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxHandler;
  4 +import com.example.mina.entity.AeroflexDataBuffer;
  5 +import com.example.mina.util.StrUtil;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +import org.apache.mina.core.session.IoSession;
  8 +
  9 +/**
  10 + * @author 杜云山
  11 + * @date 21/03/05
  12 + */
  13 +@Slf4j
  14 +public class AeroflexVirtualBoxHandler
  15 + extends AbstractVirtualBoxHandler<AeroflexRequestMessage, AeroflexResponseMessage> {
  16 +
  17 + private static final byte[] ERROR = "ERROR".getBytes();
  18 +
  19 + private static final byte[] NONE = "OK".getBytes();
  20 +
  21 + private static final String SET_ALL = "ATTN ALL MAX";
  22 +
  23 + private static final String SET_ONE = "ATTN";
  24 +
  25 + private static final String GET_ONE = "ATTN?";
  26 +
  27 + private static final String SPACE_SPLIT = " ";
  28 +
  29 + private static final String SEMICOLON_SPLIT = ";";
  30 +
  31 + private AeroflexDataBuffer dataBuffer;
  32 +
  33 + public AeroflexVirtualBoxHandler() {
  34 + this.initMatrix();
  35 + }
  36 +
  37 + @Override
  38 + protected void initMatrix() {
  39 +
  40 + int row = 10;
  41 + int col = 1;
  42 + int maxAttenuate = 888;
  43 +
  44 + dataBuffer = new AeroflexDataBuffer(row, maxAttenuate);
  45 + }
  46 +
  47 + @Override
  48 + public void messageReceived(IoSession session, Object message) {
  49 +
  50 + if (message instanceof AeroflexRequestMessage) {
  51 +
  52 + AeroflexResponseMessage responseMessage = handleMessage((AeroflexRequestMessage) message);
  53 + session.write(responseMessage);
  54 + }
  55 + }
  56 +
  57 + @Override
  58 + protected AeroflexResponseMessage handleMessage(AeroflexRequestMessage message) {
  59 + String command = message.getCmd().trim();
  60 +
  61 + log.info("aeroflexVirtualBoxHandler receive: {}", command);
  62 +
  63 + if (command.startsWith(SET_ALL)) {
  64 + //set all to max
  65 + for (int i = 1; i < dataBuffer.getMaxRow(); i++) {
  66 + dataBuffer.setOffset(i, dataBuffer.getMaxAttenuate());
  67 + }
  68 +
  69 + return AeroflexResponseMessage.builder().result(NONE).build();
  70 + } else if (command.startsWith(GET_ONE)) {
  71 + //get
  72 + String[] sss = command.split(SPACE_SPLIT);
  73 + if (sss.length >= 2) {
  74 +
  75 + int row = StrUtil.toInt(sss[1]);
  76 + if (row >= 0 && row <= dataBuffer.getMaxRow()) {
  77 + String str = String.valueOf(dataBuffer.getOffset(row));
  78 + log.info("aeroflexVirtualBoxHandler return: {}", str);
  79 + return AeroflexResponseMessage.builder().result(str.getBytes()).build();
  80 + }
  81 + }
  82 + return AeroflexResponseMessage.builder().result(ERROR).build();
  83 +
  84 + } else if (command.startsWith(SET_ONE)) {
  85 + //Set, Follow by ATTN?
  86 + String[] aa = command.split(SEMICOLON_SPLIT);
  87 +
  88 + String[] sss = aa[0].split(SPACE_SPLIT);
  89 +
  90 + if (sss.length >= 3) {
  91 +
  92 + int row = StrUtil.toInt(sss[1]);
  93 + int val = StrUtil.toInt(sss[2]);
  94 +
  95 + if (row >= 0 && row <= dataBuffer.getMaxRow()) {
  96 + if (val >= 0 && val <= dataBuffer.getMaxAttenuate()) {
  97 + dataBuffer.setOffset(row, val);
  98 +
  99 + String str = String.valueOf(dataBuffer.getOffset(row));
  100 + log.info("aeroflexVirtualBoxHandler return =====> {}", str);
  101 + return AeroflexResponseMessage.builder().result(str.getBytes()).build();
  102 + }
  103 + }
  104 + }
  105 + return AeroflexResponseMessage.builder().result(ERROR).build();
  106 +
  107 + } else {
  108 + return AeroflexResponseMessage.builder().result(ERROR).build();
  109 + }
  110 +
  111 + }
  112 +
  113 + @Override
  114 + public void sessionCreated(IoSession session) {
  115 +
  116 + log.info("--- abstractVirtual server session created");
  117 + }
  118 +
  119 + @Override
  120 + public void sessionOpened(IoSession session) {
  121 +
  122 + log.info("--- abstractVirtual server session Opened");
  123 + }
  124 +
  125 + @Override
  126 + public void sessionClosed(IoSession session) {
  127 +
  128 + log.info("--- abstractVirtual server session Closed");
  129 + }
  130 +
  131 + @Override
  132 + public void messageSent(IoSession session, Object message) {
  133 +
  134 + log.info("--- abstractVirtual 发送数据成功!{}", message);
  135 + }
  136 +
  137 +}
... ...
src/main/java/com/example/mina/box/aeroflex/AeroflexVirtualBoxServer.java 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +package com.example.mina.box.aeroflex;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxServer;
  4 +import com.example.mina.property.AeroflexVirtualBoxProperties;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.springframework.context.annotation.Configuration;
  7 +
  8 +/**
  9 + * @author 杜云山
  10 + * @date 21/03/05
  11 + */
  12 +@Slf4j
  13 +@Configuration(proxyBeanMethods = false)
  14 +public class AeroflexVirtualBoxServer extends AbstractVirtualBoxServer {
  15 +
  16 + public AeroflexVirtualBoxServer(AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties) {
  17 + super(aeroflexVirtualBoxProperties, new AeroflexVirtualBoxHandler(), new AeroflexCodecFactory());
  18 + }
  19 +
  20 +}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000CodecFactory.java 0 → 100644
... ... @@ -0,0 +1,62 @@
  1 +package com.example.mina.box.rbm3000;
  2 +
  3 +import org.apache.mina.core.buffer.IoBuffer;
  4 +import org.apache.mina.core.session.IoSession;
  5 +import org.apache.mina.filter.codec.*;
  6 +
  7 +/**
  8 + * @author 杜云山
  9 + * @date 20/07/07
  10 + */
  11 +public class Rbm3000CodecFactory implements ProtocolCodecFactory {
  12 +
  13 + @Override
  14 + public ProtocolEncoder getEncoder(IoSession session) {
  15 + return new AeroflexMessageEncoder();
  16 + }
  17 +
  18 + @Override
  19 + public ProtocolDecoder getDecoder(IoSession session) {
  20 + return new AeroflexMessageDecoder();
  21 + }
  22 +
  23 + static class AeroflexMessageEncoder implements ProtocolEncoder {
  24 +
  25 + @Override
  26 + public void encode(IoSession session, Object message, ProtocolEncoderOutput out) {
  27 + if (message instanceof IoBuffer) {
  28 + session.write(message);
  29 + }
  30 + }
  31 +
  32 + @Override
  33 + public void dispose(IoSession session) {
  34 + }
  35 +
  36 + }
  37 +
  38 + static class AeroflexMessageDecoder implements ProtocolDecoder {
  39 +
  40 +// private final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
  41 +
  42 + @Override
  43 + public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
  44 +
  45 +// String string = in.getString(decoder);
  46 +
  47 +// out.write(string);
  48 + }
  49 +
  50 + @Override
  51 + public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
  52 +
  53 + }
  54 +
  55 + @Override
  56 + public void dispose(IoSession session) throws Exception {
  57 +
  58 + }
  59 +
  60 + }
  61 +
  62 +}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000VirtualBoxHander.java 0 → 100644
... ... @@ -0,0 +1,176 @@
  1 +//package com.example.mina.box.rbm3000;
  2 +//
  3 +//import com.example.mina.base.AbstractVirtualBoxHandler;
  4 +//import com.example.mina.util.CommandHelper;
  5 +//import com.example.mina.entity.AeroflexDataBuffer;
  6 +//import com.example.mina.entity.Entry;
  7 +//import lombok.extern.slf4j.Slf4j;
  8 +//import org.apache.mina.core.buffer.IoBuffer;
  9 +//import org.apache.mina.core.session.IoSession;
  10 +//
  11 +///**
  12 +// * @author dy
  13 +// * @date 2021/3/5
  14 +// */
  15 +//@Slf4j
  16 +//public class Rbm3000VirtualBoxHander extends AbstractVirtualBoxHandler {
  17 +//
  18 +// private AeroflexDataBuffer dataBuffer;
  19 +//
  20 +// public Rbm3000VirtualBoxHander() {
  21 +// this.initMatrix();
  22 +// }
  23 +//
  24 +// @Override
  25 +// protected void initMatrix() {
  26 +//
  27 +// int row = 6;
  28 +// int col = 8;
  29 +// int maxAtten = 222;
  30 +//
  31 +// dataBuffer = new AeroflexDataBuffer(row, maxAtten);
  32 +// }
  33 +//
  34 +// @Override
  35 +// protected byte[] handleMessage(byte[] cmd, int len) {
  36 +// return new byte[0];
  37 +// }
  38 +//
  39 +// @Override
  40 +// public void messageReceived(IoSession session, Object message) {
  41 +//
  42 +// IoBuffer ioBuffer = (IoBuffer) message;
  43 +// byte[] bytes = new byte[ioBuffer.limit()];
  44 +// ioBuffer.get(bytes, ioBuffer.position(), ioBuffer.limit());
  45 +//
  46 +// byte[] result = handleCommand(bytes, bytes.length);
  47 +//
  48 +// session.write(IoBuffer.wrap(result));
  49 +// }
  50 +//
  51 +// @Override
  52 +// public void sessionCreated(IoSession session) {
  53 +//
  54 +// log.info("---server session created");
  55 +// }
  56 +//
  57 +// @Override
  58 +// public void sessionOpened(IoSession session) {
  59 +//
  60 +// log.info("---server session Opened");
  61 +// }
  62 +//
  63 +// @Override
  64 +// public void sessionClosed(IoSession session) {
  65 +//
  66 +// log.info("---server session Closed");
  67 +// }
  68 +//
  69 +// @Override
  70 +// public void messageSent(IoSession session, Object message) {
  71 +//
  72 +// log.info("---发送数据成功了。。。{}", message);
  73 +// }
  74 +//
  75 +// protected byte[] handleCommand(byte[] cmd, int len) {
  76 +//
  77 +// byte[] buffer;
  78 +// Entry[][] entries = dataBuffer.getMatrixData();
  79 +//
  80 +// if (len == 8) {
  81 +// switch (cmd[0]) {
  82 +// case 0x30:
  83 +// int row = CommandHelper.BCD_REV[cmd[3]];
  84 +// int col = CommandHelper.BCD_REV[cmd[4]];
  85 +//
  86 +// int rowCol = 0;
  87 +// int colRow = 0;
  88 +//
  89 +// for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  90 +// if (entries[row - 1][i].getValue() == 1) {
  91 +// rowCol = i + 1;
  92 +// break;
  93 +// }
  94 +// }
  95 +// for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  96 +// if (entries[i][col - 1].getValue() == 1) {
  97 +// colRow = i + 1;
  98 +// }
  99 +// }
  100 +//
  101 +// dataBuffer.setAttenuation(row, rowCol, 0);
  102 +// dataBuffer.setAttenuation(row, col, 1);
  103 +// dataBuffer.setAttenuation(colRow, col, 0);
  104 +// dataBuffer.setAttenuation(colRow, rowCol, 1);
  105 +//
  106 +// buffer = new byte[8];
  107 +// buffer[0] = cmd[0];
  108 +// buffer[1] = cmd[1];
  109 +// buffer[2] = cmd[2];
  110 +// buffer[3] = cmd[3];
  111 +// buffer[4] = cmd[4];
  112 +// buffer[5] = CommandHelper.BCD[colRow];
  113 +// buffer[6] = CommandHelper.BCD[rowCol];
  114 +// buffer[7] = (byte) 0x03;
  115 +//
  116 +// return buffer;
  117 +// case 0x68:
  118 +// boolean isA = (cmd[3] == (byte) 0x0A);
  119 +// int callPort = CommandHelper.BCD_REV[cmd[4]];
  120 +// int returnPort = 0;
  121 +//
  122 +// if (isA) {
  123 +// for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  124 +// if (entries[callPort - 1][i].getValue() == 1) {
  125 +// returnPort = i + 1;
  126 +// break;
  127 +// }
  128 +// }
  129 +// } else {
  130 +// for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  131 +// if (entries[i][callPort - 1].getValue() == 1) {
  132 +// returnPort = i + 1;
  133 +// }
  134 +// }
  135 +// }
  136 +//
  137 +// buffer = new byte[8];
  138 +// buffer[0] = cmd[0];
  139 +// buffer[1] = cmd[1];
  140 +// buffer[2] = cmd[2];
  141 +// if (isA) {
  142 +// buffer[3] = 0x0B;
  143 +// } else {
  144 +// buffer[3] = 0x0A;
  145 +// }
  146 +// buffer[4] = CommandHelper.BCD[returnPort];
  147 +// buffer[5] = 0x00;
  148 +// buffer[6] = 0x06;
  149 +// buffer[7] = (byte) 0x03;
  150 +//
  151 +// return buffer;
  152 +// case 0x62:
  153 +// buffer = new byte[8];
  154 +// buffer[0] = cmd[0];
  155 +// buffer[1] = cmd[1];
  156 +// buffer[2] = cmd[2];
  157 +// buffer[3] = cmd[3];
  158 +// buffer[4] = cmd[4];
  159 +// buffer[5] = cmd[5];
  160 +// buffer[6] = cmd[6];
  161 +// buffer[7] = (byte) 0x03;
  162 +//
  163 +// return buffer;
  164 +// default:
  165 +// break;
  166 +// }
  167 +// }
  168 +//
  169 +// buffer = new byte[len + 2];
  170 +// buffer[0] = (byte) 0xFE;
  171 +// buffer[1] = (byte) 0xFE;
  172 +// System.arraycopy(cmd, 0, buffer, 2, len);
  173 +//
  174 +// return buffer;
  175 +// }
  176 +//}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000VirtualBoxServer.java 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +//package com.example.mina.box.rbm3000;
  2 +//
  3 +//import com.example.mina.base.AbstractVirtualBoxServer;
  4 +//import com.example.mina.property.Rbm3000VitualBoxProperties;
  5 +//import lombok.extern.slf4j.Slf4j;
  6 +//import org.springframework.context.annotation.Configuration;
  7 +//
  8 +///**
  9 +// * @author 杜云山
  10 +// * @date 21/03/05
  11 +// */
  12 +//@Slf4j
  13 +//@Configuration(proxyBeanMethods = false)
  14 +//public class Rbm3000VirtualBoxServer extends AbstractVirtualBoxServer {
  15 +//
  16 +// public Rbm3000VirtualBoxServer(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
  17 +// super(rbm3000VitualBoxProperties, new Rbm3000VirtualBoxHander(), new Rbm3000CodecFactory());
  18 +// }
  19 +//
  20 +//}
... ...
src/main/java/com/example/mina/boxhandler/AeroflexVirtualBoxHandler.java
... ... @@ -1,139 +0,0 @@
1   -package com.example.mina.boxhandler;
2   -
3   -import com.example.mina.base.AbstractVirtualBoxHandler;
4   -import com.example.mina.entity.AeroflexDataBuffer;
5   -import com.example.mina.util.StrUtil;
6   -import lombok.extern.slf4j.Slf4j;
7   -import org.apache.mina.core.buffer.IoBuffer;
8   -import org.apache.mina.core.session.IoSession;
9   -
10   -/**
11   - * @author 杜云山
12   - * @date 21/03/05
13   - */
14   -@Slf4j
15   -public class AeroflexVirtualBoxHandler extends AbstractVirtualBoxHandler {
16   -
17   - private static final byte[] ERROR = "ERROR".getBytes();
18   -
19   - private static final byte[] NONE = "OK".getBytes();
20   -
21   - private static final String SET_ALL = "ATTN ALL MAX";
22   -
23   - private static final String SET_ONE = "ATTN";
24   -
25   - private static final String GET_ONE = "ATTN?";
26   -
27   - private static final String SPACE_SPLIT = " ";
28   -
29   - private static final String SEMICOLON_SPLIT = ";";
30   -
31   - private AeroflexDataBuffer dataBuffer;
32   -
33   - public AeroflexVirtualBoxHandler() {
34   - this.initMatrix();
35   - }
36   -
37   - @Override
38   - protected void initMatrix() {
39   -
40   - int row = 10;
41   - int col = 1;
42   - int maxAttenuate = 888;
43   -
44   - dataBuffer = new AeroflexDataBuffer(row, maxAttenuate);
45   - }
46   -
47   - @Override
48   - public void messageReceived(IoSession session, Object message) {
49   -
50   - IoBuffer ioBuffer = (IoBuffer) message;
51   - byte[] bytes = ioBuffer.array();
52   -
53   - byte[] result = handleMessage(bytes, bytes.length);
54   -
55   - session.write(IoBuffer.wrap(result));
56   - }
57   -
58   - @Override
59   - protected byte[] handleMessage(byte[] cmd, int len) {
60   - String command = new String(cmd).trim();
61   -
62   - log.info("aeroflexVirtualBoxHandler receive: {}", command);
63   -
64   - if (command.startsWith(SET_ALL)) {
65   - //set all to max
66   - for (int i = 1; i < dataBuffer.getMaxRow(); i++) {
67   - dataBuffer.setOffset(i, dataBuffer.getMaxAttenuate());
68   - }
69   -
70   - return NONE;
71   - } else if (command.startsWith(GET_ONE)) {
72   - //get
73   - String[] sss = command.split(SPACE_SPLIT);
74   - if (sss.length >= 2) {
75   -
76   - int row = StrUtil.toInt(sss[1]);
77   - if (row >= 0 && row <= dataBuffer.getMaxRow()) {
78   - String str = String.valueOf(dataBuffer.getOffset(row));
79   - log.info("aeroflexVirtualBoxHandler return: {}", str);
80   - return str.getBytes();
81   - }
82   - }
83   - return ERROR;
84   -
85   - } else if (command.startsWith(SET_ONE)) {
86   - //Set, Follow by ATTN?
87   - String[] aa = command.split(SEMICOLON_SPLIT);
88   -
89   - String[] sss = aa[0].split(SPACE_SPLIT);
90   -
91   - if (sss.length >= 3) {
92   -
93   - int row = StrUtil.toInt(sss[1]);
94   - int val = StrUtil.toInt(sss[2]);
95   -
96   - if (row >= 0 && row <= dataBuffer.getMaxRow()) {
97   - if (val >= 0 && val <= dataBuffer.getMaxAttenuate()) {
98   - dataBuffer.setOffset(row, val);
99   -
100   - String str = String.valueOf(dataBuffer.getOffset(row));
101   - log.info("aeroflexVirtualBoxHandler return =====> {}", str);
102   - return str.getBytes();
103   - }
104   - }
105   - }
106   -
107   - return ERROR;
108   -
109   - } else {
110   - return ERROR;
111   - }
112   -
113   - }
114   -
115   - @Override
116   - public void sessionCreated(IoSession session) {
117   -
118   - log.info("--- abstractVirtual server session created");
119   - }
120   -
121   - @Override
122   - public void sessionOpened(IoSession session) {
123   -
124   - log.info("--- abstractVirtual server session Opened");
125   - }
126   -
127   - @Override
128   - public void sessionClosed(IoSession session) {
129   -
130   - log.info("--- abstractVirtual server session Closed");
131   - }
132   -
133   - @Override
134   - public void messageSent(IoSession session, Object message) {
135   -
136   - log.info("--- abstractVirtual 发送数据成功!{}", message);
137   - }
138   -
139   -}
src/main/java/com/example/mina/boxhandler/Rbm3000VirtualBoxHander.java
... ... @@ -1,176 +0,0 @@
1   -package com.example.mina.boxhandler;
2   -
3   -import com.example.mina.base.AbstractVirtualBoxHandler;
4   -import com.example.mina.base.CommandHelper;
5   -import com.example.mina.entity.AeroflexDataBuffer;
6   -import com.example.mina.entity.Entry;
7   -import lombok.extern.slf4j.Slf4j;
8   -import org.apache.mina.core.buffer.IoBuffer;
9   -import org.apache.mina.core.session.IoSession;
10   -
11   -/**
12   - * @author dy
13   - * @date 2021/3/5
14   - */
15   -@Slf4j
16   -public class Rbm3000VirtualBoxHander extends AbstractVirtualBoxHandler {
17   -
18   - private AeroflexDataBuffer dataBuffer;
19   -
20   - public Rbm3000VirtualBoxHander() {
21   - this.initMatrix();
22   - }
23   -
24   - @Override
25   - protected void initMatrix() {
26   -
27   - int row = 6;
28   - int col = 8;
29   - int maxAtten = 222;
30   -
31   - dataBuffer = new AeroflexDataBuffer(row, maxAtten);
32   - }
33   -
34   - @Override
35   - protected byte[] handleMessage(byte[] cmd, int len) {
36   - return new byte[0];
37   - }
38   -
39   - @Override
40   - public void messageReceived(IoSession session, Object message) {
41   -
42   - IoBuffer ioBuffer = (IoBuffer) message;
43   - byte[] bytes = new byte[ioBuffer.limit()];
44   - ioBuffer.get(bytes, ioBuffer.position(), ioBuffer.limit());
45   -
46   - byte[] result = handleCommand(bytes, bytes.length);
47   -
48   - session.write(IoBuffer.wrap(result));
49   - }
50   -
51   - @Override
52   - public void sessionCreated(IoSession session) {
53   -
54   - log.info("---server session created");
55   - }
56   -
57   - @Override
58   - public void sessionOpened(IoSession session) {
59   -
60   - log.info("---server session Opened");
61   - }
62   -
63   - @Override
64   - public void sessionClosed(IoSession session) {
65   -
66   - log.info("---server session Closed");
67   - }
68   -
69   - @Override
70   - public void messageSent(IoSession session, Object message) {
71   -
72   - log.info("---发送数据成功了。。。{}", message);
73   - }
74   -
75   - protected byte[] handleCommand(byte[] cmd, int len) {
76   -
77   - byte[] buffer;
78   - Entry[][] entries = dataBuffer.getMatrixData();
79   -
80   - if (len == 8) {
81   - switch (cmd[0]) {
82   - case 0x30:
83   - int row = CommandHelper.BCD_REV[cmd[3]];
84   - int col = CommandHelper.BCD_REV[cmd[4]];
85   -
86   - int rowCol = 0;
87   - int colRow = 0;
88   -
89   - for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
90   - if (entries[row - 1][i].getValue() == 1) {
91   - rowCol = i + 1;
92   - break;
93   - }
94   - }
95   - for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
96   - if (entries[i][col - 1].getValue() == 1) {
97   - colRow = i + 1;
98   - }
99   - }
100   -
101   - dataBuffer.setAttenuation(row, rowCol, 0);
102   - dataBuffer.setAttenuation(row, col, 1);
103   - dataBuffer.setAttenuation(colRow, col, 0);
104   - dataBuffer.setAttenuation(colRow, rowCol, 1);
105   -
106   - buffer = new byte[8];
107   - buffer[0] = cmd[0];
108   - buffer[1] = cmd[1];
109   - buffer[2] = cmd[2];
110   - buffer[3] = cmd[3];
111   - buffer[4] = cmd[4];
112   - buffer[5] = CommandHelper.BCD[colRow];
113   - buffer[6] = CommandHelper.BCD[rowCol];
114   - buffer[7] = (byte) 0x03;
115   -
116   - return buffer;
117   - case 0x68:
118   - boolean isA = (cmd[3] == (byte) 0x0A);
119   - int callPort = CommandHelper.BCD_REV[cmd[4]];
120   - int returnPort = 0;
121   -
122   - if (isA) {
123   - for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
124   - if (entries[callPort - 1][i].getValue() == 1) {
125   - returnPort = i + 1;
126   - break;
127   - }
128   - }
129   - } else {
130   - for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
131   - if (entries[i][callPort - 1].getValue() == 1) {
132   - returnPort = i + 1;
133   - }
134   - }
135   - }
136   -
137   - buffer = new byte[8];
138   - buffer[0] = cmd[0];
139   - buffer[1] = cmd[1];
140   - buffer[2] = cmd[2];
141   - if (isA) {
142   - buffer[3] = 0x0B;
143   - } else {
144   - buffer[3] = 0x0A;
145   - }
146   - buffer[4] = CommandHelper.BCD[returnPort];
147   - buffer[5] = 0x00;
148   - buffer[6] = 0x06;
149   - buffer[7] = (byte) 0x03;
150   -
151   - return buffer;
152   - case 0x62:
153   - buffer = new byte[8];
154   - buffer[0] = cmd[0];
155   - buffer[1] = cmd[1];
156   - buffer[2] = cmd[2];
157   - buffer[3] = cmd[3];
158   - buffer[4] = cmd[4];
159   - buffer[5] = cmd[5];
160   - buffer[6] = cmd[6];
161   - buffer[7] = (byte) 0x03;
162   -
163   - return buffer;
164   - default:
165   - break;
166   - }
167   - }
168   -
169   - buffer = new byte[len + 2];
170   - buffer[0] = (byte) 0xFE;
171   - buffer[1] = (byte) 0xFE;
172   - System.arraycopy(cmd, 0, buffer, 2, len);
173   -
174   - return buffer;
175   - }
176   -}
src/main/java/com/example/mina/boxserver/AbstractVirtualBoxServer.java
... ... @@ -1,56 +0,0 @@
1   -package com.example.mina.boxserver;
2   -
3   -import com.example.mina.base.AbstractVirtualBoxHandler;
4   -import com.example.mina.property.AbstractVirtualBoxProperties;
5   -import lombok.extern.slf4j.Slf4j;
6   -import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
7   -import org.springframework.boot.ApplicationArguments;
8   -import org.springframework.boot.ApplicationRunner;
9   -
10   -import java.net.InetSocketAddress;
11   -
12   -/**
13   - * @author 杜云山
14   - * @date 21/03/05
15   - */
16   -@Slf4j
17   -public class AbstractVirtualBoxServer implements ApplicationRunner {
18   -
19   - private final AbstractVirtualBoxProperties abstractVirtualBoxProperties;
20   -
21   - private final AbstractVirtualBoxHandler abstractVirtualBoxHandler;
22   -
23   - public AbstractVirtualBoxServer(AbstractVirtualBoxProperties abstractVirtualBoxProperties,
24   - AbstractVirtualBoxHandler abstractVirtualBoxHandler) {
25   - this.abstractVirtualBoxProperties = abstractVirtualBoxProperties;
26   - this.abstractVirtualBoxHandler = abstractVirtualBoxHandler;
27   - }
28   -
29   - @Override
30   - public void run(ApplicationArguments args) {
31   -
32   - String name = abstractVirtualBoxProperties.getName();
33   - Integer port = abstractVirtualBoxProperties.getPort();
34   -
35   - if (!abstractVirtualBoxProperties.getEnable()) {
36   - log.info("{}服务端 配置未开启", name);
37   - return;
38   - }
39   -
40   - if (port == null) {
41   - log.info("{}服务端 端口未配置", name);
42   - return;
43   - }
44   -
45   - try {
46   - NioSocketAcceptor acceptor = new NioSocketAcceptor();
47   - acceptor.setHandler(abstractVirtualBoxHandler);
48   - acceptor.setReuseAddress(true);
49   - acceptor.bind(new InetSocketAddress(port));
50   - log.info("{}服务端已经启动,监听端口: {}", name, port);
51   - } catch (Exception e) {
52   - log.error("无法启动{}服务端, {}", name, e.getMessage(), e);
53   - }
54   - }
55   -
56   -}
src/main/java/com/example/mina/boxserver/AeroflexVirtualBoxServer.java
... ... @@ -1,20 +0,0 @@
1   -package com.example.mina.boxserver;
2   -
3   -import com.example.mina.boxhandler.AeroflexVirtualBoxHandler;
4   -import com.example.mina.property.AeroflexVirtualBoxProperties;
5   -import lombok.extern.slf4j.Slf4j;
6   -import org.springframework.context.annotation.Configuration;
7   -
8   -/**
9   - * @author 杜云山
10   - * @date 21/03/05
11   - */
12   -@Slf4j
13   -@Configuration(proxyBeanMethods = false)
14   -public class AeroflexVirtualBoxServer extends AbstractVirtualBoxServer {
15   -
16   - public AeroflexVirtualBoxServer(AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties) {
17   - super(aeroflexVirtualBoxProperties, new AeroflexVirtualBoxHandler());
18   - }
19   -
20   -}
src/main/java/com/example/mina/boxserver/Rbm3000VirtualBoxServer.java
... ... @@ -1,20 +0,0 @@
1   -package com.example.mina.boxserver;
2   -
3   -import com.example.mina.boxhandler.Rbm3000VirtualBoxHander;
4   -import com.example.mina.property.Rbm3000VitualBoxProperties;
5   -import lombok.extern.slf4j.Slf4j;
6   -import org.springframework.context.annotation.Configuration;
7   -
8   -/**
9   - * @author 杜云山
10   - * @date 21/03/05
11   - */
12   -@Slf4j
13   -@Configuration(proxyBeanMethods = false)
14   -public class Rbm3000VirtualBoxServer extends AbstractVirtualBoxServer {
15   -
16   - public Rbm3000VirtualBoxServer(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
17   - super(rbm3000VitualBoxProperties, new Rbm3000VirtualBoxHander());
18   - }
19   -
20   -}
src/main/java/com/example/mina/util/CommandHelper.java 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +package com.example.mina.util;
  2 +
  3 +public class CommandHelper {
  4 + public static final byte[] BCD = new byte[]{
  5 + 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
  6 + 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
  7 + 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,
  8 + 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
  9 + 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
  10 + 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
  11 + 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
  12 + 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
  13 + (byte)0x80,(byte)0x81,(byte)0x82,(byte)0x83,(byte)0x84,(byte)0x85,(byte)0x86,(byte)0x87,(byte)0x88,(byte)0x89,
  14 + (byte)0x90,(byte)0x91,(byte)0x92,(byte)0x93,(byte)0x94,(byte)0x95,(byte)0x96,(byte)0x97,(byte)0x98,(byte)0x99
  15 + };
  16 +
  17 + public static final int[] BCD_REV = new int[]{
  18 + 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
  19 + 10,11,12,13,14,15,16,17,18,19,-1,-1,-1,-1,-1,-1,
  20 + 20,21,22,23,24,25,26,27,28,29,-1,-1,-1,-1,-1,-1,
  21 + 30,31,32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,
  22 + 40,41,42,43,44,45,46,47,48,49,-1,-1,-1,-1,-1,-1,
  23 + 50,51,52,53,54,55,56,57,58,59,-1,-1,-1,-1,-1,-1,
  24 + 60,61,62,63,64,65,66,67,68,69,-1,-1,-1,-1,-1,-1,
  25 + 70,71,72,73,74,75,76,77,78,79,-1,-1,-1,-1,-1,-1,
  26 + 80,81,82,83,84,85,86,87,88,89,-1,-1,-1,-1,-1,-1,
  27 + 90,91,92,93,94,95,96,97,98,99,-1,-1,-1,-1,-1,-1,
  28 + };
  29 +
  30 +
  31 +
  32 +
  33 +}
... ...