From c366f3a6551fb441cffe53110721ebd17a701ebe Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 2 May 2019 15:31:40 +0400 Subject: [PATCH] Implemented ad-hoc part --- build.gradle | 2 + .../io/github/chronosx88/GunJava/Dup.java | 50 +++++++++++++++++++ .../io/github/chronosx88/GunJava/DupOpt.java | 6 +++ .../io/github/chronosx88/GunJava/Main.java | 5 +- .../io/github/chronosx88/GunJava/Server.java | 11 +++- .../io/github/chronosx88/GunJava/Utils.java | 17 +++++++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/github/chronosx88/GunJava/Dup.java create mode 100644 src/main/java/io/github/chronosx88/GunJava/DupOpt.java create mode 100644 src/main/java/io/github/chronosx88/GunJava/Utils.java diff --git a/build.gradle b/build.gradle index f467acb..ba76629 100644 --- a/build.gradle +++ b/build.gradle @@ -13,5 +13,7 @@ repositories { dependencies { implementation 'org.java-websocket:Java-WebSocket:1.4.0' + implementation group: 'org.json', name: 'json', version: '20180813' + testCompile group: 'junit', name: 'junit', version: '4.12' } diff --git a/src/main/java/io/github/chronosx88/GunJava/Dup.java b/src/main/java/io/github/chronosx88/GunJava/Dup.java new file mode 100644 index 0000000..d033851 --- /dev/null +++ b/src/main/java/io/github/chronosx88/GunJava/Dup.java @@ -0,0 +1,50 @@ +package io.github.chronosx88.GunJava; + +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; + +public class Dup { + private static char[] randomPack = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + private static Random random = new Random(System.currentTimeMillis()); + private Map s = new ConcurrentHashMap<>(); + private DupOpt opt = new DupOpt(); + private Thread to = null; + + public Dup() { + opt.max = 1000; + opt.age = 1000 * 9; + } + + public String track(String id) { + s.put(id, System.currentTimeMillis()); + if(to == null) { + Utils.setTimeout(() -> { + for(Map.Entry entry : s.entrySet()) { + if(opt.age > (System.currentTimeMillis() - entry.getValue())) + continue; + s.remove(entry.getKey()); + } + to = null; + }, opt.age); + } + return id; + } + + public boolean check(String id) { + if(s.containsKey(id)) { + track(id); + return true; + } else { + return false; + } + } + + public String random() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < randomPack.length; i++) { + sb.append(randomPack[random.nextInt(randomPack.length)]); + } + return sb.toString(); + } +} diff --git a/src/main/java/io/github/chronosx88/GunJava/DupOpt.java b/src/main/java/io/github/chronosx88/GunJava/DupOpt.java new file mode 100644 index 0000000..029a9b3 --- /dev/null +++ b/src/main/java/io/github/chronosx88/GunJava/DupOpt.java @@ -0,0 +1,6 @@ +package io.github.chronosx88.GunJava; + +public class DupOpt { + public int max; + public int age; +} diff --git a/src/main/java/io/github/chronosx88/GunJava/Main.java b/src/main/java/io/github/chronosx88/GunJava/Main.java index c6cea04..3a647ad 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Main.java +++ b/src/main/java/io/github/chronosx88/GunJava/Main.java @@ -2,6 +2,7 @@ package io.github.chronosx88.GunJava; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; +import org.json.JSONObject; import java.net.URI; import java.net.URISyntaxException; @@ -18,7 +19,9 @@ public class Main { @Override public void onMessage(String message) { - System.out.println(message); + JSONObject msg = new JSONObject(message); + System.out.println(msg); + this.send(message); } @Override diff --git a/src/main/java/io/github/chronosx88/GunJava/Server.java b/src/main/java/io/github/chronosx88/GunJava/Server.java index c9cac51..f1a584b 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Server.java +++ b/src/main/java/io/github/chronosx88/GunJava/Server.java @@ -3,6 +3,7 @@ package io.github.chronosx88.GunJava; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; +import org.json.JSONObject; import java.net.InetSocketAddress; import java.util.Timer; @@ -10,6 +11,7 @@ import java.util.TimerTask; public class Server extends WebSocketServer { private Timer timer = new Timer(true); + private Dup dup = new Dup(); public Server(int port) { super(new InetSocketAddress(port)); @@ -22,7 +24,9 @@ public class Server extends WebSocketServer { @Override public void run() { count[0] += 1; - conn.send("hello world " + count[0]); + JSONObject msg = new JSONObject(); + msg.put("#", dup.track(String.valueOf(count[0]))); + conn.send(msg.toString()); } }, 0, 1000); } @@ -34,7 +38,10 @@ public class Server extends WebSocketServer { @Override public void onMessage(WebSocket conn, String message) { - System.out.println("received: " + message); + JSONObject msg = new JSONObject(message); + //if(dup.check(msg.getString("#"))) { return; } + dup.track(msg.getString("#")); + System.out.println("received: " + msg.toString()); } @Override diff --git a/src/main/java/io/github/chronosx88/GunJava/Utils.java b/src/main/java/io/github/chronosx88/GunJava/Utils.java new file mode 100644 index 0000000..14d87cc --- /dev/null +++ b/src/main/java/io/github/chronosx88/GunJava/Utils.java @@ -0,0 +1,17 @@ +package io.github.chronosx88.GunJava; + +public class Utils { + public static Thread setTimeout(Runnable runnable, int delay){ + Thread thread = new Thread(() -> { + try { + Thread.sleep(delay); + runnable.run(); + } + catch (Exception e){ + e.printStackTrace(); + } + }); + thread.start(); + return thread; + } +}