Skip to content

Commit

Permalink
Use ebs client to broadcast messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MaT1g3R committed Oct 9, 2023
1 parent 1430271 commit c9ed0f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/main/java/str_exporter/SlayTheRelicsExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down
38 changes: 4 additions & 34 deletions src/main/java/str_exporter/client/BackendBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
35 changes: 23 additions & 12 deletions src/main/java/str_exporter/client/EBSClient.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
package str_exporter.client;


import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import str_exporter.config.Config;

import java.io.BufferedReader;
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) {
this.config = config;
}

public User verifyCredentials(String code) throws IOException {
Gson gson = new Gson();
Map<String, String> 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> 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());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/str_exporter/config/Config.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit c9ed0f1

Please sign in to comment.