Commit 5938f6b5904f2c0481b445230fe81e1afc35693b

Authored by 杜云山
1 parent 321b0c9e
Exists in develop

feat: 完成aero客户端发送消息

src/main/java/com/example/mina/Application.java
... ... @@ -8,8 +8,7 @@ import org.springframework.boot.SpringApplication;
8 8 import org.springframework.boot.autoconfigure.SpringBootApplication;
9 9 import org.springframework.context.ConfigurableApplicationContext;
10 10  
11   -import java.util.HashMap;
12   -import java.util.Map;
  11 +import java.util.concurrent.locks.LockSupport;
13 12  
14 13 /**
15 14 * @author 杜云山
... ... @@ -35,7 +34,13 @@ public class Application {
35 34 MatrixClient client = clientManager.getOrCreateClient(connectConfig);
36 35  
37 36  
38   - client.setAttenuation(1,2,3);
  37 + client.setAttenuation(1,2,3, MatrixCommand.Type.AERO_ATTN_ALL_MAX);
  38 + LockSupport.parkNanos(1000_000_000);
  39 +
  40 + client.setAttenuation(1,2,3, MatrixCommand.Type.AERO_SET_ATTN);
  41 + LockSupport.parkNanos(1000_000_000);
  42 +
  43 + client.setAttenuation(1,2,3, MatrixCommand.Type.AERO_GET_ATTN);
39 44  
40 45 }
41 46  
... ...
src/main/java/com/example/mina/client/base/MatrixClient.java
... ... @@ -121,9 +121,9 @@ public class MatrixClient {
121 121 sendCommand(command);
122 122 }
123 123  
124   - public void setAttenuation(int row, int col, int attn) {
  124 + public void setAttenuation(int row, int col, int attn, MatrixCommand.Type type) {
125 125 MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN)
126   - .col(col).row(row).attn(attn).build();
  126 + .col(col).row(row).attn(attn).type(type).build();
127 127  
128 128 sendCommand(command);
129 129 }
... ...
src/main/java/com/example/mina/client/base/MatrixCommand.java
... ... @@ -18,4 +18,27 @@ public class MatrixCommand {
18 18 private Integer attn;
19 19  
20 20 private Integer offset;
  21 +
  22 + private Type type;
  23 +
  24 + public enum Type {
  25 +
  26 + /**
  27 + * eg: "ATTN? 1"
  28 + */
  29 + AERO_GET_ATTN,
  30 +
  31 + /**
  32 + * ATTN 2 3;
  33 + */
  34 + AERO_SET_ATTN,
  35 +
  36 + /**
  37 + * eg: "ATTN ALL MAX"
  38 + */
  39 + AERO_ATTN_ALL_MAX,
  40 +
  41 + }
  42 +
21 43 }
  44 +
... ...
src/main/java/com/example/mina/client/box/aeroflex/AeroFlexProtocolFactory.java
... ... @@ -7,9 +7,18 @@ import org.apache.mina.core.buffer.IoBuffer;
7 7 import org.apache.mina.core.session.IoSession;
8 8 import org.apache.mina.filter.codec.*;
9 9  
  10 +import java.nio.charset.CharacterCodingException;
  11 +import java.nio.charset.CharsetDecoder;
  12 +import java.nio.charset.CharsetEncoder;
  13 +import java.nio.charset.StandardCharsets;
  14 +
10 15 @Slf4j
11 16 public class AeroFlexProtocolFactory implements ProtocolCodecFactory {
12 17  
  18 + private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder();
  19 +
  20 + private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder();
  21 +
13 22 @Override
14 23 public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
15 24 return new AeroFlexProtocolEncoder();
... ... @@ -21,40 +30,66 @@ public class AeroFlexProtocolFactory implements ProtocolCodecFactory {
21 30 }
22 31  
23 32 public static class AeroFlexProtocolEncoder extends ProtocolEncoderAdapter {
  33 +
24 34 @Override
25   - public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput) throws Exception {
  35 + public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
  36 + throws CharacterCodingException {
26 37  
27   - if(! (msg instanceof MatrixCommand) ) {
  38 + if (!(msg instanceof MatrixCommand)) {
28 39 log.error("error msg, msg is: {}", msg);
29 40 return;
30   - }else{
31   - MatrixCommand mc = (MatrixCommand) msg;
32   - log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {}", mc.getAttn(),mc.getCol(),mc.getCommand(),mc.getRow(),mc.getMatrixId());
33   - IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
34   - buffer.putInt(mc.getCommand());
35   - buffer.putInt(mc.getAttn());
36   - buffer.putInt(mc.getCol());
37   - buffer.putInt(mc.getRow());
38   -
39   - //buffer.put(lm.getContent().getBytes(charset));
40   -
41   - buffer.flip();
42   - protocolEncoderOutput.write(buffer);
43   - log.info("-=-=-=-=-=-=send finish==");
44 41 }
  42 +
  43 + MatrixCommand mc = (MatrixCommand) msg;
  44 + log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
  45 + mc.getAttn(),
  46 + mc.getCol(),
  47 + mc.getCommand(),
  48 + mc.getRow(),
  49 + mc.getMatrixId(),
  50 + mc.getType()
  51 + );
  52 + MatrixCommand.Type mcType = mc.getType();
  53 +
  54 + IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
  55 +
  56 + if (MatrixCommand.Type.AERO_ATTN_ALL_MAX.equals(mcType)) {
  57 +
  58 + buffer.putString("ATTN ALL MAX", ENCODER);
  59 +
  60 + } else if (MatrixCommand.Type.AERO_GET_ATTN.equals(mcType)) {
  61 +
  62 + buffer.putString("ATTN?", ENCODER);
  63 + buffer.putString(" ", ENCODER);
  64 + buffer.putString(mc.getRow().toString(), ENCODER);
  65 +
  66 + } else if (MatrixCommand.Type.AERO_SET_ATTN.equals(mcType)) {
  67 +
  68 + buffer.putString("ATTN", ENCODER);
  69 + buffer.putString(" ", ENCODER);
  70 + buffer.putString(mc.getRow().toString(), ENCODER);
  71 + buffer.putString(" ", ENCODER);
  72 + buffer.putString(mc.getAttn().toString(), ENCODER);
  73 + buffer.putString(";", ENCODER);
  74 + }
  75 +
  76 + buffer.flip();
  77 + protocolEncoderOutput.write(buffer);
45 78 }
46 79 }
47 80  
48   -
49 81 public static class AeroFlexProtocolDecoder extends ProtocolDecoderAdapter {
50 82  
51 83 @Override
52   - public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
53   - log.info("--server返回给----client ------IoBuffer{}" , ioBuffer);
  84 + public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
  85 + throws Exception {
  86 + log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
54 87 int cmd = ioBuffer.get();
55 88 MatrixResponse response = new MatrixResponse(); // todo
56 89  
57 90 protocolDecoderOutput.write(ioBuffer);
58 91 }
  92 +
59 93 }
  94 +
60 95 }
... ...