Don’t let clients handle connection failures
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / FcpReplySequence.java
index 2c2dfe1..8bdaf5c 100644 (file)
@@ -4,6 +4,7 @@ import java.io.IOException;
 import java.util.Objects;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
@@ -66,6 +67,7 @@ public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener
        private final FcpConnection fcpConnection;
        private final Queue<FcpMessage> messages = new ConcurrentLinkedQueue<>();
        private final AtomicReference<String> identifier = new AtomicReference<>();
+       private final AtomicReference<Throwable> connectionFailureReason = new AtomicReference<>();
 
        public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) {
                this.executorService = MoreExecutors.listeningDecorator(executorService);
@@ -84,17 +86,21 @@ public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener
                messages.add(fcpMessage);
                return executorService.submit(() -> {
                        synchronized (syncObject) {
-                               while (!isFinished() || !messages.isEmpty()) {
+                               while ((connectionFailureReason.get() == null) && (!isFinished() || !messages.isEmpty())) {
                                        while (messages.peek() != null) {
                                                FcpMessage message = messages.poll();
                                                fcpConnection.sendMessage(message);
                                        }
-                                       if (isFinished()) {
+                                       if (isFinished() || (connectionFailureReason.get() != null)) {
                                                continue;
                                        }
                                        syncObject.wait();
                                }
                        }
+                       Throwable throwable = connectionFailureReason.get();
+                       if (throwable != null) {
+                               throw new ExecutionException(throwable);
+                       }
                        return getResult();
                });
        }
@@ -143,7 +149,7 @@ public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener
        }
 
        private void consumeClose(Throwable throwable) {
-               consumeConnectionClosed(throwable);
+               connectionFailureReason.set(throwable);
                notifySyncObject();
        }
 
@@ -423,6 +429,4 @@ public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener
                consumeClose(throwable);
        }
 
-       protected void consumeConnectionClosed(Throwable throwable) { }
-
 }