| | |
| | | package com.ots.project.exam.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.ots.project.exam.service.ReportCalculationServices; |
| | | import com.ots.project.tool.report.reportCalculation.response.ReportAPIResult; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.PreDestroy; |
| | | import java.io.BufferedReader; |
| | |
| | | import java.io.PrintWriter; |
| | | import java.net.Socket; |
| | | import java.util.Objects; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class ReportCalculationServicesImpl implements ReportCalculationServices { |
| | | |
| | | private Socket socket; |
| | | |
| | | private PrintWriter socketOut; |
| | | |
| | | private BufferedReader socketIn; |
| | | |
| | | @Value("${ATSTai.port}") |
| | | private int port; |
| | | |
| | | @Value("${ATSTai.host}") |
| | | private String host; |
| | | |
| | | @PostConstruct |
| | | private void init() { |
| | | try { |
| | | |
| | | //首先直接创建socket,端口号1~1023为系统保存,一般设在1023之外 |
| | | socket = new Socket(host, port); |
| | | |
| | | //创建三个流,系统输入流BufferedReader systemIn,socket输入流BufferedReader socketIn,socket输出流PrintWriter socketOut; |
| | | socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| | | socketOut = new PrintWriter(socket.getOutputStream()); |
| | | } catch (Exception ex) { |
| | | log.error("调用泰国团队组件连接失败:{}",ex.getMessage(),ex); |
| | | } |
| | | } |
| | | |
| | | @PreDestroy |
| | | private void destory() { |
| | | if (Objects.nonNull(socketIn)) { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断是否断开连接,断开返回true,没有返回false |
| | | * @param socket |
| | | * @return |
| | | */ |
| | | public Boolean isServerClose(Socket socket){ |
| | | try{ |
| | | socket.sendUrgentData(0xFF); |
| | | socket.sendUrgentData(0xFF);//发送1个字节的紧急数据,默认情况下,服务器端没有开启紧急数据处理,不影响正常通信 |
| | | return false; |
| | | }catch(Exception se){ |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public ReportAPIResult stageCalculation(Object request) { |
| | | if (isServerClose(socket)) { |
| | |
| | | } |
| | | return reportAPIResult; |
| | | } |
| | | |
| | | private ReportAPIResult getReportAPIResult(Object request) throws IOException { |
| | | ReportAPIResult reportAPIResult; |
| | | socketOut.println(JSON.toJSONString(request)); |
| | | socketOut.flush(); |
| | | socketOut.flush();//赶快刷新使Server收到,也可以换成socketOut.println(readline, ture) |
| | | String inTemp = socketIn.readLine(); |
| | | reportAPIResult = JSON.parseObject(inTemp, ReportAPIResult.class); |
| | | return reportAPIResult; |