From 1879b703390fee9b7db23fb889fa6886932db40b Mon Sep 17 00:00:00 2001 From: HttpMarco Date: Wed, 24 Jul 2024 15:47:07 +0200 Subject: [PATCH] Implement actions for server and client --- build.gradle.kts | 2 +- .../networking/CommunicationComponent.java | 30 ++++++++++++++++++- .../client/CommunicationClient.java | 19 +----------- .../client/CommunicationClientTransmit.java | 8 ++--- .../osgan/networking/codec/PacketEncoder.java | 5 ---- .../server/CommunicationServer.java | 16 ++++++++-- .../server/CommunicationServerAction.java | 8 +++++ .../osgan/test/networking/ServerTest.java | 11 ++++++- 8 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServerAction.java diff --git a/build.gradle.kts b/build.gradle.kts index fac1eed..a2f15ee 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ allprojects { apply(plugin = "maven-publish") group = "dev.httpmarco" - version = "1.2.16-SNAPSHOT" + version = "1.2.17-SNAPSHOT" repositories { mavenCentral() diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationComponent.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationComponent.java index 0c65c32..5c04055 100644 --- a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationComponent.java +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationComponent.java @@ -1,5 +1,6 @@ package dev.httpmarco.osgan.networking; +import dev.httpmarco.osgan.networking.channel.ChannelTransmit; import io.netty5.channel.Channel; import io.netty5.channel.EventLoopGroup; import io.netty5.util.concurrent.FutureListener; @@ -7,9 +8,16 @@ import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; @Accessors(fluent = true) -public abstract class CommunicationComponent extends CommunicationListener { +public abstract class CommunicationComponent> extends CommunicationListener { @Getter(AccessLevel.PROTECTED) @Setter(AccessLevel.PROTECTED) @@ -20,6 +28,7 @@ public abstract class CommunicationComponent extends CommunicationListener { private final String hostname; @Getter(AccessLevel.PROTECTED) private final int port; + private final Map>> localActions = new HashMap<>(); public CommunicationComponent(int bossGroupThreads, String hostname, int port) { this.bossGroup = CommunicationNetworkUtils.createEventLoopGroup(bossGroupThreads); @@ -51,4 +60,23 @@ public boolean isAlive() { public void close() { bossGroup.shutdownGracefully(); } + + + public void clientAction(E action, Consumer runnable) { + var currentActionCollection = this.localActions.getOrDefault(action, new ArrayList<>()); + currentActionCollection.add(runnable); + this.localActions.put(action, currentActionCollection); + } + + protected void callClientAction(E action, @Nullable ChannelTransmit transmit) { + if (this.localActions.containsKey(action)) { + for (var runnable : localActions.get(action)) { + runnable.accept(transmit); + } + } + } + + protected void callClientAction(E action) { + this.callClientAction(action, null); + } } \ No newline at end of file diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClient.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClient.java index efee5eb..4df6cf5 100644 --- a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClient.java +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClient.java @@ -16,14 +16,11 @@ import java.util.Map; import java.util.function.Consumer; -public final class CommunicationClient extends CommunicationComponent { +public final class CommunicationClient extends CommunicationComponent { private final Bootstrap bootstrap; private CommunicationClientTransmit channelTransmit; - // only custom client events - private final Map>> localClientActions = new HashMap<>(); - public CommunicationClient(String hostname, int port) { super(0, hostname, port); @@ -76,18 +73,4 @@ public void sendPacket(Packet packet) { this.channelTransmit.sendPacket(packet); } - public CommunicationClient clientAction(CommunicationClientAction action, Consumer runnable) { - var currentActionCollection = this.localClientActions.getOrDefault(action, new ArrayList<>()); - currentActionCollection.add(runnable); - this.localClientActions.put(action, currentActionCollection); - return this; - } - - private void callClientAction(CommunicationClientAction action) { - if (this.localClientActions.containsKey(action)) { - for (var runnable : this.localClientActions.get(action)) { - runnable.accept(this.channelTransmit); - } - } - } } \ No newline at end of file diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClientTransmit.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClientTransmit.java index 7a75320..c57d8ee 100644 --- a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClientTransmit.java +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/client/CommunicationClientTransmit.java @@ -14,9 +14,9 @@ public final class CommunicationClientTransmit extends ChannelTransmit { - private final CommunicationComponent communicationComponent; + private final CommunicationComponent communicationComponent; - public CommunicationClientTransmit(CommunicationComponent communicationComponent, String id, Channel channel) { + public CommunicationClientTransmit(CommunicationComponent communicationComponent, String id, Channel channel) { super(id, channel); this.communicationComponent = communicationComponent; } @@ -42,11 +42,11 @@ public

void request(String id, CommunicationProperty property } @Contract("_ -> new") - public static @NotNull CommunicationClientTransmit empty(CommunicationComponent communicationComponent) { + public static @NotNull CommunicationClientTransmit empty(CommunicationComponent communicationComponent) { return new CommunicationClientTransmit(communicationComponent, null, null); } - public static @NotNull CommunicationClientTransmit of(CommunicationComponent communicationComponent, @NotNull ChannelTransmit transmit) { + public static @NotNull CommunicationClientTransmit of(CommunicationComponent communicationComponent, @NotNull ChannelTransmit transmit) { var clientTransmit = new CommunicationClientTransmit(communicationComponent, transmit.id(), transmit.channel()); clientTransmit.listeners().putAll(transmit.listeners()); clientTransmit.requests().putAll(transmit.requests()); diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/codec/PacketEncoder.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/codec/PacketEncoder.java index dd4dd9b..6d540c6 100644 --- a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/codec/PacketEncoder.java +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/codec/PacketEncoder.java @@ -2,17 +2,12 @@ import dev.httpmarco.osgan.networking.packet.Packet; import dev.httpmarco.osgan.networking.packet.PacketBuffer; -import dev.httpmarco.osgan.reflections.Reflections; import io.netty5.buffer.Buffer; import io.netty5.channel.ChannelHandlerContext; import io.netty5.handler.codec.MessageToByteEncoder; import lombok.SneakyThrows; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.util.HashMap; -import java.util.UUID; public class PacketEncoder extends MessageToByteEncoder { diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServer.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServer.java index 5040ca6..c03dcea 100644 --- a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServer.java +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServer.java @@ -5,6 +5,7 @@ import dev.httpmarco.osgan.networking.CommunicationTransmitHandler; import dev.httpmarco.osgan.networking.channel.ChannelInitializer; import dev.httpmarco.osgan.networking.channel.ChannelTransmit; +import dev.httpmarco.osgan.networking.client.CommunicationClientAction; import dev.httpmarco.osgan.networking.packet.Packet; import io.netty5.bootstrap.ServerBootstrap; import io.netty5.channel.ChannelOption; @@ -13,10 +14,13 @@ import lombok.experimental.Accessors; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.function.Consumer; @Accessors(fluent = true) -public final class CommunicationServer extends CommunicationComponent { +public final class CommunicationServer extends CommunicationComponent { @Getter private final List channels = new ArrayList<>(); @@ -34,8 +38,14 @@ public void initialize() { .childHandler(new ChannelInitializer(new CommunicationTransmitHandler( (it) -> this.channels, (it, channel) -> channel.call(it, channel), - (it) -> channels.add(CommunicationServerTransmit.of(it, this)), - (it) -> channels.removeIf(channel -> channel.channel().equals(it.channel())) + (it) -> { + callClientAction(CommunicationServerAction.CLIENT_CONNECT, it); + channels.add(CommunicationServerTransmit.of(it, this)); + }, + (it) -> { + callClientAction(CommunicationServerAction.CLIENT_DISCONNECT, it); + channels.removeIf(channel -> channel.channel().equals(it.channel())); + } ))) // all channel options diff --git a/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServerAction.java b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServerAction.java new file mode 100644 index 0000000..d6221fd --- /dev/null +++ b/osgan-netty/src/main/java/dev/httpmarco/osgan/networking/server/CommunicationServerAction.java @@ -0,0 +1,8 @@ +package dev.httpmarco.osgan.networking.server; + +public enum CommunicationServerAction { + + CLIENT_CONNECT, + CLIENT_DISCONNECT + +} diff --git a/osgan-netty/src/test/java/dev/httpmarco/osgan/test/networking/ServerTest.java b/osgan-netty/src/test/java/dev/httpmarco/osgan/test/networking/ServerTest.java index 6801a6d..e7b14e1 100644 --- a/osgan-netty/src/test/java/dev/httpmarco/osgan/test/networking/ServerTest.java +++ b/osgan-netty/src/test/java/dev/httpmarco/osgan/test/networking/ServerTest.java @@ -4,6 +4,7 @@ import dev.httpmarco.osgan.networking.client.CommunicationClient; import dev.httpmarco.osgan.networking.client.CommunicationClientAction; import dev.httpmarco.osgan.networking.server.CommunicationServer; +import dev.httpmarco.osgan.networking.server.CommunicationServerAction; import lombok.SneakyThrows; import org.junit.jupiter.api.Test; @@ -17,7 +18,7 @@ public void handle() { var server = new CommunicationServer("127.0.0.1", 8080); - var client = new CommunicationClient("127.0.0.1", 8081); + var client = new CommunicationClient("127.0.0.1", 8080); client.clientAction(CommunicationClientAction.CONNECTED, (it) -> { @@ -34,6 +35,14 @@ public void handle() { }); + server.clientAction(CommunicationServerAction.CLIENT_CONNECT, transmit -> { + System.out.println("connected"); + }); + + server.clientAction(CommunicationServerAction.CLIENT_DISCONNECT, transmit -> { + System.out.println("disconnected"); + }); + server.initialize(); client.initialize();