Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working PN-via-OP support #515

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.xp.postnamazu;

import java.net.http.HttpResponse;

public record PnHttpResponse(HttpResponse<String> response) implements PnResponse {
@Override
public boolean isSuccess() {
return response.statusCode() == 200;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package gg.xp.postnamazu;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gg.xp.reevent.events.EventContext;
import gg.xp.reevent.events.EventMaster;
import gg.xp.reevent.scan.FilteredEventHandler;
import gg.xp.reevent.scan.HandleEvents;
import gg.xp.xivsupport.events.ws.ActWsLogSource;
import gg.xp.xivsupport.persistence.PersistenceProvider;
import gg.xp.xivsupport.persistence.settings.EnumSetting;
import gg.xp.xivsupport.persistence.settings.HttpURISetting;
import gg.xp.xivsupport.persistence.settings.IntSetting;
import gg.xp.xivsupport.sys.KnownLogSource;
Expand All @@ -19,6 +22,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -32,6 +36,7 @@ public class PnMain implements FilteredEventHandler {
private final HttpClient http = HttpClient.newBuilder().build();
private final ObjectMapper mapper = new ObjectMapper();
private final HttpURISetting uriSetting;
private final EnumSetting<PnMode> modeSetting;
private final EventMaster master;

private volatile PnStatus status = PnStatus.UNKNOWN;
Expand All @@ -40,16 +45,19 @@ public class PnMain implements FilteredEventHandler {
private final IntSetting cmdDelayPlus;
private final IntSetting amDelayBase;
private final IntSetting amDelayPlus;
private final ActWsLogSource ws;

public PnMain(EventMaster master, PersistenceProvider pers, PrimaryLogSource pls) {
public PnMain(EventMaster master, PersistenceProvider pers, PrimaryLogSource pls, ActWsLogSource ws) {
this.master = master;
this.pls = pls;
this.ws = ws;
try {
uriSetting = new HttpURISetting(pers, "pn-support.uri", new URI("http://localhost:2019/"));
cmdDelayBase = new IntSetting(pers, "pn-support.base-cmd-delay", 150, 0, 5000);
cmdDelayPlus = new IntSetting(pers, "pn-support.plus-cmd-delay", 150, 0, 5000);
amDelayBase = new IntSetting(pers, "pn-support.base-am-delay", 150, 0, 5000);
amDelayPlus = new IntSetting(pers, "pn-support.plus-am-delay", 150, 0, 5000);
modeSetting = new EnumSetting<>(pers, "pn-support.mode", PnMode.class, PnMode.OP);
}
catch (URISyntaxException e) {
throw new RuntimeException(e);
Expand All @@ -60,7 +68,7 @@ public PnMain(EventMaster master, PersistenceProvider pers, PrimaryLogSource pls
public void handleOutgoing(EventContext context, PnOutgoingMessage message) {
Runnable task = () -> {
try {
HttpResponse<String> response = sendMessageDirectly(message.getCommand(), message.getPayload());
sendMessageDirectly(message.getCommand(), message.getPayload());
updateStatus(PnStatus.GOOD);
}
catch (Throwable e) {
Expand Down Expand Up @@ -95,26 +103,43 @@ private void updateStatus(PnStatus status) {
this.status = status;
}

public HttpResponse<String> sendMessageDirectly(String command, Object payload) {
public PnResponse sendMessageDirectly(String command, Object payload) {
String body;
try {
body = payload instanceof String sp ? sp : mapper.writeValueAsString(payload);
log.info("PostNamazu outgoing: ({}): '{}'", command, body);
HttpResponse<String> response = http.send(
HttpRequest
.newBuilder(uriSetting.get().resolve(command))
.POST(
HttpRequest.BodyPublishers
.ofString(body)).build(),
HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
log.error("PostNamazu: Response failure: {}: {}", response.statusCode(), response.body());
}
return response;
}
catch (IOException | InterruptedException e) {
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return switch (modeSetting.get()) {
case HTTP -> {
try {
log.info("PostNamazu http outgoing: ({}): '{}'", command, body);
HttpResponse<String> response = http.send(
HttpRequest
.newBuilder(uriSetting.get().resolve(command))
.POST(
HttpRequest.BodyPublishers
.ofString(body)).build(),
HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
log.error("PostNamazu: Response failure: {}: {}", response.statusCode(), response.body());
}
yield new PnHttpResponse(response);
}
catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
case OP -> {
ws.sendObject(Map.of(
"call", "PostNamazu",
"c", command,
"p", body,
"rseq", "pn-rseq"));
yield new PnOpResponse();
}
};
}

@Override
Expand Down Expand Up @@ -145,4 +170,8 @@ public PnStatus getStatus() {
public HttpURISetting getUriSetting() {
return uriSetting;
}

public EnumSetting<PnMode> getModeSetting() {
return modeSetting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gg.xp.postnamazu;

import gg.xp.xivsupport.gui.util.HasFriendlyName;

public enum PnMode implements HasFriendlyName {
/**
* Direct PN connection
*/
HTTP("HTTP Connection"),
/**
* Use OverlayPlugin hook
*/
OP("Via OverlayPlugin (Easier)");

private final String friendlyName;

PnMode(String friendlyName) {
this.friendlyName = friendlyName;
}

@Override
public String getFriendlyName() {
return friendlyName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gg.xp.postnamazu;

public record PnOpResponse() implements PnResponse {
@Override
public boolean isSuccess() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gg.xp.postnamazu;

public sealed interface PnResponse permits PnHttpResponse, PnOpResponse {
boolean isSuccess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gg.xp.postnamazu.PnGameCommand;
import gg.xp.postnamazu.PnMain;
import gg.xp.postnamazu.PnMode;
import gg.xp.postnamazu.PnStatusUpdatedEvent;
import gg.xp.reevent.events.EventContext;
import gg.xp.reevent.events.EventMaster;
Expand All @@ -12,8 +13,10 @@
import gg.xp.xivsupport.gui.WrapLayout;
import gg.xp.xivsupport.gui.extra.PluginTab;
import gg.xp.xivsupport.gui.util.GuiUtil;
import gg.xp.xivsupport.persistence.gui.EnumSettingGui;
import gg.xp.xivsupport.persistence.gui.HttpURISettingGui;
import gg.xp.xivsupport.persistence.gui.IntSettingSpinner;
import gg.xp.xivsupport.persistence.settings.EnumSetting;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -48,7 +51,10 @@ public String getTabName() {
@Override
public Component getTabContents() {
TitleBorderFullsizePanel outer = new TitleBorderFullsizePanel("PostNamazu");
EnumSetting<PnMode> modeSetting = backend.getModeSetting();
var modeSettingGui = new EnumSettingGui<>(modeSetting, "Mode", () -> true, true);
JPanel uriControl = new HttpURISettingGui(backend.getUriSetting(), "Base URI", false).getComponent();
modeSetting.addAndRunListener(() -> uriControl.setEnabled(modeSetting.get() == PnMode.HTTP));
JPanel testPanel;
JScrollPane scroll;
{
Expand Down Expand Up @@ -92,7 +98,7 @@ public Component getTabContents() {
}


GuiUtil.simpleTopDownLayout(outer, 400, uriControl, Box.createRigidArea(new Dimension(390, 5)), scroll, testPanel, amDelayPanel, cmdDelayPanel);
GuiUtil.simpleTopDownLayout(outer, 400, modeSettingGui.getComponent(), uriControl, Box.createRigidArea(new Dimension(390, 5)), scroll, testPanel, amDelayPanel, cmdDelayPanel);
outer.invalidate();

return outer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,19 @@ public Component getResetButton() {
}

public JPanel getComponent() {
JPanel box = new JPanel();
Component textBox = getTextBoxOnly();
Component resetButton = getResetButton();
JPanel box = new JPanel() {
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
textBox.setEnabled(enabled);
resetButton.setEnabled(enabled);
}
};
box.setLayout(new WrapLayout());
box.add(getTextBoxOnly());
box.add(getResetButton());
box.add(textBox);
box.add(resetButton);
box.add(getLabelOnly());
box.setMaximumSize(box.getPreferredSize());
return box;
Expand Down
Loading