mirror of
https://github.com/ChronosX88/JGUN.git
synced 2024-11-25 07:42:18 +00:00
Implemented ad-hoc part
This commit is contained in:
parent
01e9e53480
commit
c366f3a655
@ -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'
|
||||
}
|
||||
|
50
src/main/java/io/github/chronosx88/GunJava/Dup.java
Normal file
50
src/main/java/io/github/chronosx88/GunJava/Dup.java
Normal file
@ -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<String, Long> 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<String, Long> 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();
|
||||
}
|
||||
}
|
6
src/main/java/io/github/chronosx88/GunJava/DupOpt.java
Normal file
6
src/main/java/io/github/chronosx88/GunJava/DupOpt.java
Normal file
@ -0,0 +1,6 @@
|
||||
package io.github.chronosx88.GunJava;
|
||||
|
||||
public class DupOpt {
|
||||
public int max;
|
||||
public int age;
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
17
src/main/java/io/github/chronosx88/GunJava/Utils.java
Normal file
17
src/main/java/io/github/chronosx88/GunJava/Utils.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user