Просмотр исходного кода

增加了websocket的部分但是并未跑通//TODO

Ruilin Geng 3 лет назад
Родитель
Сommit
d56e517c73

+ 7 - 0
src/main/java/cn/edu/nju/plagdemo/api/impl/PlagDetectApiImpl.java

@@ -8,11 +8,13 @@ import cn.edu.nju.plagdemo.model.dto.plag.EndpointDTO;
 import cn.edu.nju.plagdemo.model.po.plag.AlgorithmPO;
 import cn.edu.nju.plagdemo.model.po.plag.BaseFilePO;
 import cn.edu.nju.plagdemo.model.po.plag.DetectionFilePO;
+import cn.edu.nju.plagdemo.model.po.plag.RecordPO;
 import cn.edu.nju.plagdemo.model.vo.AliyunTokenVO;
 import cn.edu.nju.plagdemo.model.vo.OssInfoVO;
 import cn.edu.nju.plagdemo.service.IAlgorithmService;
 import cn.edu.nju.plagdemo.service.IBaseFileService;
 import cn.edu.nju.plagdemo.service.IDetectionFileService;
+import cn.edu.nju.plagdemo.service.IRecordService;
 import cn.edu.nju.plagdemo.utils.RestResult;
 import cn.edu.nju.plagdemo.utils.SimpleCache;
 import com.aliyun.fc_open20210406.Client;
