Identity, port, hostname acknowledgement

This commit is contained in:
2024-11-25 16:56:06 +02:00
parent 6bee3a0e13
commit 8713c6e7b3
9 changed files with 74 additions and 25 deletions

View File

@@ -1,13 +1,19 @@
package io.github.lumijiez;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.github.lumijiez.data.Data;
import io.github.lumijiez.data.models.NodeInfo;
import jakarta.persistence.EntityManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
@@ -15,13 +21,17 @@ import java.util.concurrent.CountDownLatch;
public class Main {
public static Logger logger = LogManager.getLogger(Main.class);
private static final CountDownLatch waitForConnection = new CountDownLatch(1);
private static final Gson gson = new Gson();
public static void main(String[] args) {
try {
String hostname = System.getenv().getOrDefault("HOSTNAME", "localhost");
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
logger.info("Node started");
EntityManager em = Data.getEntityManager();
logger.info("Connected to database: << symphony >>");
logger.info("Connected to database:\u001B[33m\033[1m symphony");
em.close();
try (HttpClient client = HttpClient.newHttpClient()) {
@@ -31,8 +41,15 @@ public class Main {
@Override
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
try {
int nodeCount = Integer.parseInt(data.toString());
logger.info("Acknowledged nodes: {}", nodeCount);
Type nodeListType = new TypeToken<List<NodeInfo>>() {}.getType();
List<NodeInfo> nodes = gson.fromJson(data.toString(), nodeListType);
//// logger.info("Discovered Nodes (JSON):\n{}",
//// new GsonBuilder()
//// .setPrettyPrinting()
//// .create()
//// .toJson(nodes)
// );
logger.info("Acknowledged nodes:\u001B[33m\033[1m {}\u001B[0m", nodes.size());
} catch (NumberFormatException e) {
logger.error("Received invalid node count: {}", data);
}
@@ -41,7 +58,9 @@ public class Main {
@Override
public void onOpen(WebSocket webSocket) {
logger.info("Successfully registered to Discovery");
NodeInfo nodeInfo = new NodeInfo(hostname, port);
webSocket.sendText(gson.toJson(nodeInfo), true);
logger.info("Successfully registered to \033[1mDiscovery");
waitForConnection.countDown();
WebSocket.Listener.super.onOpen(webSocket);
}

View File

@@ -0,0 +1,3 @@
package io.github.lumijiez.data.models;
public record NodeInfo(String hostname, int port) { }

View File

@@ -4,7 +4,7 @@
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>
%highlight{%d{yyyy-MM-dd HH:mm:ss} %-5level [%t]: %msg}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue}%n
%d{yyyy-MM-dd HH:mm:ss} %highlight{%-5level}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue}: %highlight{%msg}{FATAL=red, ERROR=red}%n
</Pattern>
</PatternLayout>
</Console>