From c9ed0f14cbfb54d683c9a19fd3519e12081d4331 Mon Sep 17 00:00:00 2001 From: Peijun Ma Date: Mon, 9 Oct 2023 02:18:06 -0400 Subject: [PATCH] Use ebs client to broadcast messages --- .../str_exporter/SlayTheRelicsExporter.java | 8 ++-- .../client/BackendBroadcaster.java | 38 ++----------------- .../java/str_exporter/client/EBSClient.java | 35 +++++++++++------ src/main/java/str_exporter/config/Config.java | 2 + 4 files changed, 33 insertions(+), 50 deletions(-) diff --git a/src/main/java/str_exporter/SlayTheRelicsExporter.java b/src/main/java/str_exporter/SlayTheRelicsExporter.java index 8505ac3..1e7308e 100644 --- a/src/main/java/str_exporter/SlayTheRelicsExporter.java +++ b/src/main/java/str_exporter/SlayTheRelicsExporter.java @@ -154,9 +154,9 @@ private User getOauthToken(String state) { @Override public void receivePostInitialize() { - tipsBroadcaster = new BackendBroadcaster(config, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); - deckBroadcaster = new BackendBroadcaster(config, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); - okayBroadcaster = new BackendBroadcaster(config, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, true); + tipsBroadcaster = new BackendBroadcaster(config, ebsClient, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); + deckBroadcaster = new BackendBroadcaster(config, ebsClient, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); + okayBroadcaster = new BackendBroadcaster(config, ebsClient, BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, true); tipsJsonBuilder = new TipsJSONBuilder(config.getUser(), config.getOathToken(), version); deckJsonBuilder = new DeckJSONBuilder(config.getUser(), config.getOathToken(), version); okayJsonBuilder = new JSONMessageBuilder(config.getUser(), config.getOathToken(), version, 5); @@ -271,7 +271,7 @@ public void receivePostRender(SpriteBatch spriteBatch) { } } - long lastSuccessAuth = EBSClient.lastSuccessAuth.get(); + long lastSuccessAuth = EBSClient.lastSuccessRequest.get(); long lastSuccessBroadcast = okayBroadcaster.lastSuccessBroadcast.get(); long lastSuccess = Math.max(lastSuccessAuth, lastSuccessBroadcast); if (this.inProgress.get()) { diff --git a/src/main/java/str_exporter/client/BackendBroadcaster.java b/src/main/java/str_exporter/client/BackendBroadcaster.java index da2a945..9c11cdb 100644 --- a/src/main/java/str_exporter/client/BackendBroadcaster.java +++ b/src/main/java/str_exporter/client/BackendBroadcaster.java @@ -4,13 +4,7 @@ import org.apache.logging.log4j.Logger; import str_exporter.config.Config; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.ReentrantLock; @@ -28,11 +22,13 @@ public class BackendBroadcaster { private final long checkQueuePeriodMillis; private final boolean sendDuplicates; private final Config config; + private final EBSClient client; - public BackendBroadcaster(Config config, long checkQueuePeriodMillis, boolean sendDuplicates) { + public BackendBroadcaster(Config config, EBSClient client, long checkQueuePeriodMillis, boolean sendDuplicates) { this.checkQueuePeriodMillis = checkQueuePeriodMillis; this.sendDuplicates = sendDuplicates; this.config = config; + this.client = client; message = null; lastMessage = null; queueLock = new ReentrantLock(); @@ -97,32 +93,6 @@ private void readQueue() { } private void broadcastMessage(String msg) throws IOException { - URL url = new URL(config.getApiUrl() + "/api/v1/message"); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("Content-Type", "application/json"); //; utf-8 - con.setRequestProperty("Accept", "application/json"); - con.setDoOutput(true); - - OutputStream os = con.getOutputStream(); - byte[] input = msg.getBytes(StandardCharsets.UTF_8); - os.write(input, 0, input.length); - - try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), - StandardCharsets.UTF_8))) { - StringBuilder response = new StringBuilder(); - String responseLine; - while ((responseLine = br.readLine()) != null) { - response.append(responseLine.trim()); - } - if (!response.toString().equals("Success")) { - logger.info("message not broadcast successfully, response: " + response); - } - if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) { - lastSuccessBroadcast.set(System.currentTimeMillis()); - } - } catch (Exception e) { - e.printStackTrace(); - } + this.client.broadcastMessage(msg); } } diff --git a/src/main/java/str_exporter/client/EBSClient.java b/src/main/java/str_exporter/client/EBSClient.java index dab095a..f8623c1 100644 --- a/src/main/java/str_exporter/client/EBSClient.java +++ b/src/main/java/str_exporter/client/EBSClient.java @@ -1,7 +1,6 @@ package str_exporter.client; -import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import str_exporter.config.Config; @@ -9,13 +8,16 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; +import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.atomic.AtomicLong; public class EBSClient { - public static final AtomicLong lastSuccessAuth = new AtomicLong(0); + public static final AtomicLong lastSuccessRequest = new AtomicLong(0); private final Config config; public EBSClient(Config config) { @@ -23,28 +25,37 @@ public EBSClient(Config config) { } public User verifyCredentials(String code) throws IOException { - Gson gson = new Gson(); + Map body = new HashMap<>(); + body.put("code", code); + return doRequest("POST", "/api/v1/auth", config.gson.toJson(body), User.class); + } + + public void broadcastMessage(String message) throws IOException { + doRequest("POST", "/api/v1/message", message, String.class); + } - URL url = new URL(config.getApiUrl() + "/api/v1/auth"); + private T doRequest(String method, String path, String body, Type outputType) throws IOException { + URL url = new URL(config.getApiUrl() + path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); + con.setRequestMethod(method); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true); OutputStream os = con.getOutputStream(); - String msg = "{\"code\":\"" + code + "\"}"; - byte[] input = msg.getBytes(StandardCharsets.UTF_8); - os.write(input, 0, input.length); + if (body != null && !body.isEmpty()) { + byte[] input = body.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) { - if (con.getResponseCode() == 200) { + if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) { JsonReader reader = new JsonReader(br); - EBSClient.lastSuccessAuth.set(System.currentTimeMillis()); - return gson.fromJson(reader, User.class); + EBSClient.lastSuccessRequest.set(System.currentTimeMillis()); + return config.gson.fromJson(reader, outputType); } - throw new IOException("Failed : HTTP error code : " + con.getResponseCode()); + throw new IOException(method + " " + path + " failed: HTTP error code: " + con.getResponseCode()); } } } diff --git a/src/main/java/str_exporter/config/Config.java b/src/main/java/str_exporter/config/Config.java index f6a63bb..9d22d70 100644 --- a/src/main/java/str_exporter/config/Config.java +++ b/src/main/java/str_exporter/config/Config.java @@ -1,6 +1,7 @@ package str_exporter.config; import com.evacipated.cardcrawl.modthespire.lib.SpireConfig; +import com.google.gson.Gson; import java.io.IOException; import java.net.MalformedURLException; @@ -13,6 +14,7 @@ public class Config { private static final String OAUTH_SETTINGS = "oauth"; private static final String USER_SETTINGS = "user"; private final SpireConfig config; + public final Gson gson = new Gson(); public Config() throws IOException { Properties strDefaultSettings = new Properties();