You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2022/12/31 16:36:37 UTC

[plc4x] branch develop updated: feat(plc4j/connection-cache): Added support for subscriptions to the plc connection cache.

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

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 1f81060fec feat(plc4j/connection-cache): Added support for subscriptions to the plc connection cache.
1f81060fec is described below

commit 1f81060feccbe16df1333c933078322e8e1152cd
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sat Dec 31 17:36:30 2022 +0100

    feat(plc4j/connection-cache): Added support for subscriptions to the plc connection cache.
---
 .../utils/connectionpool2/CachedPlcConnection.java | 10 ++-
 .../connectionpool2/CachedSubscriptionRequest.java | 71 ++++++++++++++++++++
 .../CachedSubscriptionRequestBuilder.java          | 78 ++++++++++++++++++++++
 .../CachedUnsubscriptionRequest.java               | 46 +++++++++++++
 .../CachedUnsubscriptionRequestBuilder.java        | 59 ++++++++++++++++
 5 files changed, 262 insertions(+), 2 deletions(-)

diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnection.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnection.java
index 7c2b9005d4..4cf912d3a9 100644
--- a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnection.java
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnection.java
@@ -337,12 +337,18 @@ public class CachedPlcConnection implements PlcConnection, PlcConnectionMetadata
 
     @Override
     public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
-        throw new UnsupportedOperationException();
+        if (closed) {
+            throw new IllegalStateException("Trying to build a Request on a closed Connection!");
+        }
+        return new CachedSubscriptionRequestBuilder(this, this.getActiveConnection().subscriptionRequestBuilder());
     }
 
     @Override
     public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
