Skip to content

Commit

Permalink
Implement actions for server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
HttpMarco committed Jul 24, 2024
1 parent 45b17af commit 1879b70
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "dev.httpmarco"
version = "1.2.16-SNAPSHOT"
version = "1.2.17-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
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;
import lombok.AccessLevel;
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<E extends Enum<?>> extends CommunicationListener {

@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PROTECTED)
Expand All @@ -20,6 +28,7 @@ public abstract class CommunicationComponent extends CommunicationListener {
private final String hostname;
@Getter(AccessLevel.PROTECTED)
private final int port;
private final Map<E, List<Consumer<ChannelTransmit>>> localActions = new HashMap<>();

public CommunicationComponent(int bossGroupThreads, String hostname, int port) {
this.bossGroup = CommunicationNetworkUtils.createEventLoopGroup(bossGroupThreads);
Expand Down Expand Up @@ -51,4 +60,23 @@ public boolean isAlive() {
public void close() {
bossGroup.shutdownGracefully();
}


public void clientAction(E action, Consumer<ChannelTransmit> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommunicationClientAction> {

private final Bootstrap bootstrap;
private CommunicationClientTransmit channelTransmit;

// only custom client events
private final Map<CommunicationClientAction, List<Consumer<ChannelTransmit>>> localClientActions = new HashMap<>();

public CommunicationClient(String hostname, int port) {
super(0, hostname, port);

Expand Down Expand Up @@ -76,18 +73,4 @@ public void sendPacket(Packet packet) {
this.channelTransmit.sendPacket(packet);
}

public CommunicationClient clientAction(CommunicationClientAction action, Consumer<ChannelTransmit> 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -42,11 +42,11 @@ public <P extends Packet> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Packet> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CommunicationServerAction> {

@Getter
private final List<ChannelTransmit> channels = new ArrayList<>();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.httpmarco.osgan.networking.server;

public enum CommunicationServerAction {

CLIENT_CONNECT,
CLIENT_DISCONNECT

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) -> {
Expand All @@ -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();

Expand Down

0 comments on commit 1879b70

Please sign in to comment.