|
@@ -3,6 +3,7 @@ package com.sig.ggits.tsinfo.server.service.worker;
|
|
|
import com.its.common.utils.SysUtils;
|
|
import com.its.common.utils.SysUtils;
|
|
|
import com.sig.common.protocol.SigProtocolConst;
|
|
import com.sig.common.protocol.SigProtocolConst;
|
|
|
import com.sig.ggits.tsinfo.server.dto.GgitsRegion;
|
|
import com.sig.ggits.tsinfo.server.dto.GgitsRegion;
|
|
|
|
|
+import com.sig.ggits.tsinfo.server.dto.UdpServerInfo;
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
import io.netty.buffer.Unpooled;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -13,12 +14,10 @@ import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.net.DatagramSocket;
|
|
|
|
|
-import java.net.InetAddress;
|
|
|
|
|
-import java.net.SocketException;
|
|
|
|
|
-import java.net.UnknownHostException;
|
|
|
|
|
|
|
+import java.net.*;
|
|
|
import java.nio.file.*;
|
|
import java.nio.file.*;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
@@ -31,16 +30,17 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
private final String filePath; // 지역 번호
|
|
private final String filePath; // 지역 번호
|
|
|
private final String absolutePath; // 파일 절대 경로
|
|
private final String absolutePath; // 파일 절대 경로
|
|
|
private final AtomicBoolean running = new AtomicBoolean(false);
|
|
private final AtomicBoolean running = new AtomicBoolean(false);
|
|
|
- private InetAddress serverAddress;
|
|
|
|
|
- private DatagramSocket datagramSocket;
|
|
|
|
|
|
|
+ private final List<String> ggitsUdpServers;
|
|
|
|
|
+ private final List<UdpServerInfo> ggitsUdpSocketServers = new ArrayList<>();
|
|
|
private int packetSeq = 0;
|
|
private int packetSeq = 0;
|
|
|
private int sendLcNoCnt = 0;
|
|
private int sendLcNoCnt = 0;
|
|
|
|
|
|
|
|
- public TsinfoFileWorker(GgitsRegion center, String filePath, String absoluteFilePath) throws IOException {
|
|
|
|
|
|
|
+ public TsinfoFileWorker(GgitsRegion center, String filePath, String absoluteFilePath, List<String> ggitsUdpServers) throws IOException {
|
|
|
this.service = FileSystems.getDefault().newWatchService();
|
|
this.service = FileSystems.getDefault().newWatchService();
|
|
|
this.center = center;
|
|
this.center = center;
|
|
|
this.filePath = filePath;
|
|
this.filePath = filePath;
|
|
|
this.absolutePath = absoluteFilePath;
|
|
this.absolutePath = absoluteFilePath;
|
|
|
|
|
+ this.ggitsUdpServers = ggitsUdpServers;
|
|
|
|
|
|
|
|
Path watchPath = FileSystems.getDefault().getPath(absoluteFilePath);
|
|
Path watchPath = FileSystems.getDefault().getPath(absoluteFilePath);
|
|
|
watchPath.register(this.service,
|
|
watchPath.register(this.service,
|
|
@@ -50,16 +50,21 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
|
|
|
|
|
private boolean initUdpChannel() {
|
|
private boolean initUdpChannel() {
|
|
|
try {
|
|
try {
|
|
|
- this.datagramSocket = new DatagramSocket();
|
|
|
|
|
- try {
|
|
|
|
|
- this.serverAddress = InetAddress.getByName(this.center.getServerIp());
|
|
|
|
|
- }
|
|
|
|
|
- catch (UnknownHostException e) {
|
|
|
|
|
- log.error("{}", e.getMessage());
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ for (String serverIp : this.ggitsUdpServers) {
|
|
|
|
|
+ UdpServerInfo server = new UdpServerInfo();
|
|
|
|
|
+ server.setServerIp(serverIp);
|
|
|
|
|
+ server.setPort(this.center.getCommPort());
|
|
|
|
|
+ server.setDatagramSocket(new DatagramSocket());
|
|
|
|
|
+ try {
|
|
|
|
|
+ server.setIpAddress(InetAddress.getByName(serverIp));
|
|
|
|
|
+ server.getDatagramSocket().connect(server.getIpAddress(), this.center.getCommPort());
|
|
|
|
|
+ this.ggitsUdpSocketServers.add(server);
|
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
|
+ log.error("{}: {}", server, e.getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- this.datagramSocket.connect(this.serverAddress, this.center.getServerPort());
|
|
|
|
|
- } catch (SocketException e) {
|
|
|
|
|
|
|
+ } catch(SocketException e){
|
|
|
log.error("{}", e.getMessage());
|
|
log.error("{}", e.getMessage());
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
@@ -194,7 +199,7 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
// 데이터 길이 계산 (2바이트)
|
|
// 데이터 길이 계산 (2바이트)
|
|
|
short dataLength = (short) bytesRead;
|
|
short dataLength = (short) bytesRead;
|
|
|
- short count = (short)(dataLength / 10);
|
|
|
|
|
|
|
+ short count = (short) (dataLength / 10);
|
|
|
this.sendLcNoCnt += count;
|
|
this.sendLcNoCnt += count;
|
|
|
this.packetSeq++;
|
|
this.packetSeq++;
|
|
|
|
|
|
|
@@ -219,7 +224,7 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
|
|
|
|
|
// Packet Frame Tail
|
|
// Packet Frame Tail
|
|
|
ByteBuf tail = Unpooled.buffer(2);
|
|
ByteBuf tail = Unpooled.buffer(2);
|
|
|
- tail.writeByte((byte)0x00);
|
|
|
|
|
|
|
+ tail.writeByte((byte) 0x00);
|
|
|
tail.writeByte(SigProtocolConst.SIG_ETX);
|
|
tail.writeByte(SigProtocolConst.SIG_ETX);
|
|
|
|
|
|
|
|
// Packet Frame (Head + Data + Tail)
|
|
// Packet Frame (Head + Data + Tail)
|
|
@@ -229,8 +234,16 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
int length = packet.readableBytes();
|
|
int length = packet.readableBytes();
|
|
|
byte[] bytes = new byte[length];
|
|
byte[] bytes = new byte[length];
|
|
|
packet.getBytes(packet.readerIndex(), bytes);
|
|
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);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 등록된 통신서버 모두에 차례로 전송
|
|
|
|
|
+ for (UdpServerInfo udpServer : this.ggitsUdpSocketServers) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length, udpServer.getIpAddress(), this.center.getCommPort());
|
|
|
|
|
+ udpServer.getDatagramSocket().send(sendPacket);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("Send Error: {}.{}: IOException: {}", udpServer.getServerIp(), udpServer.getPort(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
if (this.center.isDump()) {
|
|
if (this.center.isDump()) {
|
|
|
//log.info("Packet Frame Sent: Data {} EA, Total {} Bytes.", count, length);
|
|
//log.info("Packet Frame Sent: Data {} EA, Total {} Bytes.", count, length);
|
|
|
log.info("SEND: [{}]. {} Bytes. {}", center.getLogKey(), bytes.length, SysUtils.byteArrayToHex(bytes));
|
|
log.info("SEND: [{}]. {} Bytes. {}", center.getLogKey(), bytes.length, SysUtils.byteArrayToHex(bytes));
|
|
@@ -239,11 +252,11 @@ public class TsinfoFileWorker implements Runnable, AutoCloseable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
catch (FileNotFoundException e) {
|
|
catch (FileNotFoundException e) {
|
|
|
- log.warn("{}, {}, {}, File Not Found. {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
|
|
|
|
+ log.error("{}, {}, {}, File Not Found. {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
catch (IOException e) {
|
|
catch (IOException e) {
|
|
|
- log.warn("{}, {}, {}, IOException: {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
|
|
|
|
+ log.error("{}, {}, {}, IOException: {}", this.center.getRegionCd(), this.filePath, absoluteFilePath, e.getMessage());
|
|
|
return calDataCount;
|
|
return calDataCount;
|
|
|
}
|
|
}
|
|
|
return calDataCount;
|
|
return calDataCount;
|