-        throw new UnsupportedOperationException();
+        if (closed) {
+            throw new IllegalStateException("Trying to build a Request on a closed Connection!");
+        }
+        return new CachedUnsubscriptionRequestBuilder(this, this.getActiveConnection().unsubscriptionRequestBuilder());
     }
 
     @Override
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequest.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequest.java
new file mode 100644
index 0000000000..1c8ba5e797
--- /dev/null
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.utils.connectionpool2;
+
+import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.model.PlcSubscriptionTag;
+
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+public class CachedSubscriptionRequest implements PlcSubscriptionRequest {
+
+    private final CachedPlcConnection parent;
+    private final PlcSubscriptionRequest innerRequest;
+
+    public CachedSubscriptionRequest(CachedPlcConnection parent, PlcSubscriptionRequest innerRequest) {
+        this.parent = parent;
+        this.innerRequest = innerRequest;
+    }
+
+    @Override
+    public CompletableFuture<? extends PlcSubscriptionResponse> execute() {
+        // Only allowed if connection is still active
+        return innerRequest.execute();
+    }
+
+    @Override
+    public int getNumberOfTags() {
+        return innerRequest.getNumberOfTags();
+    }
+
+    @Override
+    public LinkedHashSet<String> getTagNames() {
+        return innerRequest.getTagNames();
+    }
+
+    @Override
+    public PlcSubscriptionTag getTag(String name) {
+        return innerRequest.getTag(name);
+    }
+
+    @Override
+    public List<PlcSubscriptionTag> getTags() {
+        return innerRequest.getTags();
+    }
+
+    @Override
+    public Map<String, List<Consumer<PlcSubscriptionEvent>>> getPreRegisteredConsumers() {
+        return innerRequest.getPreRegisteredConsumers();
+    }
+
+}
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequestBuilder.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequestBuilder.java
new file mode 100644
index 0000000000..759b882d69
--- /dev/null
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedSubscriptionRequestBuilder.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.utils.connectionpool2;
+
+import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
+import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
+import org.apache.plc4x.java.api.model.PlcTag;
+
+import java.time.Duration;
+import java.util.function.Consumer;
+
+public class CachedSubscriptionRequestBuilder implements PlcSubscriptionRequest.Builder {
+
+    private final CachedPlcConnection parent;
+    private final PlcSubscriptionRequest.Builder builder;
+
+    public CachedSubscriptionRequestBuilder(CachedPlcConnection parent, PlcSubscriptionRequest.Builder builder) {
+        this.parent = parent;
+        this.builder = builder;
+    }
+
+    @Override
+    public PlcSubscriptionRequest build() {
+        return builder.build();
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addCyclicTagAddress(String name, String tagAddress, Duration pollingInterval) {
+        return builder.addCyclicTagAddress(name, tagAddress, pollingInterval);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addCyclicTag(String name, PlcTag tag, Duration pollingInterval) {
+        return builder.addCyclicTag(name, tag, pollingInterval);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addChangeOfStateTagAddress(String name, String tagAddress) {
+        return builder.addChangeOfStateTagAddress(name, tagAddress);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addChangeOfStateTag(String name, PlcTag tag) {
+        return builder.addChangeOfStateTag(name, tag);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addEventTagAddress(String name, String tagAddress) {
+        return builder.addEventTagAddress(name, tagAddress);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addEventTag(String name, PlcTag tag) {
+        return builder.addEventTag(name, tag);
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder addPreRegisteredConsumer(String name, Consumer<PlcSubscriptionEvent> preRegisteredConsumer) {
+        return builder.addPreRegisteredConsumer(name, preRegisteredConsumer);
+    }
+
+}
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequest.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequest.java
new file mode 100644
index 0000000000..2345b5f713
--- /dev/null
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.utils.connectionpool2;
+
+import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+public class CachedUnsubscriptionRequest implements PlcUnsubscriptionRequest {
+
+    private final CachedPlcConnection parent;
+    private final PlcUnsubscriptionRequest innerRequest;
+
+    public CachedUnsubscriptionRequest(CachedPlcConnection parent, PlcUnsubscriptionRequest innerRequest) {
+        this.parent = parent;
+        this.innerRequest = innerRequest;
+    }
+
+    @Override
+    public CompletableFuture<PlcUnsubscriptionResponse> execute() {
+        return innerRequest.execute();
+    }
+    @Override
+    public List<PlcSubscriptionHandle> getSubscriptionHandles() {
+        return innerRequest.getSubscriptionHandles();
+    }
+
+}
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequestBuilder.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequestBuilder.java
new file mode 100644
index 0000000000..313514add9
--- /dev/null
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/connectionpool2/CachedUnsubscriptionRequestBuilder.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.utils.connectionpool2;
+
+import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
+import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
+import org.apache.plc4x.java.api.model.PlcTag;
+
+import java.time.Duration;
+import java.util.Collection;
+import java.util.function.Consumer;
+
+public class CachedUnsubscriptionRequestBuilder implements PlcUnsubscriptionRequest.Builder {
+
+    private final CachedPlcConnection parent;
+    private final PlcUnsubscriptionRequest.Builder builder;
+
+    public CachedUnsubscriptionRequestBuilder(CachedPlcConnection parent, PlcUnsubscriptionRequest.Builder builder) {
+        this.parent = parent;
+        this.builder = builder;
+    }
+
+    @Override
+    public PlcUnsubscriptionRequest build() {
+        return builder.build();
+    }
+
+    @Override
+    public PlcUnsubscriptionRequest.Builder addHandles(PlcSubscriptionHandle plcSubscriptionHandle) {
+        return builder.addHandles(plcSubscriptionHandle);
+    }
+
+    @Override
+    public PlcUnsubscriptionRequest.Builder addHandles(PlcSubscriptionHandle plcSubscriptionHandle, PlcSubscriptionHandle... plcSubscriptionHandles) {
+        return builder.addHandles(plcSubscriptionHandle, plcSubscriptionHandles);
+    }
+
+    @Override
+    public PlcUnsubscriptionRequest.Builder addHandles(Collection<PlcSubscriptionHandle> plcSubscriptionHandles) {
+        return builder.addHandles(plcSubscriptionHandles);
+    }
+
+}