You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2021/05/15 11:26:46 UTC

[plc4x] branch feature/native_opua_client updated: Started to clean up

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

hutcheb pushed a commit to branch feature/native_opua_client
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/native_opua_client by this push:
     new caddf0c  Started to clean up
caddf0c is described below

commit caddf0ce7c6a5cb78c9fea88966d186ba011d0c3
Author: hutcheb <be...@gmail.com>
AuthorDate: Sat May 15 07:26:14 2021 -0400

    Started to clean up
---
 .../java/opcua/protocol/OpcuaProtocolLogic.java    |  68 ++-------
 .../opcua/protocol/OpcuaSubscriptionHandle.java    | 158 +++++++++++----------
 2 files changed, 98 insertions(+), 128 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
index 215dc8e..1cd4d8d 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
@@ -133,6 +133,7 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
         for (Map.Entry<Long, OpcuaSubscriptionHandle> subscriber : subscriptions.entrySet()) {
             subscriber.getValue().stopSubscriber();
         }
+
         channel.onDisconnect(context);
     }
 
@@ -781,7 +782,6 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
 
     private PlcWriteResponse writeResponse(DefaultPlcWriteRequest request, WriteResponse writeResponse) {
         Map<String, PlcResponseCode> responseMap = new HashMap<>();
-
         StatusCode[] results = writeResponse.getResults();
         Iterator<String> responseIterator = request.getFieldNames().iterator();
         for (int i = 0; i < request.getFieldNames().size(); i++ ) {
@@ -798,7 +798,6 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
                     responseMap.put(fieldName, PlcResponseCode.REMOTE_ERROR);
             }
         }
-
         return new DefaultPlcWriteResponse(request, responseMap);
     }
 
@@ -807,24 +806,15 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
     public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
         CompletableFuture<PlcSubscriptionResponse> future = CompletableFuture.supplyAsync(() -> {
             Map<String, ResponseItem<PlcSubscriptionHandle>> values = new HashMap<>();
-
-            boolean isFirst = true;
             long subscriptionId = -1L;
-            List<MonitoredItemCreateRequest> requestList = new LinkedList<>();
-
             ArrayList<String> fields = new ArrayList<>( subscriptionRequest.getFieldNames() );
-
-            //long cycleTime = ((DefaultPlcSubscriptionField) subscriptionRequest.getField(fields.get(0))).getDuration().orElse(Duration.ofMillis(50)).toMillis();
-            long cycleTime = (Duration.ofMillis(50)).toMillis();
-
+            long cycleTime = ((DefaultPlcSubscriptionField) subscriptionRequest.getField(fields.get(0))).getDuration().orElse(Duration.ofMillis(1000)).toMillis();
 
             try {
                 CompletableFuture<CreateSubscriptionResponse> subscription = onSubscribeCreateSubscription(cycleTime);
                 CreateSubscriptionResponse response = subscription.get(SecureChannel.REQUEST_TIMEOUT_LONG, TimeUnit.MILLISECONDS);
                 subscriptionId = response.getSubscriptionId();
-
                 subscriptions.put(subscriptionId, new OpcuaSubscriptionHandle(context, this, channel, subscriptionRequest, subscriptionId, cycleTime));
-
             } catch (InterruptedException e) {
                 Thread.currentThread().interrupt();
                 LOGGER.warn("Unable to subscribe Elements because of: {}", e.getMessage());
@@ -899,25 +889,27 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
             };
 
             /* Functional Consumer example using inner class */
-            Consumer<TimeoutException> timeout = t -> {
-
+            Consumer<TimeoutException> timeout = e -> {
+                LOGGER.error("Timeout while waiting on the crate subscription response");
+                e.printStackTrace();
                 // Pass the response back to the application.
                 future.completeExceptionally(t);
             };
 
             /* Functional Consumer example using inner class */
-            BiConsumer<OpcuaAPU, Throwable> error = (message, t) -> {
-
+            BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
+                LOGGER.error("Error while creating the subscription");
+                e.printStackTrace();
                 // Pass the response back to the application.
-                future.completeExceptionally(t);
+                future.completeExceptionally(e);
             };
 
             channel.submit(context, timeout, error, consumer, buffer);
-
         } catch (ParseException e) {
-            LOGGER.error("Unable to serialise the ReadRequest");
+            LOGGER.error("Error while creating the subscription");
+            e.printStackTrace();
+            future.completeExceptionally(e);
         }
-
         return future;
     }
 
@@ -926,18 +918,10 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
     @Override
     public CompletableFuture<PlcUnsubscriptionResponse> unsubscribe(PlcUnsubscriptionRequest unsubscriptionRequest) {
         unsubscriptionRequest.getSubscriptionHandles().forEach(o -> {
-            OpcuaSubscriptionHandle opcSubHandle = (OpcuaSubscriptionHandle) o;
-            /*
-            try {
-                client.getSubscriptionManager().deleteSubscription(opcSubHandle.getClientHandle()).get();
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                LOGGER.warn("Unable to unsubscribe Elements because of: {}", e.getMessage());
-            } catch (ExecutionException e) {
-                LOGGER.warn("Unable to unsubscribe Elements because of: {}", e.getMessage());
-            }*/
+            OpcuaSubscriptionHandle opcuaSubHandle = (OpcuaSubscriptionHandle) o;
+            opcuaSubHandle.stopSubscriber();
+            subscriptions.remove(opcuaSubHandle);
         });
-
         return null;
     }
 
@@ -950,7 +934,6 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
             final PlcConsumerRegistration consumerRegistration = subscriptionHandle.register(consumer);
             registrations.add(consumerRegistration);
         }
