From 9f8898561de654820ce9e973762d2f9823cbdf58 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 12 Dec 2023 00:53:05 +0300 Subject: [PATCH] Implement example chat app --- build.gradle | 1 - .../JGUN/examples/chat/FXMLController.java | 72 +++++++++++++++++-- .../JGUN/examples/chat/Gateway.java | 14 ++++ .../chronosxyz/JGUN/examples/chat/scene.fxml | 44 ++++++++++-- .../JGUN/network/GatewayNetworkNode.java | 8 +-- 5 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/Gateway.java diff --git a/build.gradle b/build.gradle index f826fd1..7d793cf 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,6 @@ repositories { dependencies { api 'org.java-websocket:Java-WebSocket:1.5.4' compileOnly 'org.java-websocket:Java-WebSocket:1.5.4' - implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.0' implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.3' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.3' implementation 'com.github.ben-manes.caffeine:jcache:3.1.5' diff --git a/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/FXMLController.java b/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/FXMLController.java index 22b8c4e..125bce8 100644 --- a/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/FXMLController.java +++ b/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/FXMLController.java @@ -1,20 +1,82 @@ package io.github.chronosxyz.JGUN.examples.chat; +import io.github.chronosx88.JGUN.api.Gun; +import io.github.chronosx88.JGUN.api.graph.ArrayBuilder; +import io.github.chronosx88.JGUN.api.graph.NodeBuilder; +import io.github.chronosx88.JGUN.models.graph.Node; +import io.github.chronosx88.JGUN.models.graph.NodeValue; +import io.github.chronosx88.JGUN.models.graph.values.ArrayValue; +import io.github.chronosx88.JGUN.models.graph.values.NodeLinkValue; +import io.github.chronosx88.JGUN.models.graph.values.StringValue; +import javafx.application.Platform; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Label; +import javafx.scene.control.Button; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.ResourceBundle; +import java.util.concurrent.ExecutionException; public class FXMLController implements Initializable { @FXML - private Label label; + Button sendButton; + + @FXML + TextArea chatBox; + + @FXML + TextArea msgEditbox; + + @FXML + TextField nicknameEditbox; + + private Gun gun; @Override public void initialize(URL url, ResourceBundle rb) { - String javaVersion = System.getProperty("java.version"); - String javafxVersion = System.getProperty("javafx.version"); - label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + "."); + Platform.runLater(() -> { + gun.get("chat").map((k, v) -> { + if (!k.equals("messages")) return; + ArrayValue val = (ArrayValue) v; + val.getValue().forEach((e) -> { + if (e.getValueType() == NodeValue.ValueType.LINK) { + Node obj; + try { + obj = gun.get(((NodeLinkValue) e).getLink()).once().get().getData(); + } catch (InterruptedException | ExecutionException ex) { + throw new RuntimeException(ex); + } + StringValue fromVal = (StringValue) obj.getValues().get("from"); + StringValue msgVal = (StringValue) obj.getValues().get("text"); + StringValue dateVal = (StringValue) obj.getValues().get("date"); + chatBox.appendText(String.format("[%s] <%s> %s\n", dateVal.getValue(), fromVal.getValue(), msgVal.getValue())); + } + }); + }); + }); + + sendButton.setOnAction((e) -> { + try { + gun.get("chat") + .put(new NodeBuilder().add("messages", new ArrayBuilder() + .add(new NodeBuilder() + .add("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .format(Calendar.getInstance().getTime())) + .add("from", nicknameEditbox.getText()) + .add("text", msgEditbox.getText()))).build()) + .get(); + } catch (InterruptedException | ExecutionException ex) { + throw new RuntimeException(ex); + } + msgEditbox.clear(); + }); + } + + public void setGunInstance(Gun gun) { + this.gun = gun; } } \ No newline at end of file diff --git a/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/Gateway.java b/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/Gateway.java new file mode 100644 index 0000000..81932e5 --- /dev/null +++ b/examples/chat/src/main/java/io/github/chronosxyz/JGUN/examples/chat/Gateway.java @@ -0,0 +1,14 @@ +package io.github.chronosxyz.JGUN.examples.chat; + +import io.github.chronosx88.JGUN.api.Gun; +import io.github.chronosx88.JGUN.network.GatewayNetworkNode; +import io.github.chronosx88.JGUN.storage.MemoryStorage; +import io.github.chronosx88.JGUN.storage.Storage; + +public class Gateway { + public static void main(String[] args) throws InterruptedException { + Storage storage = new MemoryStorage(); + GatewayNetworkNode peer = new GatewayNetworkNode(5054, storage); + Gun gun = new Gun(storage, peer); + } +} diff --git a/examples/chat/src/main/resources/io/github/chronosxyz/JGUN/examples/chat/scene.fxml b/examples/chat/src/main/resources/io/github/chronosxyz/JGUN/examples/chat/scene.fxml index 00e0612..78a977f 100644 --- a/examples/chat/src/main/resources/io/github/chronosxyz/JGUN/examples/chat/scene.fxml +++ b/examples/chat/src/main/resources/io/github/chronosxyz/JGUN/examples/chat/scene.fxml @@ -1,10 +1,42 @@ - - + + + - - \ No newline at end of file + + + +