Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netty/rework #11

Merged
merged 29 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
98ca6e6
fixed some things
Thiies Apr 7, 2024
65e5a72
Add better reflection system
HttpMarco Apr 8, 2024
0ac3402
Merge remote-tracking branch 'origin/dev' into dev
HttpMarco Apr 8, 2024
54aa453
Merge pull request #8 from HttpMarco/master
HttpMarco Apr 8, 2024
b415763
Add method for collect all superclass and class fields
HttpMarco Apr 8, 2024
2f35484
Merge remote-tracking branch 'origin/dev' into dev
HttpMarco Apr 8, 2024
7dcb0e6
Update version and remove unused import
Apr 9, 2024
3e7d65f
removed channel id
Thiies Apr 12, 2024
f92a958
Merge branch 'dev' of https://github.com/HttpMarco/osgan into dev
Thiies Apr 12, 2024
f9b9422
set default for onactive and oninactive
Thiies Apr 12, 2024
126d7fc
fixed no responder exception
Thiies Apr 14, 2024
b8f5dc2
Fix reflection return mapping
HttpMarco Apr 19, 2024
1293a51
Implement createDirectoryIfNotExists method
HttpMarco May 14, 2024
9dda7b7
Update version
HttpMarco May 14, 2024
7586c69
Update version
HttpMarco May 14, 2024
0ae341c
Small hot fix on files
HttpMarco May 14, 2024
4e44372
Fix files creation
HttpMarco May 14, 2024
26093e7
Fix netty error out print
HttpMarco May 21, 2024
c2bba43
Remove useless logging information
HttpMarco May 21, 2024
825ca5c
Init commit of rework
HttpMarco May 25, 2024
709f337
Work on netty things
HttpMarco May 25, 2024
9b571b9
Rework netty api
HttpMarco Jun 4, 2024
319b197
Fix small netty things
HttpMarco Jun 4, 2024
0e56875
Update version
HttpMarco Jun 4, 2024
c0fad2c
Implement properties and append properties
HttpMarco Jun 4, 2024
3c5ce9c
Add simple request method
HttpMarco Jun 4, 2024
8f917b3
Append success support
HttpMarco Jun 4, 2024
0b27e3e
Remove kube module
HttpMarco Jun 6, 2024
52e0d02
Update gradle wrapper
HttpMarco Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions 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.1.4-SNAPSHOT"
version = "1.2.5-SNAPSHOT"

repositories {
mavenCentral()
Expand All @@ -22,6 +22,9 @@ allprojects {

implementation(rootProject.libs.annotations)
annotationProcessor(rootProject.libs.annotations)

testAnnotationProcessor(rootProject.libs.lombok)
testImplementation(rootProject.libs.lombok)
}

tasks.withType<JavaCompile> {
Expand Down Expand Up @@ -66,12 +69,12 @@ allprojects {
nexusPublishing {
repositories {
sonatype {
nexusUrl.set(uri("https://nexus.bytemc.de/repository/maven-public/"))
snapshotRepositoryUrl.set(uri("https://nexus.bytemc.de/repository/maven-public/"))
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))

username.set(System.getenv("BYTEMC_REPO_USER")?.toString() ?: "")
password.set(System.getenv("BYTEMC_REPO_PASSWORD")?.toString() ?: "")
username.set(System.getenv("ossrhUsername")?.toString() ?: "")
password.set(System.getenv("ossrhPassword")?.toString() ?: "")
}
}
useStaging.set(!project.rootProject.version.toString().endsWith("-SNAPSHOT"))
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jan 23 09:21:27 CET 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public static void copyInputStreamToFile(InputStream inputStream, File file) thr
}
}

@SneakyThrows
public static void createDirectoryIfNotExists(Path path) {
if(!java.nio.file.Files.exists(path)) {
java.nio.file.Files.createDirectory(path);
}
}

