Commit d34c9d807516eb2227b575085c1467fe4cd300f2

Authored by 尹本进
1 parent 23416fa6
Exists in develop

experiment

src/main/java/com/example/mina/client/base/AbstractClientFactory.java
@@ -16,22 +16,15 @@ public abstract class AbstractClientFactory { @@ -16,22 +16,15 @@ public abstract class AbstractClientFactory {
16 abstract public AbstractMatrixIoHandler getClientHandler(); 16 abstract public AbstractMatrixIoHandler getClientHandler();
17 17
18 public void buildFilterChain(IoFilterChain ioFilterChain) throws Exception{ 18 public void buildFilterChain(IoFilterChain ioFilterChain) throws Exception{
19 -  
20 ioFilterChain.addLast("codec", new ProtocolCodecFilter(new ByteProtocolFactory())); 19 ioFilterChain.addLast("codec", new ProtocolCodecFilter(new ByteProtocolFactory()));
21 -  
22 } 20 }
23 21
24 public MatrixClient createClient(MatrixConnectConfig connectConfig){ 22 public MatrixClient createClient(MatrixConnectConfig connectConfig){
25 -  
26 -  
27 try{ 23 try{
28 -  
29 MatrixClient client = new MatrixClient(connectConfig); 24 MatrixClient client = new MatrixClient(connectConfig);
30 -  
31 client.setClientHandler(getClientHandler()); 25 client.setClientHandler(getClientHandler());
32 client.setMatrixDataProxy(matrixDataProxy); 26 client.setMatrixDataProxy(matrixDataProxy);
33 client.connect(); 27 client.connect();
34 -  
35 buildFilterChain(client.getFilterChain()); 28 buildFilterChain(client.getFilterChain());
36 return client; 29 return client;
37 }catch (Exception e) { 30 }catch (Exception e) {
src/main/java/com/example/mina/client/base/AbstractExperimentFactory.java 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +package com.example.mina.client.base;
  2 +
  3 +import lombok.extern.slf4j.Slf4j;
  4 +
  5 +@Slf4j
  6 +public abstract class AbstractExperimentFactory<T extends Experiment<MatrixClient, ExperimentOptions>> {
  7 +
  8 + abstract public boolean isSupport(ExperimentOptions options);
  9 +
  10 + protected abstract Class<T> getClazz();
  11 +
  12 + public ExperimentProgressListner getListner() {
  13 + //TODO 生成任务进度更新监听器
  14 + return null;
  15 + }
  16 +
  17 + public T createExperiment(MatrixClient client, ExperimentOptions options) {
  18 + if (isSupport(options)) {
  19 + log.error("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());
  20 + throw new RuntimeException("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());
  21 + }
  22 + try {
  23 + T t = getClazz().getDeclaredConstructor().newInstance();
  24 + t.setClient(client);
  25 + t.setOptions(options);
  26 + t.setProgressListner(getListner());
  27 + return t;
  28 + } catch (Exception e) {
  29 + log.error("create a experiment task is failed, matrix id: " + client.getDeviceId());
  30 + throw new RuntimeException("create a experiment task is failed, matrix id: " + client.getDeviceId());
  31 + }
  32 + }
  33 +
  34 +}
src/main/java/com/example/mina/client/base/ExperimentFactory.java
@@ -1,62 +0,0 @@ @@ -1,62 +0,0 @@
1 -package com.example.mina.client.base;  
2 -  
3 -import lombok.extern.slf4j.Slf4j;  
4 -import org.springframework.util.CollectionUtils;  
5 -  
6 -import java.util.Map;  
7 -  
8 -@Slf4j  
9 -public abstract class ExperimentFactory < T extends Experiment> {  
10 -  
11 - private Map<String, T> experiments;  
12 -  
13 - public ExperimentFactory() {  
14 - experiments = CollectionUtils.newHashMap(10);  
15 - }  
16 -  
17 - public T getExperiment(String matrixId) {  
18 -  
19 - return experiments.getOrDefault(matrixId, null);  
20 - }  
21 -  
22 - public void stopExperiment(String matrixId) {  
23 - T t = getExperiment(matrixId);  
24 -  
25 - if( t != null && t.isContinue()){  
26 - t.finish();  
27 - experiments.remove(matrixId);  
28 - }  
29 - }  
30 -  
31 - abstract public boolean isSupport();  
32 -  
33 - abstract Class<T> getClazz();  
34 -  
35 - public ExperimentProgressListner getListner() {  
36 - return null; //TODO 生成任务进度更新监听器  
37 - }  
38 -  
39 - public T createExperiment(MatrixClient client, ExperimentOptions options){  
40 -  
41 - if(isSupport()) {  
42 - log.error("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());  
43 - throw new RuntimeException("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());  
44 - }  
45 -  
46 - Class<T> tClass = getClazz();  
47 -  
48 - try{  
49 -  
50 - T t = tClass.newInstance();  
51 -  
52 - t.setClient(client);  
53 - t.setOptions(options);  
54 - t.setProgressListner(getListner());  
55 - return t;  
56 - }catch (Exception e) {  
57 - log.error("create a experiment task is failed, matrix id: " + client.getDeviceId());  
58 - throw new RuntimeException("create a experiment task is failed, matrix id: " + client.getDeviceId());  
59 - }  
60 - }  
61 -  
62 -}  
src/main/java/com/example/mina/client/base/ExperimentManager.java 0 → 100644
@@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
  1 +package com.example.mina.client.base;
  2 +
  3 +import java.util.concurrent.ConcurrentHashMap;
  4 +import java.util.concurrent.ConcurrentMap;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.springframework.util.Assert;
  7 +
  8 +/**
  9 + * @author BenJin Yin
  10 + * @date 2021/4/6
  11 + */
  12 +@Slf4j
  13 +public class ExperimentManager<T extends AbstractExperimentFactory> {
  14 + private final ConcurrentMap<String, T> experimentFactories;
  15 +
  16 + private final ConcurrentMap<String, Experiment<? extends MatrixClient,? extends ExperimentOptions>> experiments
  17 + = new ConcurrentHashMap<>(8);
  18 +
  19 + public ExperimentManager(ConcurrentMap<String, T> experimentFactories) {
  20 + this.experimentFactories = experimentFactories;
  21 + }
  22 +
  23 + public Experiment<? extends MatrixClient,? extends ExperimentOptions> getExperiment(String experimentId) {
  24 + return experiments.getOrDefault(experimentId, null);
  25 + }
  26 +
  27 + public void stopExperiment(String experimentId) {
  28 + Experiment<? extends MatrixClient,? extends ExperimentOptions> t = getExperiment(experimentId);
  29 + if (t != null && t.isContinue()) {
  30 + t.finish();
  31 + experiments.remove(experimentId);
  32 + }
  33 + }
  34 +
  35 + public T getClientFactory(String name) {
  36 + return this.experimentFactories.get(name);
  37 + }
  38 +
  39 + public Experiment<? extends MatrixClient,? extends ExperimentOptions> getOrCreateExperiment(MatrixClient client, ExperimentOptions options) {
  40 + Assert.notNull(client, "matrix connect config is null");
  41 + Assert.hasLength(client.getDeviceId(), "deviceId can not be empty");
  42 +
  43 + String deviceId = client.getDeviceId();
  44 + Experiment<? extends MatrixClient,? extends ExperimentOptions> experiment = experiments.get(options.getExperimentId());
  45 + if (experiment != null) {
  46 + return experiment;
  47 + }
  48 + if (!experimentFactories.containsKey(deviceId)) {
  49 + log.error("the matrix type is not supported by the system! matrix type is: " + deviceId);
  50 + throw new RuntimeException("the matrix type is not supported by the system! matrix type is: " + deviceId);
  51 +
  52 + }
  53 + T t = experimentFactories.get(deviceId);
  54 + experiment = t.createExperiment(client, options);
  55 + experiments.put(options.experimentId, experiment);
  56 + return experiment;
  57 + }
  58 +
  59 +}
src/main/java/com/example/mina/client/base/ExperimentOptions.java
1 package com.example.mina.client.base; 1 package com.example.mina.client.base;
2 2
3 import java.time.ZonedDateTime; 3 import java.time.ZonedDateTime;
4 -import java.util.List;  
5 import lombok.Data; 4 import lombok.Data;
6 5
7 @Data 6 @Data
@@ -17,28 +16,18 @@ public abstract class ExperimentOptions { @@ -17,28 +16,18 @@ public abstract class ExperimentOptions {
17 16
18 protected Integer step; 17 protected Integer step;
19 18
20 - /**  
21 - * 每次发送指令的 时间间隔  
22 - */  
23 protected Integer period; 19 protected Integer period;
24 20
25 - /**  
26 - * 单次从 start --> end 一个周期后的 暂停时间  
27 - */  
28 protected Integer pause; 21 protected Integer pause;
29 22
30 - /**  
31 - * 最大循环次数  
32 - */  
33 protected Integer iterations; 23 protected Integer iterations;
34 24
35 protected ZonedDateTime finishAt; 25 protected ZonedDateTime finishAt;
36 26
37 -  
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())) { 27 + protected boolean checkOptions() {
  28 + if (startAttn < 0 || endAttn < 0 || startAttn > endAttn || step <= 0
  29 + || pause < 0 || period <= 0 || iterations <= 0 || finishAt == null ||
  30 + finishAt.isBefore(ZonedDateTime.now())) {
42 return false; 31 return false;
43 } 32 }
44 return true; 33 return true;
src/main/java/com/example/mina/client/base/ExperimentType.java 0 → 100644
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +package com.example.mina.client.base;
  2 +
  3 +/**
  4 + * @author BenJin Yin
  5 + * @date 2021/4/6
  6 + */
  7 +public enum ExperimentType {
  8 + HAND_OVER("handOver");
  9 +
  10 + private final String remark;
  11 +
  12 + ExperimentType(String remark) {
  13 + this.remark = remark;
  14 + }
  15 +
  16 + public String getRemark() {
  17 + return remark;
  18 + }
  19 +
  20 + public static ExperimentType getInstanceByRemark(String remark) {
  21 + for (ExperimentType obj : values()) {
  22 + if (obj.getRemark().equals(remark)) {
  23 + return obj;
  24 + }
  25 + }
  26 + return null;
  27 + }
  28 +}
src/main/java/com/example/mina/client/base/MatrixConnectConfig.java
@@ -20,11 +20,5 @@ public class MatrixConnectConfig { @@ -20,11 +20,5 @@ public class MatrixConnectConfig {
20 20
21 private Integer port; 21 private Integer port;
22 22
23 -// private Integer rows;  
24 -  
25 -// private Integer cols;  
26 -  
27 -// private Integer maxAttenuation;  
28 -  
29 private Map<String, Object> options; 23 private Map<String, Object> options;
30 } 24 }
src/main/java/com/example/mina/client/base/MatrixConstants.java
1 package com.example.mina.client.base; 1 package com.example.mina.client.base;
2 2
3 -public class MatrixConstants { 3 +public final class MatrixConstants {
4 4
5 public static final String SESSION_CONFIG_NAME = "SESSION_CONFIG"; 5 public static final String SESSION_CONFIG_NAME = "SESSION_CONFIG";
6 6
src/main/java/com/example/mina/client/base/StationPair.java
@@ -6,7 +6,6 @@ import lombok.Builder; @@ -6,7 +6,6 @@ import lombok.Builder;
6 import lombok.Data; 6 import lombok.Data;
7 7
8 /** 8 /**
9 - * 输入输出端口  
10 * @author BenJin Yin 9 * @author BenJin Yin
11 * @date 2021/4/2 10 * @date 2021/4/2
12 */ 11 */
@@ -14,11 +13,11 @@ import lombok.Data; @@ -14,11 +13,11 @@ import lombok.Data;
14 @Builder 13 @Builder
15 public class StationPair { 14 public class StationPair {
16 /** 15 /**
17 - * 输出端口 16 + * out port
18 */ 17 */
19 private Integer out; 18 private Integer out;
20 /** 19 /**
21 - * 输入端口 20 + * in port
22 */ 21 */
23 private List<Integer> inList; 22 private List<Integer> inList;
24 } 23 }
src/main/java/com/example/mina/client/experiment/HandOverExperimentFactory.java 0 → 100644
@@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
  1 +package com.example.mina.client.experiment;
  2 +
  3 +import com.example.mina.client.base.AbstractExperimentFactory;
  4 +import com.example.mina.client.base.Experiment;
  5 +import com.example.mina.client.base.ExperimentOptions;
  6 +import com.example.mina.client.base.ExperimentType;
  7 +import org.springframework.stereotype.Component;
  8 +
  9 +/**
  10 + * @author BenJin Yin
  11 + * @date 2021/4/6
  12 + */
  13 +@Component
  14 +public class HandOverExperimentFactory extends AbstractExperimentFactory {
  15 + @Override
  16 + public boolean isSupport(ExperimentOptions options) {
  17 + ExperimentType instance = ExperimentType.getInstanceByRemark(options.getExperimentType());
  18 + return instance == ExperimentType.HAND_OVER;
  19 + }
  20 +
  21 + @Override
  22 + public Class<? extends Experiment> getClazz() {
  23 + return HandoverExperiment.class;
  24 + }
  25 +}
src/main/java/com/example/mina/client/experiment/HandoverExperiment.java
1 package com.example.mina.client.experiment; 1 package com.example.mina.client.experiment;
2 2
3 -import com.example.mina.client.base.*;  
4 -import org.springframework.util.CollectionUtils;  
5 - 3 +import com.example.mina.client.base.Experiment;
  4 +import com.example.mina.client.base.MatrixClient;
  5 +import com.example.mina.client.base.MatrixCommand;
  6 +import com.example.mina.client.base.MatrixConstants;
  7 +import com.example.mina.client.base.StationPair;
6 import java.util.ArrayList; 8 import java.util.ArrayList;
7 import java.util.List; 9 import java.util.List;
  10 +import org.springframework.util.CollectionUtils;
8 11
9 public class HandoverExperiment extends Experiment<MatrixClient, HandOverExperimentOptions> { 12 public class HandoverExperiment extends Experiment<MatrixClient, HandOverExperimentOptions> {
10 13