|
@@ -1,10 +1,12 @@
|
|
|
package com.sig.ggits.tsinfo.server.service.worker;
|
|
|
|
|
|
+import com.its.common.utils.SysUtils;
|
|
|
import com.sig.common.protocol.SigProtocolConst;
|
|
|
import com.sig.ggits.tsinfo.server.dto.GgitsRegion;
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.io.File;
|
|
@@ -32,6 +34,7 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
private InetAddress serverAddress;
|
|
|
private DatagramSocket datagramSocket;
|
|
|
private int packetSeq = 0;
|
|
|
+ private int sendLcNoCnt = 0;
|
|
|
|
|
|
public TsinfoFileWorker(GgitsRegion center, String filePath, String absoluteFilePath) throws IOException {
|
|
|
this.service = FileSystems.getDefault().newWatchService();
|
|
@@ -50,7 +53,8 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
this.datagramSocket = new DatagramSocket();
|
|
|
try {
|
|
|
this.serverAddress = InetAddress.getByName(this.center.getServerIp());
|
|
|
- } catch (UnknownHostException e) {
|
|
|
+ }
|
|
|
+ catch (UnknownHostException e) {
|
|
|
log.error("{}", e.getMessage());
|
|
|
return false;
|
|
|
}
|
|
@@ -64,10 +68,10 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
if (!initUdpChannel()) {
|
|
|
- log.error("TsinfoFileWorker.run: initUdpChannel failed: {}, {}", this.filePath, this.absolutePath);
|
|
|
+ log.error("TsinfoFileWorker.run: initUdpChannel failed: {}, {}, {}", this.center.getRegionCd(), this.filePath, this.absolutePath);
|
|
|
return;
|
|
|
}
|
|
|
- log.info("TsinfoFileWorker.run: Start File Watcher: {}, {}", this.filePath, this.absolutePath);
|
|
|
+ log.info("TsinfoFileWorker.run: Start File Watcher: {}, {}, {}", this.center.getRegionCd(), this.filePath, this.absolutePath);
|
|
|
|
|
|
String oldFileName = "";
|
|
|
long oldFileSize = 0;
|
|
@@ -81,66 +85,75 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
key = service.take();
|
|
|
}
|
|
|
catch (InterruptedException e) {
|
|
|
- log.error("{}, {}", this.filePath, e.getMessage());
|
|
|
+ log.error("{}, {}, {}", this.center.getRegionCd(), this.filePath, e.getMessage());
|
|
|
Thread.currentThread().interrupt();
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (key.isValid()) {
|
|
|
List<WatchEvent<?>> watchEvents = key.pollEvents();
|
|
|
- for (WatchEvent<?> watchEvent : watchEvents) {
|
|
|
- WatchEvent<Path> pathEvent = (WatchEvent<Path>) watchEvent;
|
|
|
- Path path = pathEvent.context();
|
|
|
- WatchEvent.Kind<Path> eventKind = pathEvent.kind();
|
|
|
-
|
|
|
- if (eventKind != StandardWatchEventKinds.ENTRY_MODIFY) {
|
|
|
- log.warn("{}, Unsupported event, {}", this.filePath, eventKind.name());
|
|
|
- continue;
|
|
|
- }
|
|
|
+ try {
|
|
|
+ MDC.put("id", this.center.getLogKey());
|
|
|
|
|
|
- // A1-20240723153652.dat
|
|
|
- String fileName = path.toString();
|
|
|
- if (fileName.length() != 21) {
|
|
|
- log.error("{}, FileName Length Error. {}", this.filePath, fileName);
|
|
|
- continue;
|
|
|
- }
|
|
|
- String fileExt = StringUtils.getFilenameExtension(fileName);
|
|
|
- if (!"dat".equalsIgnoreCase(fileExt)) {
|
|
|
- log.error("{}, FileName Extension Error. {}", this.filePath, fileName);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ for (WatchEvent<?> watchEvent : watchEvents) {
|
|
|
+ WatchEvent<Path> pathEvent = (WatchEvent<Path>) watchEvent;
|
|
|
+ Path path = pathEvent.context();
|
|
|
+ WatchEvent.Kind<Path> eventKind = pathEvent.kind();
|
|
|
|
|
|
- Path absoluteFilePath = FileSystems.getDefault().getPath(this.absolutePath + File.separator + fileName);
|
|
|
- long fileSize = absoluteFilePath.toFile().length();
|
|
|
+ if (eventKind != StandardWatchEventKinds.ENTRY_MODIFY) {
|
|
|
+ log.warn("{}, {}, Unsupported event, {}", this.center.getRegionCd(), this.filePath, eventKind.name());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if (fileSize == 0) {
|
|
|
- log.warn("{}, FileSize is zero, {}", this.filePath, fileName);
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (fileName.equals(oldFileName)) {
|
|
|
- log.warn("{}, FileName is equals, {}, {}, {}, {}", this.filePath, fileName, oldFileName, oldFileSize, fileSize);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // A1-20240723153652.dat
|
|
|
+ String fileName = path.toString();
|
|
|
+ if (fileName.length() != 21) {
|
|
|
+ log.error("{}, {}, FileName Length Error. {}", this.center.getRegionCd(), this.filePath, fileName);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String fileExt = StringUtils.getFilenameExtension(fileName);
|
|
|
+ if (!"dat".equalsIgnoreCase(fileExt)) {
|
|
|
+ log.error("{}, {}, FileName Extension Error. {}", this.center.getRegionCd(), this.filePath, fileName);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Path absoluteFilePath = FileSystems.getDefault().getPath(this.absolutePath + File.separator + fileName);
|
|
|
+ long fileSize = absoluteFilePath.toFile().length();
|
|
|
+
|
|
|
+ if (fileSize == 0) {
|
|
|
+ log.warn("{}, {}, FileSize is zero, {}", this.center.getRegionCd(), this.filePath, fileName);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (fileName.equals(oldFileName)) {
|
|
|
+ log.warn("{}, {}, FileName is equals, {}, {}, {}, {}", this.center.getRegionCd(), this.filePath, fileName, oldFileName, oldFileSize, fileSize);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- //remainder = (int)(fileSize % 10);
|
|
|
- //dataCount = (int)(fileSize / 10);
|
|
|
- createTime = fileName.substring(3, 17);
|
|
|
- for (int ii = 0; ii < 2; ii++) {
|
|
|
- calDataCount = tsinfoDataSend(absoluteFilePath, fileName, createTime);
|
|
|
- log.info("{}, DataSend: {}", this.filePath, calDataCount);
|
|
|
- if (calDataCount > 0) {
|
|
|
- break;
|
|
|
+ //remainder = (int)(fileSize % 10);
|
|
|
+ //dataCount = (int)(fileSize / 10);
|
|
|
+ createTime = fileName.substring(3, 17);
|
|
|
+ for (int ii = 0; ii < 2; ii++) {
|
|
|
+ calDataCount = tsinfoDataSend(absoluteFilePath, fileName, createTime);
|
|
|
+ log.info("{}, {}, DataSend: {} EA. {}.", this.center.getRegionCd(), this.filePath, this.sendLcNoCnt, fileName);
|
|
|
+ if (calDataCount > 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
+ this.center.setLastCommMilliSeconds(System.currentTimeMillis());
|
|
|
+ this.center.setSendSeq(this.center.getSendSeq() + 1);
|
|
|
+ oldFileName = fileName;
|
|
|
+ oldFileSize = fileSize;
|
|
|
+ // if (calDataCount > 0) {
|
|
|
+ // this.center.setClctTime(createTime);
|
|
|
+ // this.center.setClctCount(calDataCount);
|
|
|
+ // }
|
|
|
}
|
|
|
- this.center.setLastCommMilliSeconds(System.currentTimeMillis());
|
|
|
- this.center.setSendSeq(this.center.getSendSeq()+1);
|
|
|
- oldFileName = fileName;
|
|
|
- oldFileSize = fileSize;
|
|
|
-// if (calDataCount > 0) {
|
|
|
-// this.center.setClctTime(createTime);
|
|
|
-// this.center.setClctCount(calDataCount);
|
|
|
-// }
|
|
|
}
|
|
|
+ finally {
|
|
|
+ MDC.remove(this.center.getLogKey());
|
|
|
+ MDC.clear();
|
|
|
+ }
|
|
|
+
|
|
|
if (!key.reset()) {
|
|
|
log.error("{}, FileWatcher Event Reset Error....", this.filePath);
|
|
|
break;
|
|
@@ -175,12 +188,14 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
byte min = (byte)now.getMinute();
|
|
|
byte sec = (byte)now.getSecond();
|
|
|
|
|
|
+ this.sendLcNoCnt = 0;
|
|
|
File file = new File(absoluteFilePath.toString());
|
|
|
try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
|
|
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
// 데이터 길이 계산 (2바이트)
|
|
|
short dataLength = (short) bytesRead;
|
|
|
short count = (short)(dataLength / 10);
|
|
|
+ this.sendLcNoCnt += count;
|
|
|
this.packetSeq++;
|
|
|
|
|
|
// Packet Frame Head
|
|
@@ -216,16 +231,19 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
packet.getBytes(packet.readerIndex(), bytes);
|
|
|
java.net.DatagramPacket sendPacket = new java.net.DatagramPacket(bytes, bytes.length, this.serverAddress, this.center.getServerPort());
|
|
|
this.datagramSocket.send(sendPacket);
|
|
|
-// log.info("Packet Frame Sent: Data {} EA, Total {} Bytes.", count, length);
|
|
|
+ if (this.center.isDump()) {
|
|
|
+ //log.info("Packet Frame Sent: Data {} EA, Total {} Bytes.", count, length);
|
|
|
+ log.info("SEND: [{}]. {} Bytes. {}", center.getLogKey(), bytes.length, SysUtils.byteArrayToHex(bytes));
|
|
|
+ }
|
|
|
calDataCount++;
|
|
|
}
|
|
|
}
|
|
|
catch (FileNotFoundException e) {
|
|
|
- log.warn("{}, {}, File Not Found. {}", this.filePath, absoluteFilePath, e.getMessage());
|
|
|
+ log.warn("{}, {}, {}, File Not Found. {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
return -1;
|
|
|
}
|
|
|
catch (IOException e) {
|
|
|
- log.warn("{}, {}, IOException: {}", this.filePath, absoluteFilePath, e.getMessage());
|
|
|
+ log.warn("{}, {}, {}, IOException: {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
return calDataCount;
|
|
|
}
|
|
|
return calDataCount;
|