You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/08/07 11:06:12 UTC

[plc4x] 02/02: Added support for read and write requests.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch fix-netty-usage
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit fb4b41cd21b0ac28ff97458f98d02f01ab3277de
Author: julian <j....@pragmaticminds.de>
AuthorDate: Wed Aug 7 13:06:00 2019 +0200

    Added support for read and write requests.
---
 .../plc4x/java/s7/connection/S7PlcConnection.java  | 29 ++++++++++++----------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
index 5c63d12..e83d803 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
@@ -292,17 +292,7 @@ public class S7PlcConnection extends NettyPlcConnection implements PlcReader, Pl
         CompletableFuture<InternalPlcReadResponse> future = new CompletableFuture<>();
         PlcRequestContainer<InternalPlcReadRequest, InternalPlcReadResponse> container =
             new PlcRequestContainer<>(internalReadRequest, future);
-        GenericFutureListener<Future<? super Void>> closeListener = f -> {
-            future.completeExceptionally(new PlcRuntimeException("Connection was unexpectedly closed during read. This is most likely due to a problem in the connection layer."));
-        };
-        channel.closeFuture().addListener(closeListener);
-        channel.writeAndFlush(container).addListener(f -> {
-            if (!f.isSuccess()) {
-                future.completeExceptionally(f.cause());
-            }
-            // Remove the close listener, as it completed
-            channel.closeFuture().removeListener(closeListener);
-        });
+        sendRequest(future, container);
         return future
             .thenApply(PlcReadResponse.class::cast);
     }
@@ -313,13 +303,26 @@ public class S7PlcConnection extends NettyPlcConnection implements PlcReader, Pl
         CompletableFuture<InternalPlcWriteResponse> future = new CompletableFuture<>();
         PlcRequestContainer<InternalPlcWriteRequest, InternalPlcWriteResponse> container =
             new PlcRequestContainer<>(internalWriteRequest, future);
+        sendRequest(future, container);
+        return future
+            .thenApply(PlcWriteResponse.class::cast);
+    }
+
+    /**
+     * Sends the request to the netty channel in a robust manner.
+     */
+    private void sendRequest(CompletableFuture<?> future, PlcRequestContainer<?, ?> container) {
+        GenericFutureListener<Future<? super Void>> closeListener = f -> {
+            future.completeExceptionally(new PlcRuntimeException("Connection was unexpectedly closed during read. This is most likely due to a problem in the connection layer."));
+        };
+        channel.closeFuture().addListener(closeListener);
         channel.writeAndFlush(container).addListener(f -> {
             if (!f.isSuccess()) {
                 future.completeExceptionally(f.cause());
             }
+            // Remove the close listener, as it completed
+            channel.closeFuture().removeListener(closeListener);
         });
-        return future
-            .thenApply(PlcWriteResponse.class::cast);
     }
 
 }