Ignore identifier when consuming unknown messages
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientPutCommandImpl.java
index f0a6706..0228cce 100644 (file)
@@ -54,31 +54,31 @@ class ClientPutCommandImpl implements ClientPutCommand {
        }
 
        @Override
-       public Keyed<Optional<Key>> redirectTo(Key key) {
-               this.redirectUri.set(Objects.requireNonNull(key, "key must not be null").getKey());
+       public WithUri<Optional<Key>> redirectTo(String uri) {
+               this.redirectUri.set(Objects.requireNonNull(uri, "uri must not be null"));
                return this::key;
        }
 
        @Override
-       public Keyed<Optional<Key>> from(File file) {
+       public WithUri<Optional<Key>> from(File file) {
                this.file.set(Objects.requireNonNull(file, "file must not be null"));
                return this::key;
        }
 
        @Override
-       public Lengthed<Keyed<Optional<Key>>> from(InputStream inputStream) {
+       public WithLength<WithUri<Optional<Key>>> from(InputStream inputStream) {
                payload.set(Objects.requireNonNull(inputStream, "inputStream must not be null"));
                return this::length;
        }
 
-       private Keyed<Optional<Key>> length(long length) {
+       private WithUri<Optional<Key>> length(long length) {
                this.length.set(length);
                return this::key;
        }
 
-       private ListenableFuture<Optional<Key>> key(Key key) {
+       private ListenableFuture<Optional<Key>> key(String uri) {
                String identifier = new RandomIdentifierGenerator().generate();
-               ClientPut clientPut = createClientPutCommand(key.getKey(), identifier);
+               ClientPut clientPut = createClientPutCommand(uri, identifier);
                return threadPool.submit(() -> new ClientPutReplySequence().send(clientPut).get());
        }
 
@@ -119,7 +119,6 @@ class ClientPutCommandImpl implements ClientPutCommand {
        private class ClientPutReplySequence extends FcpReplySequence<Optional<Key>> {
 
                private final AtomicReference<FcpMessage> originalClientPut = new AtomicReference<>();
-               private final AtomicReference<String> identifier = new AtomicReference<>();
                private final AtomicReference<String> directory = new AtomicReference<>();
                private final AtomicReference<Key> finalKey = new AtomicReference<>();
                private final AtomicBoolean putFinished = new AtomicBoolean();
@@ -141,7 +140,6 @@ class ClientPutCommandImpl implements ClientPutCommand {
                @Override
                public ListenableFuture<Optional<Key>> send(FcpMessage fcpMessage) throws IOException {
                        originalClientPut.set(fcpMessage);
-                       identifier.set(fcpMessage.getField("Identifier"));
                        String filename = fcpMessage.getField("Filename");
                        if (filename != null) {
                                directory.set(new File(filename).getParent());
@@ -151,49 +149,39 @@ class ClientPutCommandImpl implements ClientPutCommand {
 
                @Override
                protected void consumePutSuccessful(PutSuccessful putSuccessful) {
-                       if (putSuccessful.getIdentifier().equals(identifier.get())) {
-                               finalKey.set(new Key(putSuccessful.getURI()));
-                               putFinished.set(true);
-                       }
+                       finalKey.set(new Key(putSuccessful.getURI()));
+                       putFinished.set(true);
                }
 
                @Override
                protected void consumePutFailed(PutFailed putFailed) {
-                       if (putFailed.getIdentifier().equals(identifier.get())) {
-                               putFinished.set(true);
-                       }
+                       putFinished.set(true);
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       if (protocolError.getIdentifier().equals(identifier.get()) && (protocolError.getCode() == 25)) {
+                       if (protocolError.getCode() == 25) {
+                               setIdentifier(directory.get());
                                sendMessage(new TestDDARequest(directory.get(), true, false));
+                       } else {
+                               putFinished.set(true);
                        }
                }
 
                @Override
                protected void consumeTestDDAReply(TestDDAReply testDDAReply) {
-                       if (testDDAReply.getDirectory().equals(directory.get())) {
-                               try {
-                                       String readContent = Files.readAllLines(new File(testDDAReply.getReadFilename()).toPath()).get(0);
-                                       sendMessage(new TestDDAResponse(directory.get(), readContent));
-                               } catch (IOException e) {
-                                       e.printStackTrace();
-                                       sendMessage(new TestDDAResponse(directory.get(), "failed-to-read"));
-                               }
+                       try {
+                               String readContent = Files.readAllLines(new File(testDDAReply.getReadFilename()).toPath()).get(0);
+                               sendMessage(new TestDDAResponse(directory.get(), readContent));
+                       } catch (IOException e) {
+                               sendMessage(new TestDDAResponse(directory.get(), "failed-to-read"));
                        }
                }
 
                @Override
                protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) {
-                       if (testDDAComplete.getDirectory().equals(directory.get())) {
-                               sendMessage(originalClientPut.get());
-                       }
-               }
-
-               @Override
-               protected void consumeConnectionClosed(Throwable throwable) {
-                       putFinished.set(true);
+                       setIdentifier(originalClientPut.get().getField("Identifier"));
+                       sendMessage(originalClientPut.get());
                }
 
        }