-
         return new DefaultPlcConsumerRegistration((PlcSubscriber) this, consumer, handles.toArray(new PlcSubscriptionHandle[0]));
     }
 
@@ -959,31 +942,10 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
         registration.unregister();
     }
 
-
-
-
-
-
-
-    /**
-     * Gets the Conversation Context.
-     *
-     * @return int representing the token identifier
-     */
-    public ConversationContext<OpcuaAPU> getConversationContext() {
-        return this.context;
-    }
-
-
-
-
-
     public static long getDateTime(long dateTime) {
         return (dateTime - EPOCH_OFFSET) / 10000;
     }
 
-
-
     private GuidValue toGuidValue(String identifier) {
         LOGGER.error("Querying Guid nodes is not supported");
         byte[] data4 = new byte[] {0,0};
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
index 9ec5b09..0a1ed28 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandle.java
@@ -20,10 +20,7 @@ package org.apache.plc4x.java.opcua.protocol;
 
 import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
 import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionResponse;
 import org.apache.plc4x.java.api.model.PlcConsumerRegistration;
-import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
-import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.api.value.PlcValue;
 import org.apache.plc4x.java.opcua.context.SecureChannel;
 import org.apache.plc4x.java.opcua.field.OpcuaField;
@@ -39,39 +36,30 @@ import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
 import org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration;
 import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField;
 import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle;
-import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
-import org.apache.plc4x.protocol.opcua.OpcuaProtocol;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import java.time.Duration;
 import java.time.Instant;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 
-/**
- */
 public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(OpcuaSubscriptionHandle.class);
 
     private Set<Consumer<PlcSubscriptionEvent>> consumers;
     private List<String> fieldNames;
-    private long clientHandle;
     private SecureChannel channel;
     private PlcSubscriptionRequest subscriptionRequest;
-
     private AtomicBoolean destroy = new AtomicBoolean(false);
     private OpcuaProtocolLogic plcSubscriber;
     private Long subscriptionId;
     private long cycleTime;
+    private long revisedCycleTime;
 
     private final AtomicLong clientHandles = new AtomicLong(1L);
 
@@ -86,18 +74,15 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
         this.subscriptionId = subscriptionId;
         this.plcSubscriber = plcSubscriber;
         this.cycleTime = cycleTime;
+        this.revisedCycleTime = cycleTime;
         this.context = context;
         try {
             onSubscribeCreateMonitoredItemsRequest().get();
-        } catch (InterruptedException e) {
-            LOGGER.info("Ah ha");
-        } catch (ExecutionException e) {
-            LOGGER.info("Ah ha");
         } catch (Exception e) {
-            LOGGER.info("What?");
+            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message");
             e.printStackTrace();
+            plcSubscriber.onDisconnect(context);
         }
-
         startSubscriber();
     }
 
@@ -178,7 +163,6 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
             WriteBuffer buffer = new WriteBuffer(extObject.getLengthInBytes(), true);
             ExtensionObjectIO.staticSerialize(buffer, extObject);
 
-            /* Functional Consumer example using inner class */
             Consumer<OpcuaMessageResponse> consumer = opcuaResponse -> {
                 CreateMonitoredItemsResponse responseMessage = null;
                 try {
@@ -186,51 +170,48 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                 } catch (ParseException e) {
                     e.printStackTrace();
                 }
-
-                // Pass the response back to the application.
                 future.complete(responseMessage);
 
             };
 