@SneakyThrows
public static void writeString(Path path, String content) {
java.nio.file.Files.writeString(path, content);
Expand Down
14 changes: 0 additions & 14 deletions osgan-kubernetes/build.gradle.kts

This file was deleted.

1 change: 1 addition & 0 deletions osgan-netty/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
api(project(":osgan-files"))
api(project(":osgan-reflections"))

testImplementation(libs.gson)
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
package dev.httpmarco.osgan.networking;

import dev.httpmarco.osgan.files.json.JsonObjectSerializer;
import dev.httpmarco.osgan.networking.listening.ChannelPacketListener;
import dev.httpmarco.osgan.networking.request.PacketResponder;
import dev.httpmarco.osgan.networking.request.RequestHandler;
import dev.httpmarco.osgan.utils.executers.FutureResult;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Getter
@Accessors(fluent = true)
public abstract class CommunicationComponent<M extends Metadata> {
public abstract class CommunicationComponent extends CommunicationListener {

@Setter
@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PROTECTED)
private FutureResult<Void> connectionFuture = new FutureResult<>();

private final M metadata;
@Getter(AccessLevel.PROTECTED)
private final EventLoopGroup bossGroup;
private final Map<Class<? extends Packet>, List<ChannelPacketListener<? extends Packet>>> packetListeners = new HashMap<>();
private final RequestHandler requestHandler;
@Getter(AccessLevel.PROTECTED)
private final String hostname;
@Getter(AccessLevel.PROTECTED)
private final int port;

public CommunicationComponent(M metadata, int workerThreads) {
this.bossGroup = NetworkUtils.createEventLoopGroup(workerThreads);
this.metadata = metadata;
this.requestHandler = new RequestHandler(this);
public CommunicationComponent(int bossGroupThreads, String hostname, int port) {
this.bossGroup = CommunicationNetworkUtils.createEventLoopGroup(bossGroupThreads);
this.hostname = hostname;
this.port = port;
}

public abstract <P extends Packet> void sendPacket(P packet);

public abstract <P extends Packet> void sendPacket(Channel channel, P packet);

public abstract <P extends Packet> void redirectPacket(String id, P packet);
public abstract void initialize();

public FutureListener<? super Channel> handleConnectionRelease() {
return it -> {
Expand All @@ -65,25 +53,4 @@ public void close() {
bossGroup.shutdownGracefully();
}

public void callPacketReceived(ChannelTransmit transmit, Packet packet) {
if (this.packetListeners.containsKey(packet.getClass())) {
this.packetListeners.get(packet.getClass()).forEach(it -> it.listenWithMapping(transmit, packet));
}
}

public <P extends Packet> void listen(Class<P> packetClass, ChannelPacketListener<P> listener) {
this.packetListeners.computeIfAbsent(packetClass, it -> new ArrayList<>()).add(listener);
}

public <T extends Packet> void request(String id, Class<T> responsePacket, Consumer<T> consumer) {
this.requestHandler.request(id, responsePacket, consumer);
}

public <T extends Packet> void request(String id, JsonObjectSerializer properties, Class<T> responsePacket, Consumer<T> consumer) {
this.requestHandler.request(id, properties, responsePacket, consumer);
}

public <T extends Packet> void registerResponder(String id, PacketResponder<T> responder) {
this.requestHandler.registerResponder(id, responder);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.httpmarco.osgan.networking;

import dev.httpmarco.osgan.networking.channel.ChannelTransmit;
import dev.httpmarco.osgan.networking.packet.BadRequestPacket;
import dev.httpmarco.osgan.networking.packet.Packet;
import dev.httpmarco.osgan.networking.packet.RequestPacket;
import dev.httpmarco.osgan.networking.packet.RequestResponsePacket;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

@Getter
@Accessors(fluent = true)
public abstract class CommunicationListener {

private final Map<Class<? extends Packet>, List<BiConsumer<ChannelTransmit, Packet>>> listeners = new HashMap<>();
private final Map<String, Function<CommunicationProperty, Packet>> responders = new HashMap<>();
private final Map<UUID, Consumer<Packet>> requests = new HashMap<>();

@SuppressWarnings("unchecked")
public <P extends Packet> void listen(Class<P> listeningClass, BiConsumer<ChannelTransmit, P> packetCallback) {
var packetListeners = listeners.getOrDefault(listeningClass, new ArrayList<>());
packetListeners.add((BiConsumer<ChannelTransmit, Packet>) packetCallback);
listeners.put(listeningClass, packetListeners);
}

public <P extends Packet> void listen(Class<P> listeningClass, Consumer<P> packetCallback) {
this.listen(listeningClass, (channelTransmit, packet) -> packetCallback.accept(packet));
}

@SuppressWarnings("unchecked")
public <P extends Packet> void request(String id, CommunicationProperty property, Class<P> packet, Consumer<P> packetCallback) {
var uuid = UUID.randomUUID();
this.requests.put(uuid, (Consumer<Packet>) packetCallback);
sendPacket(new RequestPacket(id, uuid, property));
}

public <P extends Packet> void request(String id, Class<P> packet, Consumer<P> packetCallback) {
request(id, new CommunicationProperty(), packet, packetCallback);
}


public void callResponder(ChannelTransmit channelTransmit, @NotNull RequestPacket requestPacket) {
if (!responders.containsKey(requestPacket.id())) {
channelTransmit.sendPacket(new BadRequestPacket(requestPacket.uuid()));
System.out.println("Found no responder for: " + requestPacket.id());
return;
}

var response = responders.get(requestPacket.id()).apply(requestPacket.property());
channelTransmit.sendPacket(new RequestResponsePacket(requestPacket.uuid(), response));
}

public void responder(String id, Function<CommunicationProperty, Packet> packetFunction) {
this.responders.put(id, packetFunction);
}

public <P extends Packet> boolean call(@NotNull P packet, ChannelTransmit channelTransmit) {
if (packet instanceof RequestPacket requestPacket) {
this.callResponder(channelTransmit, requestPacket);
return true;
}

if (packet instanceof RequestResponsePacket requestResponsePacket) {
if (!this.requests.containsKey(requestResponsePacket.uuid())) {
return true;
}
this.requests.get(requestResponsePacket.uuid()).accept(requestResponsePacket.response());
this.requests.remove(requestResponsePacket.uuid());
return true;
}

if (packet instanceof BadRequestPacket badRequestPacket) {
this.requests.remove(badRequestPacket.uuid());
System.out.println("Invalid request from: " + badRequestPacket.uuid());
return true;
}

if (!this.listeners.containsKey(packet.getClass())) {
return false;
}

for (var consumer : this.listeners.get(packet.getClass())) {
consumer.accept(channelTransmit, packet);
}
return true;
}

public abstract void sendPacket(Packet packet);
}
Loading
Loading