Commit 23416fa69f8f79ae220688001bb34f27d61b1232

Authored by wu
1 parent 8cb7be3c
Exists in develop

feat: refactor the client and the step iterations;

src/main/java/com/example/mina/client/base/AbstractClientFactory.java
... ... @@ -15,8 +15,10 @@ public abstract class AbstractClientFactory {
15 15  
16 16 abstract public AbstractMatrixIoHandler getClientHandler();
17 17  
18   - public void buildFilterChain(IoFilterChain ioFilterChain){
  18 + public void buildFilterChain(IoFilterChain ioFilterChain) throws Exception{
  19 +
19 20 ioFilterChain.addLast("codec", new ProtocolCodecFilter(new ByteProtocolFactory()));
  21 +
20 22 }
21 23  
22 24 public MatrixClient createClient(MatrixConnectConfig connectConfig){
... ... @@ -27,9 +29,10 @@ public abstract class AbstractClientFactory {
27 29 MatrixClient client = new MatrixClient(connectConfig);
28 30  
29 31 client.setClientHandler(getClientHandler());
30   - //TODO initiate the client
31   - buildFilterChain(client.getFilterChain());
  32 + client.setMatrixDataProxy(matrixDataProxy);
  33 + client.connect();
32 34  
  35 + buildFilterChain(client.getFilterChain());
33 36 return client;
34 37 }catch (Exception e) {
35 38 log.error("Can not create the client, error: ", e);
... ...
src/main/java/com/example/mina/client/base/AbstractMatrixIoHandler.java
... ... @@ -3,7 +3,6 @@ package com.example.mina.client.base;
3 3  
4 4 import lombok.Data;
5 5 import lombok.extern.slf4j.Slf4j;
6   -import org.apache.mina.core.buffer.IoBuffer;
7 6 import org.apache.mina.core.service.IoHandlerAdapter;
8 7 import org.apache.mina.core.session.IdleStatus;
9 8 import org.apache.mina.core.session.IoSession;
... ... @@ -15,12 +14,6 @@ import org.apache.mina.core.session.IoSession;
15 14 @Slf4j
16 15 public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter {
17 16  
18   - protected MatrixDataProxy matrixDataProxy;
19   -
20   - public AbstractMatrixIoHandler(MatrixDataProxy matrixDataProxy) {
21   - this.matrixDataProxy = matrixDataProxy;
22   - }
23   -
24 17 protected MatrixConnectConfig getConnectConfig(IoSession session) {
25 18 Object boxConnectConfig = session.getAttribute(MatrixConstants.SESSION_CONFIG_NAME);
26 19  
... ... @@ -30,6 +23,15 @@ public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter {
30 23 return null;
31 24 }
32 25  
  26 + protected MatrixDataProxy getMatrixDataProxy(IoSession session) {
  27 + Object matrixDataProxy = session.getAttribute(MatrixConstants.SESSION_DATA_PROXY_NAME);
  28 +
  29 + if( matrixDataProxy instanceof MatrixDataProxy) {
  30 + return (MatrixDataProxy) matrixDataProxy;
  31 + }
  32 + return null;
  33 + }
  34 +
33 35 @Override
34 36 public void exceptionCaught(IoSession session, Throwable throwable) {
35 37 }
... ...
src/main/java/com/example/mina/client/base/Experiment.java
... ... @@ -53,7 +53,7 @@ public abstract class Experiment <T extends MatrixClient, O extends ExperimentOp
53 53 }
54 54  
55 55 public boolean isContinue() {
56   - return iteratedTimes < options.getMaxIterCount() &&
  56 + return iteratedTimes < options.getIterations() &&
57 57 ZonedDateTime.now().isBefore(options.getFinishAt()) &&
58 58 isRunning;
59 59 }
... ...
src/main/java/com/example/mina/client/base/ExperimentOptions.java
... ... @@ -6,11 +6,14 @@ import lombok.Data;
6 6  
7 7 @Data
8 8 public abstract class ExperimentOptions {
  9 +
9 10 protected String experimentId;
10 11  
11   - protected Integer startAttenuation;
  12 + protected String experimentType;
  13 +
  14 + protected Integer startAttn;
12 15  
13   - protected Integer endAttenuation;
  16 + protected Integer endAttn;
14 17  
15 18 protected Integer step;
16 19  
... ... @@ -27,11 +30,17 @@ public abstract class ExperimentOptions {
27 30 /**
28 31 * 最大循环次数
29 32 */
30   - protected Integer maxIterCount;
  33 + protected Integer iterations;
31 34  
32 35 protected ZonedDateTime finishAt;
33 36  
34   - protected List<StationPair> pairs;
35 37  
36   - protected abstract boolean checkOptions();
  38 + protected boolean checkOptions(){
  39 + if(startAttn<0 || endAttn<0 || startAttn>endAttn || step<=0
  40 + || pause<0 || period<=0 || iterations<=0 || finishAt==null ||
  41 + finishAt.isBefore(ZonedDateTime.now())) {
  42 + return false;
  43 + }
  44 + return true;
  45 + }
37 46 }
... ...
src/main/java/com/example/mina/client/base/MatrixClient.java
1 1 package com.example.mina.client.base;
2 2  
3 3 import lombok.extern.slf4j.Slf4j;
4   -import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
5 4 import org.apache.mina.core.filterchain.IoFilterChain;
6 5 import org.apache.mina.core.future.ConnectFuture;
7 6 import org.apache.mina.core.future.WriteFuture;
8 7 import org.apache.mina.core.service.IoConnector;
9 8 import org.apache.mina.core.session.IoSession;
10   -import org.apache.mina.filter.codec.ProtocolCodecFilter;
11 9 import org.apache.mina.transport.socket.nio.NioSocketConnector;
12 10 import org.springframework.util.Assert;
13 11  
14 12 import java.net.InetSocketAddress;
15 13 import java.util.concurrent.LinkedBlockingQueue;
16   -import java.util.concurrent.TimeUnit;
17 14  
18 15 /**
19 16 * @author dy
... ... @@ -32,6 +29,11 @@ public class MatrixClient {
32 29 */
33 30 protected LinkedBlockingQueue<MatrixCommand> requestsPool;
34 31  
  32 + /**
  33 + * the data proxy
  34 + */
  35 + private MatrixDataProxy matrixDataProxy;
  36 +
35 37 protected AbstractMatrixIoHandler clientHandler;
36 38  
37 39 protected IoSession ioSession;
... ... @@ -42,8 +44,6 @@ public class MatrixClient {
42 44 this.matrixConnectConfig = connectConfig;
43 45 this.requestsPool = new LinkedBlockingQueue<>();
44 46 this.sender = new CommandSender(this, this.requestsPool);
45   -
46   - this.sender.start();
47 47 }
48 48  
49 49 /**
... ... @@ -75,16 +75,22 @@ public class MatrixClient {
75 75 this.clientHandler = clientHandler;
76 76 }
77 77  
  78 + /**
  79 + * get the io filter chain
  80 + */
  81 + public IoFilterChain getFilterChain() {
  82 + return ioSession.getFilterChain();
  83 + }
78 84  
79 85 /**
80   - * 返回该盒子的编解码器,默认为tcp的协议,如果是其它协议,可以在自类中重写改方法
  86 + * Set the data proxy
81 87 * @return
82 88 */
83   - protected void setFilterChain(DefaultIoFilterChainBuilder filterChainBuilder) {
84   - filterChainBuilder.addLast("codec", new ProtocolCodecFilter(new ByteProtocolFactory()));
  89 + public void setMatrixDataProxy(MatrixDataProxy matrixDataProxy) {
  90 + this.matrixDataProxy = matrixDataProxy;
85 91 }
86 92  
87   - public IoFilterChain getFilterChain() {
  93 + public void connect() {
88 94 //1、创建客户端IoService
89 95 IoConnector connector = new NioSocketConnector();
90 96 //客户端链接超时时间
... ... @@ -103,8 +109,8 @@ public class MatrixClient {
103 109 // 获取连接会话
104 110 this.ioSession = connectFuture.getSession();
105 111 this.ioSession.setAttribute(MatrixConstants.SESSION_CONFIG_NAME, matrixConnectConfig);
  112 + this.ioSession.setAttribute(MatrixConstants.SESSION_DATA_PROXY_NAME, matrixDataProxy);
106 113  
107   - return ioSession.getFilterChain();
108 114 }
109 115  
110 116 public void start() {
... ...
src/main/java/com/example/mina/client/base/MatrixConstants.java
... ... @@ -4,6 +4,8 @@ public class MatrixConstants {
4 4  
5 5 public static final String SESSION_CONFIG_NAME = "SESSION_CONFIG";
6 6  
  7 + public static final String SESSION_DATA_PROXY_NAME = "DATA_PROXY";
  8 +
7 9 public static final String MATRIX_TYPE_AERO = "AEROFLEX";
8 10  
9 11 public static final String MATRIX_TYPE_LTE3000 = "LTE3000";
... ...
src/main/java/com/example/mina/client/base/MatrixDataProxy.java
1 1 package com.example.mina.client.base;
2 2  
3   -public class MatrixDataProxy {
  3 +public interface MatrixDataProxy {
4 4  
5 5 /**
6 6 * 当从设备端将数据同步到客户端的时候,将数据直接存入数据中,
7 7 * 该类会在ioHandler及其子类中被调用
8 8 * 设置attenuation的值,并将其存储到matrix_data的数据表中
9 9 */
10   - public void setAttenuation(String deviceId, int row, int col, int val) {
11   - //TODO 实现将数据写入到数据库的逻辑
12   - }
  10 + void setAttenuation(String deviceId, int row, int col, int val);
13 11  
14   - public int getAttenuation(String deviceId, int row, int col) {
15   - return -1;
16   - }
  12 + int getAttenuation(String deviceId, int row, int col);
17 13  
18   - public void setOffset(int row, int val) {
  14 + void setOffset(int row, int val);
19 15  
20   - }
  16 + void resetAttenuation();
21 17  
22   - public int getOffset(int row) {
23   - return -1;
24   - }
  18 + int getOffset(int row);
25 19  
26   - public void resetAttenuation() {
27   -
28   - }
29   -
30   - private String getCurrentThreadId() {
31   - return Thread.currentThread().getName();
32   - }
33 20 }
... ...
src/main/java/com/example/mina/client/base/Mimo.java 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +package com.example.mina.client.base;
  2 +
  3 +public enum Mimo {
  4 +
  5 + NO,
  6 + M2X2,
  7 + M4X4
  8 +}
... ...
src/main/java/com/example/mina/client/base/SendMessage.java
... ... @@ -1,19 +0,0 @@
1   -package com.example.mina.client.base;
2   -
3   -import lombok.AllArgsConstructor;
4   -import lombok.Data;
5   -import lombok.NoArgsConstructor;
6   -import lombok.experimental.SuperBuilder;
7   -
8   -/**
9   - * @author dy
10   - * @date 2021/3/12
11   - */
12   -@SuperBuilder
13   -@Data
14   -@AllArgsConstructor
15   -@NoArgsConstructor
16   -public class SendMessage {
17   -
18   - private byte[] sendMessage;
19   -}
src/main/java/com/example/mina/client/base/StationPair.java
1 1 package com.example.mina.client.base;
2 2  
3 3 import java.util.List;
  4 +
  5 +import lombok.Builder;
4 6 import lombok.Data;
5 7  
6 8 /**
... ... @@ -9,6 +11,7 @@ import lombok.Data;
9 11 * @date 2021/4/2
10 12 */
11 13 @Data
  14 +@Builder
12 15 public class StationPair {
13 16 /**
14 17 * 输出端口
... ...
src/main/java/com/example/mina/client/box/aeroflex/AeroflexClientIoHandler.java
... ... @@ -13,14 +13,6 @@ import org.apache.mina.core.session.IoSession;
13 13 @Slf4j
14 14 public class AeroflexClientIoHandler extends AbstractMatrixIoHandler {
15 15  
16   - //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
17   - // super(matrixDataProxy);
18   - // }
19   -
20   - public AeroflexClientIoHandler() {
21   - super(new MatrixDataProxy());
22   - }
23   -
24 16  
25 17 @Override
26 18 public boolean handleCommandResponse(MatrixResponse matrixResponse) {
... ...
src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java
... ... @@ -14,10 +14,6 @@ import org.apache.mina.core.session.IoSession;
14 14 @Slf4j
15 15 public class Lte3000ClientIoHandler extends AbstractMatrixIoHandler {
16 16  
17   - public Lte3000ClientIoHandler() {
18   - super(new MatrixDataProxy());
19   - }
20   -
21 17 protected boolean isSameValue(byte value, int expect) {
22 18 return ((0xFF & value) == expect);
23 19 }
... ...
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ClientIoHandler.java
... ... @@ -13,15 +13,6 @@ import org.apache.mina.core.session.IoSession;
13 13 @Slf4j
14 14 public class Qrb3000ClientIoHandler extends AbstractMatrixIoHandler {
15 15  
16   - //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
17   - // super(matrixDataProxy);
18   - // }
19   -
20   - public Qrb3000ClientIoHandler() {
21   - super(new MatrixDataProxy());
22   - }
23   -
24   -
25 16 @Override
26 17 public boolean handleCommandResponse(MatrixResponse matrixResponse) {
27 18 log.info("------Qrb3000ClientIoHandler-----{}",matrixResponse);
... ...
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ClientIoHandler.java
... ... @@ -13,15 +13,6 @@ import org.apache.mina.core.session.IoSession;
13 13 @Slf4j
14 14 public class Rbm3000ClientIoHandler extends AbstractMatrixIoHandler {
15 15  
16   - //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
17   - // super(matrixDataProxy);
18   - // }
19   -
20   - public Rbm3000ClientIoHandler() {
21   - super(new MatrixDataProxy());
22   - }
23   -
24   -
25 16 @Override
26 17 public boolean handleCommandResponse(MatrixResponse matrixResponse) {
27 18 log.info("------Rbm3000ClientIoHandler-----{}",matrixResponse);
... ...
src/main/java/com/example/mina/client/box/rec6000/REC6000ClientIoHandler.java
... ... @@ -13,15 +13,6 @@ import org.apache.mina.core.session.IoSession;
13 13 @Slf4j
14 14 public class REC6000ClientIoHandler extends AbstractMatrixIoHandler {
15 15  
16   - //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
17   - // super(matrixDataProxy);
18   - // }
19   -
20   - public REC6000ClientIoHandler() {
21   - super(new MatrixDataProxy());
22   - }
23   -
24   -
25 16 @Override
26 17 public boolean handleCommandResponse(MatrixResponse matrixResponse) {
27 18 log.info("------REC3000ClientIoHandler-----{}",matrixResponse);
... ...
src/main/java/com/example/mina/client/experiment/HandOverExperimentOptions.java
1 1 package com.example.mina.client.experiment;
2 2  
3 3 import com.example.mina.client.base.ExperimentOptions;
  4 +import com.example.mina.client.base.Mimo;
  5 +import com.example.mina.client.base.StationPair;
4 6 import lombok.Data;
5 7 import lombok.EqualsAndHashCode;
6 8 import lombok.ToString;
7 9  
  10 +import java.util.List;
  11 +
8 12 @Data
9 13 @EqualsAndHashCode(callSuper = true)
10 14 @ToString(callSuper = true)
11 15 public class HandOverExperimentOptions extends ExperimentOptions {
12 16  
13 17  
  18 + private Mimo mimo;
  19 +
  20 + protected List<StationPair> pairs;
  21 +
14 22 @Override
15 23 protected boolean checkOptions() {
  24 +
  25 + if(!super.checkOptions()) {
  26 + return false;
  27 + }
  28 +
  29 + if(pairs.size()<=2) {
  30 + return false;
  31 + }
  32 +
16 33 return true;
17 34 }
18 35 }
... ...
src/main/java/com/example/mina/client/experiment/HandoverExperiment.java
1 1 package com.example.mina.client.experiment;
2 2  
3   -import com.example.mina.client.base.Experiment;
4   -import com.example.mina.client.base.ExperimentOptions;
5   -import com.example.mina.client.base.MatrixClient;
6   -import com.example.mina.client.base.MatrixCommand;
7   -import com.example.mina.client.base.MatrixConstants;
8   -import com.example.mina.client.base.StationPair;
  3 +import com.example.mina.client.base.*;
  4 +import org.springframework.util.CollectionUtils;
  5 +
9 6 import java.util.ArrayList;
10 7 import java.util.List;
11   -import org.springframework.util.CollectionUtils;
12 8  
13   -public class HandoverExperiment extends Experiment {
  9 +public class HandoverExperiment extends Experiment<MatrixClient, HandOverExperimentOptions> {
14 10  
15   - public HandoverExperiment(MatrixClient client, ExperimentOptions options) {
  11 + public HandoverExperiment(MatrixClient client, HandOverExperimentOptions options) {
16 12 super(client, options);
17 13 }
18 14  
... ... @@ -37,12 +33,12 @@ public class HandoverExperiment extends Experiment {
37 33 return;
38 34 }
39 35 // circle count
40   - Integer count = options.getMaxIterCount();
  36 + Integer count = options.getIterations();
41 37 if (count == null || count < 1) {
42 38 return;
43 39 }
44 40 Integer step = options.getStep();
45   - Integer endAttenuation = options.getEndAttenuation();
  41 + Integer endAttenuation = options.getEndAttn();
46 42 if (step == null || step <= 0) {
47 43 return;
48 44 }
... ...
src/main/java/com/example/mina/client/experiment/StepIterator.java 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +package com.example.mina.client.experiment;
  2 +
  3 +import com.example.mina.client.base.MatrixCommand;
  4 +import com.example.mina.client.base.StationPair;
  5 +
  6 +import java.util.Iterator;
  7 +
  8 +/**
  9 + * This is used for handover iterations
  10 + * Usage:
  11 + * StationPair stationPair = StationPair.builder().out(5).inList(Arrays.asList(1,2,3)).build();
  12 + * StepIterator stepIterator = new StepIterator(stationPair, 3, 43, 10);
  13 + *
  14 + *
  15 + * while (stepIterator.hasNext()) {
  16 + * MatrixCommand[] cmds = stepIterator.next();
  17 + * System.out.println(cmds[0] + "----------"+ cmds[1]);
  18 + * }
  19 + *
  20 + */
  21 +
  22 +public class StepIterator implements Iterator{
  23 +
  24 + private StationPair stationPair;
  25 + private int minAttn;
  26 + private int maxAttn;
  27 + private int step;
  28 +
  29 + private int curInputIndex;
  30 + private int nextInputIndex;
  31 +
  32 + private int curAttn1;
  33 + private int curAttn2;
  34 +
  35 + public StepIterator(StationPair station, int min, int max, int step) {
  36 + this.stationPair = station;
  37 + this.minAttn = min;
  38 + this.maxAttn = max;
  39 + this.step = step;
  40 +
  41 + curInputIndex = 0;
  42 + nextInputIndex = curInputIndex + 1;
  43 + curAttn1 = minAttn;
  44 + curAttn2 = maxAttn;
  45 + }
  46 +
  47 + @Override
  48 + public boolean hasNext() {
  49 + return curInputIndex<stationPair.getInList().size() && curAttn2<=maxAttn;
  50 + }
  51 +
  52 + @Override
  53 + public MatrixCommand[] next() {
  54 + MatrixCommand[] cmds = new MatrixCommand[2];
  55 +
  56 + cmds[0] = MatrixCommand.builder().row(stationPair.getInList().get(curInputIndex))
  57 + .col(stationPair.getOut()).attn(curAttn1).build();
  58 + cmds[1] = MatrixCommand.builder().row(stationPair.getInList().get(nextInputIndex))
  59 + .col(stationPair.getOut()).attn(curAttn2).build();
  60 + if(curAttn1 >= maxAttn) {
  61 + cmds[0].setAttn(maxAttn);
  62 + cmds[1].setAttn(minAttn);
  63 + curInputIndex++;
  64 + nextInputIndex = (curInputIndex + 1) % stationPair.getInList().size();
  65 +
  66 + curAttn1 = minAttn;
  67 + curAttn2 = maxAttn;
  68 + }else{
  69 + curAttn1 = curAttn1 + step;
  70 + curAttn2 = curAttn2 - step;
  71 + }
  72 +
  73 + return cmds;
  74 + }
  75 +}
... ...