-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from HttpMarco/netty/rework
Netty/rework
- Loading branch information
Showing
56 changed files
with
958 additions
and
1,199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
...y/src/main/java/dev/httpmarco/osgan/networking/AbstractCommunicationComponentBuilder.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
osgan-netty/src/main/java/dev/httpmarco/osgan/networking/ChannelConsumer.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
osgan-netty/src/main/java/dev/httpmarco/osgan/networking/ChannelTransmit.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationComponentHandler.java
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
osgan-netty/src/main/java/dev/httpmarco/osgan/networking/CommunicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.