From 129cde4ad5e08c8c5925816bfaf473a25b43c6b0 Mon Sep 17 00:00:00 2001 From: coco3x Date: Sat, 30 Nov 2024 02:01:38 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=EC=84=B8=EC=8A=A4=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log_writer/common/process/ContinuousProcess.java | 11 +++++++++++ .../log_writer/common/process/TransientProcess.java | 12 ++++++++++++ .../log_writer/managed/arp/ArpLogProcess.java | 9 +++++---- .../whoz_in/log_writer/managed/arp/ArpLogWriter.java | 2 +- .../log_writer/managed/mdns/MdnsLogProcess.java | 7 +++++-- .../log_writer/monitor/MonitorLogProcess.java | 8 ++++++-- 6 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java create mode 100644 modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java new file mode 100644 index 00000000..f7125f5c --- /dev/null +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java @@ -0,0 +1,11 @@ +package com.whoz_in.log_writer.common.process; + +//실행 후 종료되지 않는 프로세스 +//꾸준히 출력을 읽을 수 있어야 한다. +public abstract class ContinuousProcess { + + //출력이 없을 땐 블로킹되면 안된다. + public abstract String readLine(); + public abstract boolean isAlive(); + public abstract void terminate(); +} diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java new file mode 100644 index 00000000..8e319389 --- /dev/null +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java @@ -0,0 +1,12 @@ +package com.whoz_in.log_writer.common.process; + +import java.util.List; + +//실행 후 종료되어 모든 출력을 얻는 프로세스 +//출력 스트림을 통한 프로세스와의 상호작용은 없다. +public abstract class TransientProcess { + + //종료되었을 때 출력을 얻는다. + //종료되지 않았다면 블로킹된다. + public abstract List results(); +} diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java index 72fbbbc5..5ad1fe7a 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java @@ -1,5 +1,6 @@ package com.whoz_in.log_writer.managed.arp; +import com.whoz_in.log_writer.common.process.TransientProcess; import com.whoz_in.log_writer.managed.ManagedInfo; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -10,8 +11,7 @@ import java.util.ArrayList; import java.util.List; -//단발성 프로세스 -public class ArpLogProcess { +public class ArpLogProcess extends TransientProcess { private final BufferedReader br; private final Process process; public ArpLogProcess(ManagedInfo info, String password) { @@ -31,8 +31,9 @@ public ArpLogProcess(ManagedInfo info, String password) { } } - //단발성 프로세스로, 프로세스가 종료될 때까지 run은 블로킹된다. - public List readLines(){ + + @Override + public List results(){ List logs = new ArrayList<>(); try { diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java index 13426f5f..5730f7dc 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java @@ -36,7 +36,7 @@ private void scan() { List logs= arpList.stream() .flatMap(arpInfo-> { ArpLogProcess proc = new ArpLogProcess(arpInfo, sudoPassword); //프로세스 실행 - List lines = proc.readLines(); //프로세스의 모든 출력 가져오기 + List lines = proc.results(); //프로세스의 모든 출력 가져오기 Set procLogs = lines.stream() //출력 라인들을 ManagedLog 변환하며 ssid도 넣어줌 .filter(parser::validate) .map(line->{ diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java index 0af2d9f6..f6c4e4e9 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java @@ -1,5 +1,6 @@ package com.whoz_in.log_writer.managed.mdns; +import com.whoz_in.log_writer.common.process.ContinuousProcess; import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader; import com.whoz_in.log_writer.managed.ManagedInfo; import java.io.BufferedReader; @@ -9,7 +10,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; -public class MdnsLogProcess { +public class MdnsLogProcess extends ContinuousProcess { private final Process process; private final NonBlockingBufferedReader cbr; @@ -33,8 +34,8 @@ public MdnsLogProcess(ManagedInfo info, String sudoPassword) { * @return 프로세스의 출력에서 한 줄을 읽어들인다. * 읽을 줄이 없을경우 null을 출력한다. */ + @Override public String readLine(){ - //여기서 try catch try { return this.cbr.readLine(); } catch (IOException e) { @@ -42,10 +43,12 @@ public String readLine(){ } } + @Override public boolean isAlive(){ return this.process.isAlive(); } + @Override public void terminate(){ this.process.destroy(); } diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java index 1228bfce..431f4e8b 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java @@ -1,5 +1,6 @@ package com.whoz_in.log_writer.monitor; +import com.whoz_in.log_writer.common.process.ContinuousProcess; import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader; import java.io.BufferedReader; import java.io.File; @@ -8,7 +9,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; -public final class MonitorLogProcess { +public final class MonitorLogProcess extends ContinuousProcess { private final Process process; private final NonBlockingBufferedReader cbr; @@ -28,6 +29,7 @@ public MonitorLogProcess(MonitorInfo info, String sudoPassword) { } } + @Override public String readLine(){ try { return cbr.readLine(); @@ -36,11 +38,13 @@ public String readLine(){ } } + @Override public boolean isAlive(){ return this.process.isAlive(); } - public void destory(){ + @Override + public void terminate(){ this.process.destroy(); } } From 08a98e56bbcf1b73131c0db9d1baa6dd645ee178 Mon Sep 17 00:00:00 2001 From: coco3x Date: Sat, 30 Nov 2024 02:03:42 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=EB=A1=9C=EA=B9=85=EC=9D=84=20?= =?UTF-8?q?=EC=A3=BC=EA=B8=B0=EB=A7=88=EB=8B=A4=20=EC=8B=A4=ED=96=89=20->?= =?UTF-8?q?=20=EA=B0=84=EA=B2=A9=EC=9D=84=20=EB=91=90=EA=B3=A0=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 중복 실행 가능성을 제거 --- .../java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java | 2 +- .../java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java | 2 +- .../java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java index 5730f7dc..b010179c 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java @@ -31,7 +31,7 @@ public ArpLogWriter(ManagedLogDAO dao, this.sudoPassword = sudoPassword; } - @Scheduled(initialDelay = 10000, fixedRate = 5000) + @Scheduled(initialDelay = 10000, fixedDelay = 5000) private void scan() { List logs= arpList.stream() .flatMap(arpInfo-> { diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java index e09a9e09..495707ac 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java @@ -30,7 +30,7 @@ public MdnsLogWriter(ManagedLogDAO dao, MdnsLogParser parser, NetworkConfig conf )); } - @Scheduled(initialDelay = 10000, fixedRate = 10000) + @Scheduled(initialDelay = 10000, fixedDelay = 10000) private void writeLogs() { List totalLogs = this.processes.entrySet().parallelStream() .filter(entry-> { diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java index 497145f7..34af0ff1 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java @@ -24,7 +24,7 @@ public MonitorLogWriter(MonitorLogParser parser, MonitorLogDAO repo, NetworkConf this.sudoPassword = sudoPassword; this.process = new MonitorLogProcess(monitorInfo, sudoPassword); } - @Scheduled(initialDelay = 10000, fixedRate = 10000) + @Scheduled(initialDelay = 10000, fixedDelay = 10000) private void saveLogs(){ if (!process.isAlive()) { System.err.println("[monitor] 종료됨 : ERROR"); From 0e947ef59c4cc1cbd16a211dc7b091669ad6b86d Mon Sep 17 00:00:00 2001 From: coco3x Date: Sat, 30 Nov 2024 02:04:54 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=EC=93=B8=EB=8D=B0=20=EC=97=86?= =?UTF-8?q?=EB=8A=94=20Set=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java index 34af0ff1..8c7c9fbb 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java @@ -41,7 +41,6 @@ private void saveLogs(){ System.out.println("[monitor] 저장할 mac 개수: " + macs.size()); repo.upsertAll(macs); - macs.clear(); } } \ No newline at end of file From 2680e18268d19c03d572c48251aa9a2681783db4 Mon Sep 17 00:00:00 2001 From: coco3x Date: Sat, 30 Nov 2024 02:09:24 +0900 Subject: [PATCH 4/5] =?UTF-8?q?style:=20insertAll()=20->=20upsertAll()=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/whoz_in/log_writer/managed/ManagedLogDAO.java | 2 +- .../java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java | 2 +- .../java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/ManagedLogDAO.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/ManagedLogDAO.java index 2ac2e30c..cbc6b642 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/ManagedLogDAO.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/ManagedLogDAO.java @@ -17,7 +17,7 @@ public final class ManagedLogDAO { private final JdbcTemplate jdbcTemplate; - public void insertAll(Collection logs) { + public void upsertAll(Collection logs) { if (logs.isEmpty()) return; List logList = logs.stream().toList(); diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java index b010179c..6363a43e 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java @@ -60,6 +60,6 @@ private void scan() { }) .toList(); - dao.insertAll(logs); + dao.upsertAll(logs); } } diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java index 495707ac..891c992d 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java @@ -60,7 +60,7 @@ private void writeLogs() { }) .toList(); - dao.insertAll(totalLogs); + dao.upsertAll(totalLogs); } } From 65ba2a573be4389e756c23d0ed49ba662d46c80b Mon Sep 17 00:00:00 2001 From: coco3x Date: Sat, 30 Nov 2024 02:13:18 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20monitor=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EC=84=B8=EC=8A=A4=20=EC=8B=A4=ED=96=89=EC=97=90=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=A0=95=EB=B3=B4=EC=97=90=20=EB=A7=9E?= =?UTF-8?q?=EA=B2=8C=20VO=20=EC=9E=AC=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/whoz_in/log_writer/config/NetworkConfig.java | 4 +--- .../com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java | 1 - .../main/java/com/whoz_in/log_writer/monitor/MonitorInfo.java | 3 +-- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/config/NetworkConfig.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/config/NetworkConfig.java index 8b016db6..fa3747cb 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/config/NetworkConfig.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/config/NetworkConfig.java @@ -44,9 +44,7 @@ public NetworkConfig(@Value("${spring.profiles.active:default}") String profile, //monitor Map monitorMap = (Map) map.get("monitor"); this.monitorInfo = new MonitorInfo( - monitorMap.get("interface"), - generateCommand(monitorMap.get("command"), monitorMap.get("interface")) - ); + generateCommand(monitorMap.get("command"), monitorMap.get("interface"))); // managed Map managedMap = (Map) map.get("managed"); // mdns diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java index f6c4e4e9..45ac0263 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java @@ -14,7 +14,6 @@ public class MdnsLogProcess extends ContinuousProcess { private final Process process; private final NonBlockingBufferedReader cbr; - //TODO: Info로 변경 public MdnsLogProcess(ManagedInfo info, String sudoPassword) { try { new File("../error").mkdir(); //에러 처리 수정하면 이거 없앨게요.. diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorInfo.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorInfo.java index 9771078b..9b3b6fdb 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorInfo.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorInfo.java @@ -1,5 +1,4 @@ package com.whoz_in.log_writer.monitor; -//TODO: interfaceName 제거 //Monitor 프로세스를 생성하고 처리하는 과정에서 필요한 정보를 담는다. -public record MonitorInfo(String interfaceName, String command) {} \ No newline at end of file +public record MonitorInfo(String command) {} \ No newline at end of file