Identity, port, hostname acknowledgement
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
package io.github.lumijiez;
|
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.Data;
|
||||||
|
import io.github.lumijiez.data.models.NodeInfo;
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.WebSocket;
|
import java.net.http.WebSocket;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
@@ -15,13 +21,17 @@ import java.util.concurrent.CountDownLatch;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static Logger logger = LogManager.getLogger(Main.class);
|
public static Logger logger = LogManager.getLogger(Main.class);
|
||||||
private static final CountDownLatch waitForConnection = new CountDownLatch(1);
|
private static final CountDownLatch waitForConnection = new CountDownLatch(1);
|
||||||
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
|
String hostname = System.getenv().getOrDefault("HOSTNAME", "localhost");
|
||||||
|
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
|
||||||
|
|
||||||
logger.info("Node started");
|
logger.info("Node started");
|
||||||
EntityManager em = Data.getEntityManager();
|
EntityManager em = Data.getEntityManager();
|
||||||
|
|
||||||
logger.info("Connected to database: << symphony >>");
|
logger.info("Connected to database:\u001B[33m\033[1m symphony");
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||||
@@ -31,8 +41,15 @@ public class Main {
|
|||||||
@Override
|
@Override
|
||||||
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
|
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
|
||||||
try {
|
try {
|
||||||
int nodeCount = Integer.parseInt(data.toString());
|
Type nodeListType = new TypeToken<List<NodeInfo>>() {}.getType();
|
||||||
logger.info("Acknowledged nodes: {}", nodeCount);
|
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) {
|
} catch (NumberFormatException e) {
|
||||||
logger.error("Received invalid node count: {}", data);
|
logger.error("Received invalid node count: {}", data);
|
||||||
}
|
}
|
||||||
@@ -41,7 +58,9 @@ public class Main {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket webSocket) {
|
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();
|
waitForConnection.countDown();
|
||||||
WebSocket.Listener.super.onOpen(webSocket);
|
WebSocket.Listener.super.onOpen(webSocket);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package io.github.lumijiez.data.models;
|
||||||
|
|
||||||
|
public record NodeInfo(String hostname, int port) { }
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>
|
<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>
|
</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
</Console>
|
</Console>
|
||||||
|
|||||||
@@ -1,34 +1,46 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
import io.javalin.websocket.WsContext;
|
import io.javalin.websocket.WsContext;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
public class JavalinConfig {
|
public class JavalinConfig {
|
||||||
private static final Map<String, WsContext> users = new ConcurrentHashMap<>();
|
private static final Map<String, NodeInfo> registeredNodes = new ConcurrentHashMap<>();
|
||||||
|
private static final Map<String, WsContext> nodes = new ConcurrentHashMap<>();
|
||||||
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
public static void setup(Javalin app) {
|
public static void setup(Javalin app) {
|
||||||
app.ws("/discovery", ws -> {
|
app.ws("/discovery", ws -> {
|
||||||
ws.onConnect(ctx -> {
|
ws.onConnect(ctx -> {
|
||||||
String id = ctx.sessionId();
|
// ToDo
|
||||||
users.put(id, ctx);
|
// A general notification
|
||||||
broadcast("Discovery-Join", "Join");
|
nodes.put(ctx.sessionId(), ctx);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.onMessage(ctx -> {
|
||||||
|
String message = ctx.message();
|
||||||
|
NodeInfo nodeInfo = gson.fromJson(message, NodeInfo.class);
|
||||||
|
registeredNodes.put(ctx.sessionId(), nodeInfo);
|
||||||
|
broadcastNodeCount();
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.onClose(ctx -> {
|
ws.onClose(ctx -> {
|
||||||
String id = ctx.sessionId();
|
registeredNodes.remove(ctx.sessionId());
|
||||||
users.remove(id);
|
broadcastNodeCount();
|
||||||
broadcast("Discovery-Leave", "Leave");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void broadcast(String sender, String message) {
|
private static void broadcastNodeCount() {
|
||||||
users.values().forEach(ctx -> ctx.send(users.size()));
|
List<NodeInfo> nodeInfoList = new ArrayList<>(registeredNodes.values());
|
||||||
|
String nodesJson = gson.toJson(nodeInfoList);
|
||||||
|
nodes.values().forEach(ctx -> ctx.send(nodesJson));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record NodeInfo(String hostname, int port) { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>
|
<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>
|
</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
</Console>
|
</Console>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>
|
<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>
|
</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
</Console>
|
</Console>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>
|
<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>
|
</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
</Console>
|
</Console>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>
|
<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>
|
</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
</Console>
|
</Console>
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ services:
|
|||||||
- "8100:8100"
|
- "8100:8100"
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
|
environment:
|
||||||
|
- HOSTNAME=node1
|
||||||
|
- PORT=8100
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres_db
|
- postgres_db
|
||||||
- symphony-discovery
|
- symphony-discovery
|
||||||
@@ -65,6 +68,9 @@ services:
|
|||||||
- "8101:8101"
|
- "8101:8101"
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
|
environment:
|
||||||
|
- HOSTNAME=node2
|
||||||
|
- PORT=8101
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres_db
|
- postgres_db
|
||||||
- symphony-discovery
|
- symphony-discovery
|
||||||
@@ -78,6 +84,9 @@ services:
|
|||||||
- "8102:8102"
|
- "8102:8102"
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
|
environment:
|
||||||
|
- HOSTNAME=node3
|
||||||
|
- PORT=8102
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres_db
|
- postgres_db
|
||||||
- symphony-discovery
|
- symphony-discovery
|
||||||
@@ -91,6 +100,9 @@ services:
|
|||||||
- "8103:8103"
|
- "8103:8103"
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
|
environment:
|
||||||
|
- HOSTNAME=node4
|
||||||
|
- PORT=8103
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres_db
|
- postgres_db
|
||||||
- symphony-discovery
|
- symphony-discovery
|
||||||
@@ -104,6 +116,9 @@ services:
|
|||||||
- "8104:8104"
|
- "8104:8104"
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
|
environment:
|
||||||
|
- HOSTNAME=node5
|
||||||
|
- PORT=8104
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres_db
|
- postgres_db
|
||||||
- symphony-discovery
|
- symphony-discovery
|
||||||
|
|||||||
Reference in New Issue
Block a user