blob: 655600980fea3bbd16d1d687e580996a94a28962 [file] [log] [blame]
TRM-coding24b60182025-06-09 21:19:45 +08001package tracker;
2
3import java.util.concurrent.Executors;
4import java.util.concurrent.ScheduledExecutorService;
5import java.util.concurrent.TimeUnit;
6import cheat.Cheat;
7
8/**
9 * 作弊检测定时任务调度器
10 * 管理两个定时任务:
11 * 1. 每20秒检测假种子
12 * 2. 每1分钟检测传输异常
13 */
14public class CheatDetectionScheduler {
15
16 private final ScheduledExecutorService scheduler;
17 private final Cheat cheat;
18 private volatile boolean isRunning = false;
19
20 public CheatDetectionScheduler() {
21 this.scheduler = Executors.newScheduledThreadPool(2, r -> {
22 Thread t = new Thread(r);
23 t.setDaemon(true); // 设置为守护线程
24 return t;
25 });
26 this.cheat = new Cheat();
27 }
28
29 /**
30 * 启动定时任务
31 */
32 public void start() {
33 if (isRunning) {
34 System.out.println("CheatDetectionScheduler is already running");
35 return;
36 }
37
38 System.out.println("Starting CheatDetectionScheduler...");
39
40 // 启动假种子检测任务 - 每20秒执行一次
41 scheduler.scheduleAtFixedRate(
42 this::runFakeSeedDetection,
43 10, // 初始延迟10秒
44 20, // 每20秒执行一次
45 TimeUnit.SECONDS
46 );
47
48 // 启动传输异常检测任务 - 每1分钟执行一次
49 scheduler.scheduleAtFixedRate(
50 this::runTransDetection,
51 30, // 初始延迟30秒
52 60, // 每60秒执行一次
53 TimeUnit.SECONDS
54 );
55
56 isRunning = true;
57 System.out.println("CheatDetectionScheduler started successfully");
58 }
59
60 /**
61 * 停止定时任务
62 */
63 public void stop() {
64 if (!isRunning) {
65 return;
66 }
67
68 System.out.println("Stopping CheatDetectionScheduler...");
69 scheduler.shutdown();
70
71 try {
72 if (!scheduler.awaitTermination(10, TimeUnit.SECONDS)) {
73 scheduler.shutdownNow();
74 if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
75 System.err.println("CheatDetectionScheduler did not terminate gracefully");
76 }
77 }
78 } catch (InterruptedException e) {
79 scheduler.shutdownNow();
80 Thread.currentThread().interrupt();
81 }
82
83 isRunning = false;
84 System.out.println("CheatDetectionScheduler stopped");
85 }
86
87 /**
88 * 执行假种子检测
89 */
90 private void runFakeSeedDetection() {
91 try {
92 System.out.println("[" + new java.util.Date() + "] Running fake seed detection...");
93 cheat.DetectFakeSeed();
94 System.out.println("[" + new java.util.Date() + "] Fake seed detection completed");
95 } catch (Exception e) {
96 System.err.println("[" + new java.util.Date() + "] Error in fake seed detection: " + e.getMessage());
97 e.printStackTrace();
98 }
99 }
100
101 /**
102 * 执行传输异常检测
103 */
104 private void runTransDetection() {
105 try {
106 System.out.println("[" + new java.util.Date() + "] Running transaction detection...");
107 cheat.DetectTrans();
108 System.out.println("[" + new java.util.Date() + "] Transaction detection completed");
109 } catch (Exception e) {
110 System.err.println("[" + new java.util.Date() + "] Error in transaction detection: " + e.getMessage());
111 e.printStackTrace();
112 }
113 }
114
115 /**
116 * 检查调度器是否正在运行
117 */
118 public boolean isRunning() {
119 return isRunning && !scheduler.isShutdown();
120 }
121}