X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2Fquelaton%2FFcpReplySequence.java;h=afaac4c614d280807f033007e4c28a4cf65ad049;hb=7c28553f5be04a7db175af2612df65afe54d29ce;hp=03d1b0b677b8b98a3dffd70e3438d64694190a0e;hpb=f5ba10406d666197d448956e4340254c4f5593b6;p=jFCPlib.git diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java index 03d1b0b..afaac4c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java @@ -1,10 +1,15 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import net.pterodactylus.fcp.AllData; +import net.pterodactylus.fcp.BaseMessage; import net.pterodactylus.fcp.CloseConnectionDuplicateClientName; import net.pterodactylus.fcp.ConfigData; import net.pterodactylus.fcp.DataFound; @@ -59,25 +64,34 @@ public abstract class FcpReplySequence implements AutoCloseable, FcpListener private final Object syncObject = new Object(); private final ListeningExecutorService executorService; private final FcpConnection fcpConnection; + private final Queue messages = new ConcurrentLinkedQueue<>(); + private final AtomicReference identifier = new AtomicReference<>(); public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) { this.executorService = MoreExecutors.listeningDecorator(executorService); this.fcpConnection = fcpConnection; } + protected void setIdentifier(String identifier) { + this.identifier.set(identifier); + } + protected abstract boolean isFinished(); public ListenableFuture send(FcpMessage fcpMessage) throws IOException { - try { + setIdentifier(fcpMessage.getField("Identifier")); fcpConnection.addFcpListener(this); - - } catch (Throwable throwable) { - throwable.printStackTrace(); - } - fcpConnection.sendMessage(fcpMessage); + messages.add(fcpMessage); return executorService.submit(() -> { synchronized (syncObject) { - while (!isFinished()) { + while (!isFinished() || !messages.isEmpty()) { + while (messages.peek() != null) { + FcpMessage message = messages.poll(); + fcpConnection.sendMessage(message); + } + if (isFinished()) { + continue; + } syncObject.wait(); } } @@ -85,6 +99,17 @@ public abstract class FcpReplySequence implements AutoCloseable, FcpListener }); } + protected void sendMessage(FcpMessage fcpMessage) { + messages.add(fcpMessage); + notifySyncObject(); + } + + private void notifySyncObject() { + synchronized (syncObject) { + syncObject.notifyAll(); + } + } + protected R getResult() { return null; } @@ -94,25 +119,28 @@ public abstract class FcpReplySequence implements AutoCloseable, FcpListener fcpConnection.removeFcpListener(this); } - private void consume(Consumer consumer, M message) { - consumer.accept(message); - synchronized (syncObject) { - syncObject.notifyAll(); + private void consume(Consumer consumer, M message) { + consume(consumer, message, "Identifier"); + } + + private void consume(Consumer consumer, M message, + String identifier) { + if (Objects.equals(message.getField(identifier), this.identifier.get())) { + consumer.accept(message); + notifySyncObject(); } } private void consumeUnknown(FcpMessage fcpMessage) { - consumeUnknownMessage(fcpMessage); - synchronized (syncObject) { - syncObject.notifyAll(); + if (Objects.equals(fcpMessage.getField("Identifier"), identifier.get())) { + consumeUnknownMessage(fcpMessage); + notifySyncObject(); } } private void consumeClose(Throwable throwable) { consumeConnectionClosed(throwable); - synchronized (syncObject) { - syncObject.notifyAll(); - } + notifySyncObject(); } @Override @@ -181,14 +209,14 @@ public abstract class FcpReplySequence implements AutoCloseable, FcpListener @Override public final void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) { - consume(this::consumeTestDDAReply, testDDAReply); + consume(this::consumeTestDDAReply, testDDAReply, "Directory"); } protected void consumeTestDDAReply(TestDDAReply testDDAReply) { } @Override public final void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) { - consume(this::consumeTestDDAComplete, testDDAComplete); + consume(this::consumeTestDDAComplete, testDDAComplete, "Directory"); } protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { }