You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/05/24 11:20:44 UTC

[incubator-plc4x] branch feature/notificationSupport updated: removed some duplicated code.

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

sruehl pushed a commit to branch feature/notificationSupport
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/feature/notificationSupport by this push:
     new 136d4b6  removed some duplicated code.
136d4b6 is described below

commit 136d4b665b67082634422e364f19c0ce6a3c0e8d
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu May 24 13:20:41 2018 +0200

    removed some duplicated code.
---
 .../ads/connection/AdsAbstractPlcConnection.java   | 23 +++++++++++---------
 .../java/ads/connection/AdsTcpPlcConnection.java   | 25 ++--------------------
 2 files changed, 15 insertions(+), 33 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsAbstractPlcConnection.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsAbstractPlcConnection.java
index f572731..37b9f3f 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsAbstractPlcConnection.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsAbstractPlcConnection.java
@@ -161,16 +161,7 @@ public abstract class AdsAbstractPlcConnection extends AbstractPlcConnection imp
 
                 CompletableFuture<PlcProprietaryResponse<AdsReadWriteResponse>> getHandelFuture = new CompletableFuture<>();
                 channel.writeAndFlush(new PlcRequestContainer<>(new PlcProprietaryRequest<>(adsReadWriteRequest), getHandelFuture));
-                PlcProprietaryResponse<AdsReadWriteResponse> getHandleResponse;
-                try {
-                    getHandleResponse = getHandelFuture.get(SYMBOL_RESOLVE_TIMEOUT, TimeUnit.MILLISECONDS);
-                } catch (InterruptedException e) {
-                    LOGGER.warn("Interrupted!", e);
-                    Thread.currentThread().interrupt();
-                    throw new PlcRuntimeException(e);
-                } catch (ExecutionException | TimeoutException e) {
-                    throw new PlcRuntimeException(e);
-                }
+                PlcProprietaryResponse<AdsReadWriteResponse> getHandleResponse = getFromFuture(getHandelFuture, SYMBOL_RESOLVE_TIMEOUT);
                 AdsReadWriteResponse response = getHandleResponse.getResponse();
                 if (response.getResult().toAdsReturnCode() != AdsReturnCode.ADS_CODE_0) {
                     throw new PlcRuntimeException("Non error code received " + response.getResult());
@@ -209,6 +200,18 @@ public abstract class AdsAbstractPlcConnection extends AbstractPlcConnection imp
         super.close();
     }
 
+    protected <T> T getFromFuture(CompletableFuture<T> future, long timeout) {
+        try {
+            return future.get(timeout, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            LOGGER.warn("Interrupted!", e);
+            Thread.currentThread().interrupt();
+            throw new PlcRuntimeException(e);
+        } catch (ExecutionException | TimeoutException e) {
+            throw new PlcRuntimeException(e);
+        }
+    }
+
     @Override
     public String toString() {
         return "AdsAbstractPlcConnection{" +
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
index 203a087..a56ad39 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
@@ -49,9 +49,6 @@ import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
 
@@ -175,16 +172,7 @@ public class AdsTcpPlcConnection extends AdsAbstractPlcConnection implements Plc
 
         CompletableFuture<PlcProprietaryResponse<AdsAddDeviceNotificationResponse>> addDeviceFuture = new CompletableFuture<>();
         channel.writeAndFlush(new PlcRequestContainer<>(new PlcProprietaryRequest<>(adsAddDeviceNotificationRequest), addDeviceFuture));
-        PlcProprietaryResponse<AdsAddDeviceNotificationResponse> addDeviceResponse;
-        try {
-            addDeviceResponse = addDeviceFuture.get(ADD_DEVICE_TIMEOUT, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            LOGGER.warn("Interrupted!", e);
-            Thread.currentThread().interrupt();
-            throw new PlcRuntimeException(e);
-        } catch (ExecutionException | TimeoutException e) {
-            throw new PlcRuntimeException(e);
-        }
+        PlcProprietaryResponse<AdsAddDeviceNotificationResponse> addDeviceResponse = getFromFuture(addDeviceFuture, ADD_DEVICE_TIMEOUT);
         AdsAddDeviceNotificationResponse response = addDeviceResponse.getResponse();
         if (response.getResult().toAdsReturnCode() != AdsReturnCode.ADS_CODE_0) {
             throw new PlcRuntimeException("Non error code received " + response.getResult());
@@ -234,16 +222,7 @@ public class AdsTcpPlcConnection extends AdsAbstractPlcConnection implements Plc
             CompletableFuture<PlcProprietaryResponse<AdsDeleteDeviceNotificationResponse>> deleteDeviceFuture = new CompletableFuture<>();
             channel.writeAndFlush(new PlcRequestContainer<>(new PlcProprietaryRequest<>(adsDeleteDeviceNotificationRequest), deleteDeviceFuture));
 
-            PlcProprietaryResponse<AdsDeleteDeviceNotificationResponse> deleteDeviceResponse;
-            try {
-                deleteDeviceResponse = deleteDeviceFuture.get(DEL_DEVICE_TIMEOUT, TimeUnit.MILLISECONDS);
-            } catch (InterruptedException e) {
-                LOGGER.warn("Interrupted!", e);
-                Thread.currentThread().interrupt();
-                throw new PlcRuntimeException(e);
-            } catch (ExecutionException | TimeoutException e) {
-                throw new PlcRuntimeException(e);
-            }
+            PlcProprietaryResponse<AdsDeleteDeviceNotificationResponse> deleteDeviceResponse = getFromFuture(deleteDeviceFuture, DEL_DEVICE_TIMEOUT);
             AdsDeleteDeviceNotificationResponse response = deleteDeviceResponse.getResponse();
             if (response.getResult().toAdsReturnCode() != AdsReturnCode.ADS_CODE_0) {
                 throw new PlcRuntimeException("Non error code received " + response.getResult());

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.