diff options
author | syeyoung <cyoung06@naver.com> | 2022-05-21 21:18:14 +0900 |
---|---|---|
committer | syeyoung <cyoung06@naver.com> | 2022-05-21 21:28:52 +0900 |
commit | 20dd3f99a7b139b5848128246c622fd9cfefa478 (patch) | |
tree | 78e5f84ad22fd53876d488f6b58c3528aebe6501 /src/main/java/kr/syeyoung/dungeonsguide/stomp | |
parent | 50de034c046c4ddea033b73793c8825ecb5bb86f (diff) | |
download | Skyblock-Dungeons-Guide-20dd3f99a7b139b5848128246c622fd9cfefa478.tar.gz Skyblock-Dungeons-Guide-20dd3f99a7b139b5848128246c622fd9cfefa478.tar.bz2 Skyblock-Dungeons-Guide-20dd3f99a7b139b5848128246c622fd9cfefa478.zip |
- Project separation
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide/stomp')
8 files changed, 0 insertions, 443 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/CloseListener.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/CloseListener.java deleted file mode 100644 index fdb177a3..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/CloseListener.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -public interface CloseListener { - void onClose(int code, String reason, boolean remote); -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java deleted file mode 100644 index 7226c1d6..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -import lombok.Getter; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; -import org.java_websocket.server.DefaultSSLWebSocketServerFactory; -import sun.security.ssl.SSLSocketFactoryImpl; - -import javax.net.ssl.*; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.Socket; -import java.net.URI; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class StompClient extends WebSocketClient implements StompInterface { - public StompClient(URI serverUri, final String token, CloseListener closeListener) throws Exception { - super(serverUri); - this.closeListener = closeListener; - addHeader("Authorization", token); - - System.out.println("connecting websocket"); - if (!connectBlocking()) { - throw new RuntimeException("Can't connect to ws"); - } - System.out.println("connected, stomp handshake"); - while(this.stompClientStatus == StompClientStatus.CONNECTING); - System.out.println("fully connected"); - } - private final CloseListener closeListener; - - @Getter - private volatile StompClientStatus stompClientStatus = StompClientStatus.CONNECTING; - - @Getter - private StompPayload errorPayload; - - private ScheduledFuture heartbeat = null; - - private static final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1); - - @Override - public void onOpen(ServerHandshake handshakedata) { - send(new StompPayload().method(StompHeader.CONNECT) - .header("accept-version","1.2") - .header("heart-beat", "30000,30000") - .header("host",uri.getHost()).getBuilt() - ); - } - - @Override - public void onMessage(String message) { - try { - StompPayload payload = StompPayload.parse(message); - if (payload.method() == StompHeader.CONNECTED) { - stompClientStatus = StompClientStatus.CONNECTED; - - String heartbeat = payload.headers().get("heart-beat"); - if (heartbeat != null) { -// int sx = Integer.parseInt(heartbeat.split(",")[0]); -// int sy = Integer.parseInt(heartbeat.split(",")[1]); -// -// if (sy == 0) return; - int heartbeatMS = 30000; - this.heartbeat = ex.scheduleAtFixedRate(() -> { - send("\n"); - }, heartbeatMS-1000, heartbeatMS-1000, TimeUnit.MILLISECONDS); - } - - } else if (payload.method() == StompHeader.ERROR) { - errorPayload = payload; - stompClientStatus = StompClientStatus.ERROR; - this.close(); - } else if (payload.method() == StompHeader.MESSAGE) { - // mesage - StompSubscription stompSubscription = stompSubscriptionMap.get(Integer.parseInt(payload.headers().get("subscription"))); - try { - stompSubscription.getStompMessageHandler().handle(this, payload); - if (stompSubscription.getAckMode() != StompSubscription.AckMode.AUTO) { - send(new StompPayload().method(StompHeader.ACK) - .header("id",payload.headers().get("ack")).getBuilt() - ); - } - } catch (Exception e) { - e.printStackTrace(); - if (stompSubscription.getAckMode() != StompSubscription.AckMode.AUTO) { - send(new StompPayload().method(StompHeader.NACK) - .header("id",payload.headers().get("ack")).getBuilt() - ); - } - } - } else if (payload.method() == StompHeader.RECEIPT) { - String receipt_id = payload.headers().get("receipt-id"); - StompPayload payload1 = receiptMap.remove(Integer.parseInt(receipt_id)); - if (payload1.method() == StompHeader.DISCONNECT) { - stompClientStatus = StompClientStatus.DISCONNECTED; - close(); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - public void onClose(int code, String reason, boolean remote) { - if (heartbeat != null) heartbeat.cancel(true); - closeListener.onClose(code, reason, remote); - } - - @Override - public void onError(Exception ex) { - ex.printStackTrace(); - } - - private final Map<Integer, StompSubscription> stompSubscriptionMap = new HashMap<Integer, StompSubscription>(); - private final Map<Integer, StompPayload> receiptMap = new HashMap<Integer, StompPayload>(); - - private int idIncrement = 0; - - @Override - public void send(StompPayload payload) { - if (stompClientStatus != StompClientStatus.CONNECTED) throw new IllegalStateException("not connected"); - payload.method(StompHeader.SEND); - if (payload.headers().get("receipt") != null) - receiptMap.put(Integer.parseInt(payload.headers().get("receipt")), payload); - send(payload.getBuilt()); - } - - @Override - public void subscribe(StompSubscription stompSubscription) { - if (stompClientStatus != StompClientStatus.CONNECTED) throw new IllegalStateException("not connected"); - stompSubscription.setId(++idIncrement); - - send(new StompPayload().method(StompHeader.SUBSCRIBE) - .header("id",String.valueOf(stompSubscription.getId())) - .header("destination", stompSubscription.getDestination()) - .header("ack", stompSubscription.getAckMode().getValue()).getBuilt() - ); - - stompSubscriptionMap.put(stompSubscription.getId(), stompSubscription); - } - - @Override - public void unsubscribe(StompSubscription stompSubscription) { - if (stompClientStatus != StompClientStatus.CONNECTED) throw new IllegalStateException("not connected"); - send(new StompPayload().method(StompHeader.UNSUBSCRIBE) - .header("id",String.valueOf(stompSubscription.getId())).getBuilt() - ); - stompSubscriptionMap.remove(stompSubscription.getId()); - } - - @Override - public void disconnect() { - if (stompClientStatus != StompClientStatus.CONNECTED) throw new IllegalStateException("not connected"); - StompPayload stompPayload; - stompClientStatus =StompClientStatus.DISCONNECTING; - send((stompPayload = new StompPayload().method(StompHeader.DISCONNECT) - .header("receipt", String.valueOf(++idIncrement))) - .getBuilt() - ); - receiptMap.put(idIncrement, stompPayload); - } -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClientStatus.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClientStatus.java deleted file mode 100644 index ef3f907c..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClientStatus.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -public enum StompClientStatus { - CONNECTING, CONNECTED, ERROR, DISCONNECTING, DISCONNECTED -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompHeader.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompHeader.java deleted file mode 100644 index f0b7a06b..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompHeader.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.Getter; - -public enum StompHeader { - SEND, SUBSCRIBE, UNSUBSCRIBE, BEGIN, COMMIT, ABORT, ACK, NACK, DISCONNECT, CONNECT, STOMP, CONNECTED, MESSAGE, RECEIPT, ERROR -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompInterface.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompInterface.java deleted file mode 100644 index 8244fd58..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompInterface.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -public interface StompInterface { - void send(StompPayload payload); - void subscribe(StompSubscription stompSubscription); - void unsubscribe(StompSubscription stompSubscription); - void disconnect(); -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompMessageHandler.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompMessageHandler.java deleted file mode 100644 index 8b3efd77..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompMessageHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -public interface StompMessageHandler { - void handle(StompInterface stompInterface, StompPayload stompPayload); -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompPayload.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompPayload.java deleted file mode 100644 index 27b1cdc9..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompPayload.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -import kr.syeyoung.dungeonsguide.features.FeatureRegistry; -import lombok.Data; -import lombok.Singular; -import lombok.experimental.Accessors; - -import java.util.*; - -@Data -@Accessors(chain = true, fluent = true) -public class StompPayload { - private StompHeader method; - private Map<String, String> headers = new HashMap<String, String>(); - private String payload; - - public StompPayload header(String key, String value) { - headers.put(key, value); - return this; - } - - public String getBuilt() { - StringBuilder sb = new StringBuilder(); - sb.append(method.name()); - sb.append("\n"); - for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) { - sb.append(stringStringEntry.getKey()); - sb.append(":"); - sb.append(stringStringEntry.getValue()); - sb.append("\n"); - if (stringStringEntry.getKey().contains(":")) throw new IllegalStateException("Illegal Character : inside headers"); - if (stringStringEntry.getValue().contains(":")) throw new IllegalStateException("Illegal Character : inside headers"); - } - sb.append("\n"); - if (payload != null) - sb.append(payload); - sb.append((char) 0); - if (FeatureRegistry.DEBUG.isEnabled()) System.out.println("Sending.. "+sb.toString()); - return sb.toString(); - } - - public static StompPayload parse(String payload) { - if (FeatureRegistry.DEBUG.isEnabled()) System.out.println("Receving.. "+payload); - - Scanner scanner = new Scanner(payload); - StompPayload stompPayload = new StompPayload(); - stompPayload.method = StompHeader.valueOf(scanner.nextLine()); - String line = ""; - while (!(line = scanner.nextLine()).isEmpty()) { - int index = line.indexOf(":"); - if (index == -1) throw new IllegalArgumentException("No : found in headers section"); - String name = line.substring(0, index); - String value; - if (index == line.length() - 1) - value = ""; - else - value = line.substring(index+1); - stompPayload.headers.put(name, value); - } - - List<String> lines = new ArrayList<String>(); - while (scanner.hasNextLine() && !(line = scanner.nextLine()).equals("\0")) { - lines.add(line); - } - stompPayload.payload = String.join("\n", lines); - return stompPayload; - } -} diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompSubscription.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompSubscription.java deleted file mode 100644 index 790ba344..00000000 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompSubscription.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.stomp; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.Getter; - -@Data -@Builder -public class StompSubscription { - private int id; - private String destination; - private StompMessageHandler stompMessageHandler; - private AckMode ackMode; - - @AllArgsConstructor - public enum AckMode { - AUTO("auto"), CLIENT("client"), CLIENT_INDIVIDUAL("client-individual"); - - @Getter - private final String value; - } -} |