Updated to latest Protocol
This commit is contained in:
@@ -53,13 +53,14 @@ public class Main {
|
||||
}
|
||||
|
||||
private static void initProtocol() {
|
||||
client = new WebClient();
|
||||
|
||||
ProtocolValues values = new ProtocolValues();
|
||||
|
||||
values.packetHandler = new PacketHandler();
|
||||
values.eventManager = new EventManager();
|
||||
|
||||
client = new WebClient();
|
||||
|
||||
try {
|
||||
bridge = new ProtocolBridge(
|
||||
client,
|
||||
@@ -67,6 +68,9 @@ public class Main {
|
||||
PROTOCOL_VERSION,
|
||||
new File("logs")
|
||||
);
|
||||
|
||||
// TODO
|
||||
client.connectToINSServer("127.0.0.1", -1, -1);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -75,7 +79,7 @@ public class Main {
|
||||
private static void registerEventHandlers() throws Exception {
|
||||
EventManager eventManager = bridge.getProtocolValues().eventManager;
|
||||
|
||||
eventManager.registerListener(ServerPacketHandler.class);
|
||||
eventManager.registerListener(new ServerPacketHandler());
|
||||
}
|
||||
|
||||
private static void registerPacketListeners() {
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
package org.openautonomousconnection.webclient.network;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportProtocol;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.side.client.ProtocolWebClient;
|
||||
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.WebRequestMethod;
|
||||
@@ -19,6 +19,8 @@ import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.URL;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,7 +32,7 @@ import static org.openautonomousconnection.webclient.Main.*;
|
||||
* Suppressing "unchecked" warnings because of generic casts
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class WebClient extends ProtocolWebClient {
|
||||
public class WebClient extends ProtocolClient {
|
||||
/**
|
||||
* Global protocol timeout = 30 seconds
|
||||
*/
|
||||
@@ -40,17 +42,17 @@ public class WebClient extends ProtocolWebClient {
|
||||
* Connect to Webserver with URLs (info name based)
|
||||
* @param url url containing host and port (port optional)
|
||||
*/
|
||||
public void connectToWebServer(URL url) {
|
||||
public void connectToWebServer(URL url) throws Exception {
|
||||
String host = url.getHost();
|
||||
|
||||
int port = url.getPort() != -1 ? url.getPort() : DEFAULT_WEB_PORT_TCP;
|
||||
|
||||
// UDP is always preset
|
||||
this.buildServerConnection(host, port, DEFAULT_WEB_PORT_UDP);
|
||||
this.buildServerConnection(null, true);
|
||||
|
||||
try {
|
||||
this.getClientServerConnection().connect();
|
||||
} catch (ConnectException e) {
|
||||
this.getClientServerConnection().connect(host, DEFAULT_WEB_PORT_TCP);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -58,18 +60,25 @@ public class WebClient extends ProtocolWebClient {
|
||||
/**
|
||||
* Connect to INServer with URLs (ip based)
|
||||
* @param ip ip to server
|
||||
* @param port tcp ins port
|
||||
* @param tcpPort tcp ins port
|
||||
* @param udpPort tcp ins port
|
||||
*/
|
||||
public void connectToINSServer(String ip, int port) {
|
||||
public void connectToINSServer(String ip, int tcpPort, int udpPort) throws NoSuchAlgorithmException, KeyManagementException {
|
||||
|
||||
port = port != -1 ? port : DEFAULT_INS_PORT_TCP;
|
||||
tcpPort = tcpPort != -1 ? tcpPort : DEFAULT_INS_PORT_TCP;
|
||||
udpPort = udpPort != -1 ? udpPort : DEFAULT_INS_PORT_UDP;
|
||||
|
||||
// UDP is always preset
|
||||
this.buildServerConnection(ip, port, DEFAULT_INS_PORT_UDP);
|
||||
|
||||
try {
|
||||
this.getClientINSConnection().connect();
|
||||
} catch (ConnectException e) {
|
||||
this.buildINSConnection();
|
||||
this.getClientINSConnection().connect(ip, tcpPort);
|
||||
|
||||
while (getClientINSConnection().isConnected()) {
|
||||
System.out.println("c");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -128,9 +137,9 @@ public class WebClient extends ProtocolWebClient {
|
||||
WebRequestMethod.GET,
|
||||
Map.of(),
|
||||
null
|
||||
), Transport.TCP);
|
||||
), TransportProtocol.TCP);
|
||||
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -145,8 +154,8 @@ public class WebClient extends ProtocolWebClient {
|
||||
WebRequestMethod.POST,
|
||||
Map.of(),
|
||||
null
|
||||
), Transport.TCP);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
), TransportProtocol.TCP);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -162,8 +171,8 @@ public class WebClient extends ProtocolWebClient {
|
||||
WebRequestMethod.GET,
|
||||
Map.of(),
|
||||
null
|
||||
), Transport.TCP);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
), TransportProtocol.TCP);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
package org.openautonomousconnection.webclient.network.handlers;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.packets.receive.C_PacketReceivedEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.packets.C_PacketReadEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.Packet;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportProtocol;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
import org.openautonomousconnection.webclient.packetlistener.PacketListener;
|
||||
@@ -23,10 +23,10 @@ public class ServerPacketHandler extends EventListener {
|
||||
listeners.add(packetListener);
|
||||
}
|
||||
|
||||
public void onPacketReceived(C_PacketReceivedEvent packetReceivedEvent) {
|
||||
public void onPacketReceived(C_PacketReadEvent packetReceivedEvent) {
|
||||
|
||||
Packet packet = packetReceivedEvent.getPacket();
|
||||
Transport transport = packetReceivedEvent.getTransport();
|
||||
TransportProtocol transport = packetReceivedEvent.getProtocol();
|
||||
|
||||
switch (packet.getPacketID()) {
|
||||
case 4 -> listeners
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
package org.openautonomousconnection.webclient.network.type;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.openautonomousconnection.webclient.network.type.application.NetAppstreamFile;
|
||||
import org.openautonomousconnection.webclient.network.type.image.NetTransmitIcon;
|
||||
import org.openautonomousconnection.webclient.network.type.text.NetTransmitDocument;
|
||||
import org.openautonomousconnection.webclient.network.type.text.NetTransmitStylesheet;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.css.CSSStyleSheet;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@@ -6,8 +6,8 @@ package org.openautonomousconnection.webclient.network.type.text;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.openautonomousconnection.webclient.network.type.NetTransmitFile;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class NetTransmitDocument implements NetTransmitFile<Document> {
|
||||
@Getter @Setter
|
||||
|
||||
@@ -6,11 +6,11 @@ package org.openautonomousconnection.webclient.network.website;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.openautonomousconnection.webclient.Main;
|
||||
import org.openautonomousconnection.webclient.network.Promise;
|
||||
import org.openautonomousconnection.webclient.network.type.NTFType;
|
||||
import org.openautonomousconnection.webclient.network.type.NetTransmitFile;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.swing.*;
|
||||
|
||||
@@ -19,7 +19,7 @@ public final class Tab {
|
||||
private Icon favicon;
|
||||
private WebSite webSite;
|
||||
|
||||
public Tab(URL infoName, Icon favicon) {
|
||||
public Tab(URL infoName, Icon favicon) throws Exception {
|
||||
Main.client.connectToWebServer(infoName);
|
||||
|
||||
this.infoName = infoName;
|
||||
@@ -27,7 +27,7 @@ public final class Tab {
|
||||
this.webSite = new WebSite(infoName);
|
||||
}
|
||||
|
||||
public Tab(URL infoName) {
|
||||
public Tab(URL infoName) throws Exception {
|
||||
Main.client.connectToWebServer(infoName);
|
||||
|
||||
this.infoName = infoName;
|
||||
@@ -35,7 +35,7 @@ public final class Tab {
|
||||
this.webSite = new WebSite(infoName);
|
||||
}
|
||||
|
||||
public Tab(WebSite webSite) {
|
||||
public Tab(WebSite webSite) throws Exception {
|
||||
Main.client.connectToWebServer(webSite.getInfoName());
|
||||
|
||||
this.infoName = webSite.getInfoName();
|
||||
|
||||
@@ -4,22 +4,24 @@
|
||||
|
||||
package org.openautonomousconnection.webclient.packetlistener;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportProtocol;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
|
||||
import java.util.concurrent.TransferQueue;
|
||||
|
||||
public abstract class PacketListener {
|
||||
|
||||
public void onAuthPacketReceived(AuthPacket authPacket, Transport transport) {
|
||||
public void onAuthPacketReceived(AuthPacket authPacket, TransportProtocol transport) {
|
||||
}
|
||||
|
||||
public void onAuthPacketSent(AuthPacket authPacket, Transport transport) {
|
||||
public void onAuthPacketSent(AuthPacket authPacket, TransportProtocol transport) {
|
||||
}
|
||||
|
||||
public void onWebResponsePacketReceived(WebResponsePacket webResponsePacket, Transport transport) {
|
||||
public void onWebResponsePacketReceived(WebResponsePacket webResponsePacket, TransportProtocol transport) {
|
||||
}
|
||||
|
||||
public void onWebRequestPacketSent(WebRequestPacket webRequestPacket, Transport transport) {
|
||||
public void onWebRequestPacketSent(WebRequestPacket webRequestPacket, TransportProtocol transport) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package org.openautonomousconnection.webclient.packetlistener.listeners;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportProtocol;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
import org.openautonomousconnection.webclient.Main;
|
||||
import org.openautonomousconnection.webclient.network.type.NTFType;
|
||||
@@ -14,12 +12,14 @@ import org.openautonomousconnection.webclient.network.type.NetTransmitFile;
|
||||
import org.openautonomousconnection.webclient.network.type.image.NetTransmitIcon;
|
||||
import org.openautonomousconnection.webclient.network.type.text.NetTransmitDocument;
|
||||
import org.openautonomousconnection.webclient.packetlistener.PacketListener;
|
||||
import org.openautonomousconnection.webclient.ui.dom.DomSerializer;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class WebPacketListener extends PacketListener {
|
||||
@Override
|
||||
public void onWebResponsePacketReceived(WebResponsePacket packet, Transport transport) {
|
||||
public void onWebResponsePacketReceived(WebResponsePacket packet, TransportProtocol transport) {
|
||||
NTFType type = NTFType.UNKNOWN;
|
||||
|
||||
NetTransmitFile<?> file = null;
|
||||
@@ -35,7 +35,7 @@ public class WebPacketListener extends PacketListener {
|
||||
|
||||
String htmlText = new String(packet.getBody());
|
||||
|
||||
Document dom = Jsoup.parse(htmlText);
|
||||
Document dom = DomSerializer.fromString(Main.mainFrame.getDomContainerPanel().getWebEngine(), htmlText);
|
||||
|
||||
file = new NetTransmitDocument();
|
||||
break;
|
||||
|
||||
@@ -29,10 +29,8 @@ public final class MainFrame extends BrowserFrame {
|
||||
this.setSize(800, 600);
|
||||
|
||||
try {
|
||||
this.openTab = new Tab(
|
||||
URI.create("web://127.0.0.1").toURL()
|
||||
);
|
||||
} catch (MalformedURLException e) {
|
||||
// TODO this.openTab = new Tab(URI.create("web://127.0.0.1").toURL());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -64,7 +62,7 @@ public final class MainFrame extends BrowserFrame {
|
||||
}
|
||||
}
|
||||
|
||||
public void open(URL url) {
|
||||
public void open(URL url) throws Exception {
|
||||
TabButton button = new TabButton(url);
|
||||
|
||||
this.getTopBar().addButton(button);
|
||||
|
||||
@@ -10,7 +10,7 @@ import javafx.scene.Scene;
|
||||
import javafx.scene.web.WebEngine;
|
||||
import javafx.scene.web.WebView;
|
||||
import lombok.Getter;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
@@ -30,7 +30,7 @@ public class DOMContainerPanel extends JPanel {
|
||||
}
|
||||
|
||||
public void loadContent(Document html) {
|
||||
this.loadContent(html.html());
|
||||
this.loadContent(DomSerializer.toString(html));
|
||||
}
|
||||
|
||||
public DOMContainerPanel() {
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package org.openautonomousconnection.webclient.ui.dom;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.StringWriter;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.web.WebEngine;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Serializes a W3C DOM Document to an HTML string.
|
||||
*
|
||||
* <p>Works with JavaFX WebEngine DOM.</p>
|
||||
*/
|
||||
public final class DomSerializer {
|
||||
|
||||
private DomSerializer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a DOM {@link Document} to a string.
|
||||
*
|
||||
* @param document DOM document
|
||||
* @return serialized HTML
|
||||
*/
|
||||
public static String toString(Document document) {
|
||||
if (document == null) {
|
||||
throw new IllegalArgumentException("document is null");
|
||||
}
|
||||
|
||||
try {
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer transformer = tf.newTransformer();
|
||||
|
||||
transformer.setOutputProperty(OutputKeys.METHOD, "html");
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "no");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
transformer.transform(
|
||||
new DOMSource(document),
|
||||
new StreamResult(writer)
|
||||
);
|
||||
return writer.toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to serialize DOM document", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an HTML string into a JavaFX {@link WebEngine} and returns the resulting DOM {@link Document}.
|
||||
*
|
||||
* <p>No JavaScript required. Parsing is done by WebKit.</p>
|
||||
*
|
||||
* @param engine JavaFX WebEngine (must not be null)
|
||||
* @param html HTML source
|
||||
* @return parsed DOM document
|
||||
*/
|
||||
public static Document fromString(WebEngine engine, String html) {
|
||||
Objects.requireNonNull(engine, "engine");
|
||||
Objects.requireNonNull(html, "html");
|
||||
|
||||
if (!Platform.isFxApplicationThread()) {
|
||||
final Document[] result = new Document[1];
|
||||
final RuntimeException[] error = new RuntimeException[1];
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
Platform.runLater(() -> {
|
||||
try {
|
||||
result[0] = fromString(engine, html);
|
||||
} catch (RuntimeException e) {
|
||||
error[0] = e;
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
if (!latch.await(5, TimeUnit.SECONDS)) {
|
||||
throw new RuntimeException("Timed out while parsing HTML");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while parsing HTML", e);
|
||||
}
|
||||
|
||||
if (error[0] != null) throw error[0];
|
||||
return result[0];
|
||||
}
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
engine.getLoadWorker().stateProperty().addListener((obs, o, n) -> {
|
||||
switch (n) {
|
||||
case SUCCEEDED, FAILED, CANCELLED -> latch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
engine.loadContent(html, "text/html");
|
||||
|
||||
try {
|
||||
if (!latch.await(5, TimeUnit.SECONDS)) {
|
||||
throw new RuntimeException("Timed out while parsing HTML");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while parsing HTML", e);
|
||||
}
|
||||
|
||||
Document doc = engine.getDocument();
|
||||
if (doc == null) {
|
||||
throw new IllegalStateException("WebEngine did not produce a DOM document");
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,11 @@ import java.net.URL;
|
||||
public class TabButton extends JButton {
|
||||
private Tab tab;
|
||||
|
||||
public TabButton(@NonNull String infoName) throws MalformedURLException {
|
||||
public TabButton(@NonNull String infoName) throws Exception {
|
||||
this(URI.create(infoName).toURL());
|
||||
}
|
||||
|
||||
public TabButton(@NonNull URL infoName) {
|
||||
public TabButton(@NonNull URL infoName) throws Exception {
|
||||
this.tab = new Tab(infoName);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user