目录
1、需求
发送方将文件按照数据帧进行发送,接收方完成数据接收的还原,即还原为相应的文件。
2、逻辑实现
采用ConcrrenutHashMap作为缓冲区,每次处理时都判断,数据是否连续,如果连续,就进行就根据数据偏移量完成数据文件的写入(数据偏移量是由帧头相应字段计算所得,是前期设计好的帧头),当达到缓冲区的某个阈值时,会对接收到的数据帧进行处理;如果前后接收到的数据帧时间超过某个阈值,就表示数据帧在传输过程中丢失了,那么就进行记录。
3、代码实现
package com.ruoyi.system.service.customService.method1;
/**
* @Author 不要有情绪的 ljy
* @Date 2024/5/17 11:35
* @Description:
*/
import com.ruoyi.system.domain.NetworkConfig;
import com.ruoyi.system.service.INetworkConfigService;
import com.ruoyi.system.service.customService.dealGKService_NewThread.DealGkDataServiceSuperWithNewThread;
import com.ruoyi.system.service.customService.dealGKService_ThreadPool.DealGkDataServiceSuperWithThreadPool;
import com.ruoyi.system.service.customService.dealGKService_ThreadPool_Buffer.DealGkDataServiceSuperWithThreadPoolAndBuffer;
import com.ruoyi.system.service.customService.method2.SaveGKOriginalDataServiceWithBuffer;
import com.ruoyi.system.utlis.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class UDPReceiverSuper {
private static final int BUFFER_SIZE = 1044;
private static final int HEAD_SIZE = 20;
private static final int DATA_SIZE = 1024;
private static final int MAX_BUFFER_SIZE = 1 * 1024 * 1024; // 缓冲器大小设置为1MB
private static final double MAX_BUFFER_THRESHOLD = 0.8; // 缓冲区阈值
private static final int MAX_BUFFER_INDEX = (int) (MAX_BUFFER_SIZE * MAX_BUFFER_THRESHOLD / DATA_SIZE); //缓冲区元素数量阈值
//timestampToBufferMap存储的是:时间戳,TreeMap,TreeMap里面存储的是:当前包序号,接受数据的对象
private Map<Long, ConcurrentHashMap<Long, DatagramPacket>> timestampToBufferMap = new HashMap();
private long timeStamp;
private boolean isClosed = false;// 使用阻塞队列作为缓冲区
private long errorPackageSum = 0;
private int frameNum; //用于帧计数
Thread udpReceiverThread;
@Value("${GK.GKOriginalDataFilePath}")
private String GKOriginalDataFilePath; // 管控原始数据文件存储路径
@Value("${HP.storagePath}")
private String storagePath; //高性能数据接收路径
@Autowired
private INetworkConfigService networkConfigService;
@Autowired
private DealGkDataServiceSuperWithNewThread dealGkDataServiceSuperWithNewThread;
@Autowired
private DealGkDataServiceSuperWithThreadPoolAndBuffer dealGkDataServiceSuperWithThreadPoolAndBuffer;
@Autowired
private DealGkDataServiceSuperWithThreadPool dealGkDataServiceSuperWithThreadPool;
@Autowired
private SaveGKOriginalDataService saveGKOriginalDataService;
@Autowired
private SaveGKOriginalDataServiceWithBuffer saveGKOriginalDataServiceWithBuffer;
public UDPReceiverSuper() {
}
public void start() {
//创建父文件夹
Path path = Paths.get(storagePath);
if (Files.notExists(path)) {
try {
Files.createDirectories(path);
System.out.println("Directories created successfully: " + storagePath);
} catch (IOException e) {
System.err.println("Failed to create directories: " + e.getMessage());
}