@@ -64,6 +66,10 @@ public class PlagDetectApiImpl implements IPlagDetectApi {
     @Autowired
     private IBaseFileService baseFileService;
 
+
+    @Autowired
+    private IRecordService recordService;
+
     @Autowired
     private RestTemplate restTemplate;
 
@@ -107,6 +113,7 @@ public class PlagDetectApiImpl implements IPlagDetectApi {
     @Override
     public RestResult<?> plagDetect(Integer file, Integer baseFile, Integer fileType, Integer algorithm, Map<String, Object> params) {
         // TODO 需要向数据库中记录record
+        RecordPO recordPO = new RecordPO();
         AlgorithmPO algorithmPO = algorithmService.getById(algorithm);
         String defaultOptions = algorithmPO.getDefaultOptions();
         ObjectMapper objectMapper = new ObjectMapper();

+ 90 - 0
src/main/java/cn/edu/nju/plagdemo/api/socket/MyWebSocket.java

@@ -0,0 +1,90 @@
+package cn.edu.nju.plagdemo.api.socket;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+@ServerEndpoint("/test")
+@Component
+@Slf4j
+public class MyWebSocket {
+
+    //保存所有在线socket连接
+    private static Map<String, MyWebSocket> webSocketMap = new LinkedHashMap<>();
+
+    //记录当前在线数目
+    private static int count = 0;
+
+    //当前连接(每个websocket连入都会创建一个MyWebSocket实例
+    private Session session;
+
+    //处理连接建立
+    @OnOpen
+    public void onOpen(Session session) {
+        this.session = session;
+        webSocketMap.put(session.getId(), this);
+        addCount();
+        log.info("新的连接加入:{}", session.getId());
+    }
+
+    //接受消息
+    @OnMessage
+    public void onMessage(String message, Session session) {
+        log.info("收到客户端{}消息:{}", session.getId(), message);
+        try {
+            this.sendMessage("收到消息:" + message);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    //处理错误
+    @OnError
+    public void onError(Throwable error, Session session) {
+        log.info("发生错误{},{}", session.getId(), error.getMessage());
+    }
+
+    //处理连接关闭
+    @OnClose
+    public void onClose() {
+        webSocketMap.remove(this.session.getId());
+        reduceCount();
+        log.info("连接关闭:{}", this.session.getId());
+    }
+
+    //群发消息
+
+    //发送消息
+    public void sendMessage(String message) throws IOException {
+        this.session.getBasicRemote().sendText(message);
+    }
+
+    //广播消息
+    public static void broadcast() {
+        MyWebSocket.webSocketMap.forEach((k, v) -> {
+            try {
+                v.sendMessage("这是一条测试广播");
+            } catch (Exception e) {
+            }
+        });
+    }
+
+    //获取在线连接数目
+    public static int getCount() {
+        return count;
+    }
+
+    //操作count,使用synchronized确保线程安全
+    public static synchronized void addCount() {
+        MyWebSocket.count++;
+    }
+
+    public static synchronized void reduceCount() {
+        MyWebSocket.count--;
+    }
+}

+ 14 - 0
src/main/java/cn/edu/nju/plagdemo/config/WebSocketConfig.java

@@ -0,0 +1,14 @@
+package cn.edu.nju.plagdemo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+public class WebSocketConfig {
+
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter(){
+        return new ServerEndpointExporter();
+    }
+}

+ 5 - 5
src/main/java/cn/edu/nju/plagdemo/controller/BaseFileController.java

@@ -46,8 +46,8 @@ public class BaseFileController {
             value = "like参数,可选值:name",
             dataType = "String"
     )
-    @GetMapping(value = "/getAll")
-    public RestResult<?> getAllBaseFilesAndLike(String param, PageForm page, String like) {
+    @GetMapping(value = "/getAllAndLike")
+    public RestResult<?> getAllBaseFilesAndLike(String param, PageForm page, @RequestParam String like) {
         Page<BaseFileDTO> BaseFileDTOPage = baseFileService.getAllAndLike(param, page, like);
         if (BaseFileDTOPage == null) {
             throw new DefinitionException(HttpStatus.INTERNAL_SERVER_ERROR, "获取所有待检测文件失败");
@@ -62,7 +62,7 @@ public class BaseFileController {
             dataType = "String"
     )
     @GetMapping(value = "/getByUserId")
-    public RestResult<?> getBaseFiles(String param, PageForm page, @Validated @NotNull @RequestParam Long userId) {
+    public RestResult<?> getBaseFilesByUserId(String param, PageForm page, @Validated @NotNull @RequestParam Long userId) {
         Page<BaseFileDTO> BaseFileDTOPage = baseFileService.getByUserId(param, page, userId);
         if (BaseFileDTOPage == null) {
             throw new DefinitionException(HttpStatus.INTERNAL_SERVER_ERROR, "获取该用户待检测文件失败");
@@ -77,8 +77,8 @@ public class BaseFileController {
             value = "like参数,可选值:name",
             dataType = "String"
     )
-    @GetMapping(value = "/getByUserId")
-    public RestResult<?> getBaseFilesAndLike(String param, PageForm page, String like,  @Validated @NotNull @RequestParam Long userId) {
+    @GetMapping(value = "/getByUserIdAndLike")
+    public RestResult<?> getBaseFilesByUserIdAndLike(String param, PageForm page, String like,  @Validated @NotNull @RequestParam Long userId) {
         Page<BaseFileDTO> BaseFileDTOPage = baseFileService.getByUserIdAndLike(param, page, userId, like);
         if (BaseFileDTOPage == null) {
             throw new DefinitionException(HttpStatus.INTERNAL_SERVER_ERROR, "获取所有待检测文件失败");

+ 2 - 2
src/main/java/cn/edu/nju/plagdemo/controller/DetectionFileController.java

@@ -56,7 +56,7 @@ public class DetectionFileController {
             value = "like参数,可选值:name",
             dataType = "String"
     )
-    @GetMapping(value = "/getAll")
+    @GetMapping(value = "/getAllAndLike")
     public RestResult<?> getAllDetectionFilesAndLike(String param, PageForm page, String like) {
         Page<DetectionFileDTO> detectionFileDTOPage = detectionFileService.getAllAndLike(param, page, like);
         if (detectionFileDTOPage == null) {
@@ -87,7 +87,7 @@ public class DetectionFileController {
             value = "like参数,可选值:name",
             dataType = "String"
     )
-    @GetMapping(value = "/getByUserId")
+    @GetMapping(value = "/getByUserIdAndLike")
     public RestResult<?> getDetectionFilesAndLike(String param, PageForm page, String like,  @Validated @NotNull @RequestParam Long userId) {
         Page<DetectionFileDTO> detectionFileDTOPage = detectionFileService.getByUserIdAndLike(param, page, userId, like);
         if (detectionFileDTOPage == null) {

+ 45 - 0
src/test/java/cn/edu/nju/plagdemo/api/ExecutionFinishCallbackTest.java

@@ -0,0 +1,45 @@
+package cn.edu.nju.plagdemo.api;
+
+import cn.edu.nju.plagdemo.api.impl.PlagDetectApiImpl;
+import cn.edu.nju.plagdemo.model.dto.plag.AlgorithmRemoteParams;
+import cn.edu.nju.plagdemo.model.dto.plag.EndpointDTO;
+import cn.edu.nju.plagdemo.model.vo.OssInfoVO;
+import cn.edu.nju.plagdemo.utils.RestResult;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.Rollback;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@SpringBootTest
+public class ExecutionFinishCallbackTest {
+    @Autowired
+    RestTemplate restTemplate;
+
+    @Autowired
+    PlagDetectApiImpl plagDetectApi;
+    @Autowired
+    IOssApi ossApi;
+
+    @Test
+    @Transactional
+    @Rollback(value = true)
+    void callFCLocalhost() throws Exception {
+        AlgorithmRemoteParams algorithmRemoteParams = new AlgorithmRemoteParams();
+        algorithmRemoteParams.setFileOssLink("development/file/base-1678885807796.zip");
+        algorithmRemoteParams.setBaseFileOssLink("development/basefile/test-1678885807800.zip");
+        Map<String, Object> map = new HashMap<>();
+        map.put("language","java");
+        algorithmRemoteParams.setOptions(map);
+        OssInfoVO ossClientInfo = ossApi.getOssClientInfo();
+        algorithmRemoteParams.setOssClient(ossClientInfo);
+        algorithmRemoteParams.setRecordId(2333);
+        EndpointDTO endpointDTO = new EndpointDTO("http://localhost:8887/2016-08-15/proxy/plag-detection/func-j7jidbdi/invoke");
+        RestResult<?> restResult = plagDetectApi.callFC(endpointDTO, algorithmRemoteParams);
+        System.out.println(restResult);
+    }
+}