From 0c7d92bfa522df38ef8412f498dd37206169795c Mon Sep 17 00:00:00 2001 From: Alemiz Date: Sun, 12 Nov 2023 13:11:22 +0100 Subject: [PATCH] Add option to have multiple listeners --- .../vortex/common/node/VortexNode.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/alemiz/stargate/vortex/common/node/VortexNode.java b/common/src/main/java/alemiz/stargate/vortex/common/node/VortexNode.java index 878c530..9d78900 100644 --- a/common/src/main/java/alemiz/stargate/vortex/common/node/VortexNode.java +++ b/common/src/main/java/alemiz/stargate/vortex/common/node/VortexNode.java @@ -31,6 +31,7 @@ import io.netty.util.concurrent.Promise; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import lombok.extern.log4j.Log4j2; import java.net.InetSocketAddress; @@ -53,7 +54,7 @@ public abstract class VortexNode extends SimpleChannelInboundHandler listeners = Collections.synchronizedSet(new ObjectOpenHashSet<>()); private ScheduledFuture pingFuture; private ScheduledFuture responsesFuture; @@ -123,8 +124,12 @@ protected void channelRead0(ChannelHandlerContext ctx, VortexPacket packet) thro responseHandle = this.pendingResponses.remove(((VortexResponse) packet).getResponseId()); } - if (this.vortexPacketListener != null && packet.handle(this.vortexPacketListener)) { - return; + if (!this.listeners.isEmpty()) { + for (VortexPacketListener listener : this.listeners) { + if (packet.handle(listener)) { + return; + } + } } if (this.handleInternal(packet)) { @@ -284,12 +289,13 @@ public long getLatency() { return this.latency; } - public VortexPacketListener getVortexPacketListener() { - return this.vortexPacketListener; + @Deprecated + public void setVortexPacketListener(VortexPacketListener vortexPacketListener) { + this.addVortexPacketListener(vortexPacketListener); } - public void setVortexPacketListener(VortexPacketListener vortexPacketListener) { - this.vortexPacketListener = vortexPacketListener; + public void addVortexPacketListener(VortexPacketListener vortexPacketListener) { + this.listeners.add(vortexPacketListener); } public InetSocketAddress getAddress() {