Skip to content

Commit

Permalink
Add option to have multiple listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 committed Nov 12, 2023
1 parent f83167b commit 0c7d92b
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,7 +54,7 @@ public abstract class VortexNode extends SimpleChannelInboundHandler<VortexPacke
protected final VortexNodeOwner vortexParent;
protected final StarGateSession session;

protected VortexPacketListener vortexPacketListener;
protected Set<VortexPacketListener> listeners = Collections.synchronizedSet(new ObjectOpenHashSet<>());

private ScheduledFuture<?> pingFuture;
private ScheduledFuture<?> responsesFuture;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0c7d92b

Please sign in to comment.