-            /* Functional Consumer example using inner class */
-            Consumer<TimeoutException> timeout = t -> {
-                LOGGER.info("Error While Sending Message");
-                t.printStackTrace();
-                // Pass the response back to the application.
-                future.completeExceptionally(t);
+            Consumer<TimeoutException> timeout = e -> {
+                LOGGER.info("Timeout while sending the Create Monitored Item Subscription Message");
+                e.printStackTrace();
+                plcSubscriber.onDisconnect(context);
             };
 
-            /* Functional Consumer example using inner class */
-            BiConsumer<OpcuaAPU, Throwable> error = (message, t) -> {
-                LOGGER.info("Error While Sending Message");
-                t.printStackTrace();
-                // Pass the response back to the application.
-                future.completeExceptionally(t);
+            BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
+                LOGGER.info("Error while sending the Create Monitored Item Subscription Message");
+                e.printStackTrace();
+                plcSubscriber.onDisconnect(context);
             };
-            LOGGER.info("Not Sure");
+
             channel.submit(context, timeout, error, consumer, buffer);
 
         } catch (ParseException e) {
-            LOGGER.error("Unable to serialise the ReadRequest");
+            LOGGER.info("Unable to serialize the Create Monitored Item Subscription Message");
+            e.printStackTrace();
+            plcSubscriber.onDisconnect(context);
         }
-
         return future;
     }
 
     private void sleep() {
         try {
-            Thread.sleep(this.cycleTime);
+            Thread.sleep(this.revisedCycleTime);
         } catch (InterruptedException e) {
             LOGGER.trace("Interrupted Exception");
         }
     }
 
     /**
-     *
+     * Main subscriber loop. For subscription we still need to send a request the server on every cycle.
+     * Which includes a request for an update of the previsouly agreed upon list of tags.
+     * The server will respond at most once every cycle.
      * @return
      */
     public void startSubscriber() {
-        LOGGER.info("Starting Subscription");
+        LOGGER.trace("Starting Subscription");
         CompletableFuture.supplyAsync(() -> {
             try {
                 LinkedList<SubscriptionAcknowledgement> outstandingAcknowledgements = new LinkedList<>();
@@ -238,6 +219,19 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                 while (!this.destroy.get()) {
                     long requestHandle = channel.getRequestHandle();
 
+                    /* Adjust the cycle time in the case we have more than a couple of requests outstanding, which indicates the server can't keep up */
+                    if (outstandingRequests.size() > 2) {
+                        revisedCycleTime += (long) cycleTime * 0.1f;
+                        LOGGER.warn("Revising the cycle time due to server congestion, {} ms", revisedCycleTime);
+                    } else {
+                        if (revisedCycleTime != cycleTime) {
+                            revisedCycleTime -= (long) cycleTime * 0.1f;
+                        }
+                        if (revisedCycleTime <= cycleTime) {
+                            revisedCycleTime = cycleTime;
+                        }
+                    }
+
                     RequestHeader requestHeader = new RequestHeader(channel.getAuthenticationToken(),
                         SecureChannel.getCurrentDateTime(),
                         requestHandle,
@@ -274,12 +268,23 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                         WriteBuffer buffer = new WriteBuffer(extObject.getLengthInBytes(), true);
                         ExtensionObjectIO.staticSerialize(buffer, extObject);
 
+                        /*  Create Consumer for the response message, error and timeout to be sent to the Secure Channel */
                         Consumer<OpcuaMessageResponse> consumer = opcuaResponse -> {
                             PublishResponse responseMessage = null;
                             try {
-                                responseMessage = (PublishResponse) ExtensionObjectIO.staticParse(new ReadBuffer(opcuaResponse.getMessage(), true), false).getBody();
+                                ExtensionObjectDefinition unknownExtensionObject = ExtensionObjectIO.staticParse(new ReadBuffer(opcuaResponse.getMessage(), true), false).getBody();
+                                if (unknownExtensionObject instanceof PublishResponse) {
+                                    responseMessage = (PublishResponse) unknownExtensionObject;
+                                } else {
+                                    ServiceFault serviceFault = (ServiceFault) unknownExtensionObject;
+                                    ResponseHeader header = (ResponseHeader) serviceFault.getResponseHeader();
+                                    LOGGER.error("Subscription ServiceFault return from server with error code,  '{}'", header.getServiceResult().toString());
+                                    plcSubscriber.onDisconnect(context);
+                                }
                             } catch (ParseException e) {
+                                LOGGER.error("Unable to parse the returned Subscription response");
                                 e.printStackTrace();
+                                plcSubscriber.onDisconnect(context);
                             }
                             outstandingRequests.remove(((ResponseHeader) responseMessage.getResponseHeader()).getRequestHandle());
 
@@ -290,36 +295,36 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
                             for (ExtensionObject notificationMessage : ((NotificationMessage) responseMessage.getNotificationMessage()).getNotificationData()) {
                                 ExtensionObjectDefinition notification = notificationMessage.getBody();
                                 if (notification instanceof DataChangeNotification) {
-                                    LOGGER.info("Found a Data Change notification");
+                                    LOGGER.trace("Found a Data Change notification");
                                     ExtensionObjectDefinition[] items = ((DataChangeNotification) notification).getMonitoredItems();
                                     onSubscriptionValue(Arrays.stream(items).toArray(MonitoredItemNotification[]::new));
-
                                 } else {
                                     LOGGER.warn("Unsupported Notification type");
                                 }
                             }
                         };
 
-                        /* Functional Consumer example using inner class */
-                        Consumer<TimeoutException> timeout = t -> {
-
+                        Consumer<TimeoutException> timeout = e -> {
+                            LOGGER.error("Timeout while waiting for subscription response");
+                            e.printStackTrace();
+                            plcSubscriber.onDisconnect(context);
                         };
 
-                        /* Functional Consumer example using inner class */
-                        BiConsumer<OpcuaAPU, Throwable> error = (message, t) -> {
-
+                        BiConsumer<OpcuaAPU, Throwable> error = (message, e) -> {
+                            LOGGER.error("Error while waiting for subscription response");
+                            e.printStackTrace();
+                            plcSubscriber.onDisconnect(context);
                         };
 
                         outstandingRequests.add(requestHandle);
                         channel.submit(context, timeout, error, consumer, buffer);
+                        /* Put the subscriber loop to sleep for the rest of the cycle. */
                         sleep();
                     } catch (ParseException e) {
                         LOGGER.warn("Unable to serialize subscription request");
                         e.printStackTrace();
                     }
                 }
-
-
             } catch (Exception e) {
                 LOGGER.error("Failed :(");
                 e.printStackTrace();
@@ -331,7 +336,7 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
 
 
     /**
-     *
+     * Stop the subscriber either on disconnect or on error
      * @return
      */
     public void stopSubscriber() {
@@ -339,40 +344,43 @@ public class OpcuaSubscriptionHandle extends DefaultPlcSubscriptionHandle {
     }
 
     /**
-     * @param values
+     * Receive the returned values from the OPCUA server and format it so that it can be received by the PLC4X client.
+     * @param values - array of data values to be sent to the client.
      */
-    public void onSubscriptionValue(MonitoredItemNotification[] values) {
-        LOGGER.info("Consumer Length {}", consumers.size());
+    private void onSubscriptionValue(MonitoredItemNotification[] values) {
+        LinkedHashSet<String> fieldList = new LinkedHashSet<>();
+        DataValue[] dataValues = new DataValue[values.length];
+        int i = 0;
+        for (MonitoredItemNotification value : values) {
+            fieldList.add(fieldNames.get((int) value.getClientHandle() - 1));
+            dataValues[i] = value.getValue();
+            i++;
+        }
+        Map<String, ResponseItem<PlcValue>> fields = plcSubscriber.readResponse(fieldList, dataValues);
+        final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), fields);
+
         consumers.forEach(plcSubscriptionEventConsumer -> {
-            PlcResponseCode resultCode = PlcResponseCode.OK;
-            PlcValue stringItem = null;
-            try {
-                LinkedHashSet<String> fieldList = new LinkedHashSet<>();
-                DataValue[] dataValues = new DataValue[values.length];
-                int i = 0;
-                for (MonitoredItemNotification value : values) {
-
-                    fieldList.add(fieldNames.get((int) value.getClientHandle() - 1));
-                    dataValues[i] = value.getValue();
-                    i++;
-                }
-                LOGGER.info("Variant Type - {} ", dataValues[0].getValue().getVariantType());
-                Map<String, ResponseItem<PlcValue>> fields = plcSubscriber.readResponse(fieldList, dataValues);
-                PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), fields);
-                plcSubscriptionEventConsumer.accept(event);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+            plcSubscriptionEventConsumer.accept(event);
         });
     }
 
+    /**
+     * Registers a new Consumer, this allows multiple PLC4X consumers to use the same subscription.
+     * @param consumer - Consumer to be used to send any returned values.
+     * @return PlcConsumerRegistration - return the important information back to the client.
+     */
     @Override
     public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer) {
-        LOGGER.info("Registering within Handle class");
+        LOGGER.info("Registering a new OPCUA subscription consumer");
         consumers.add(consumer);
         return new DefaultPlcConsumerRegistration(plcSubscriber, consumer, this);
     }
 
+    /**
+     * Given an PLC4X OpcuaField generate the OPC UA Node Id
+     * @param field - The PLC4X OpcuaField, this is the field generated from the OpcuaField class from the parsed field string.
+     * @return NodeId - Returns an OPC UA Node Id which can be sent over the wire.
+     */
     private NodeId generateNodeId(OpcuaField field) {
         NodeId nodeId = null;
         if (field.getIdentifierType() == OpcuaIdentifierType.BINARY_IDENTIFIER) {