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 2020/07/07 20:25:50 UTC
[plc4x] 02/02: - Initial commit for some discovery functionality
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
commit da134f1e4869033dd3d7a7aecc41be2c6e2221b4
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Jul 7 22:21:33 2020 +0200
- Initial commit for some discovery functionality
---
.../plc4x/java/spi/discovery/ActiveDiscovery.java | 25 ++++
.../java/spi/discovery/BroadcastDiscovery.java | 25 ++++
.../plc4x/java/spi/discovery/PassiveDiscovery.java | 25 ++++
.../java/spi/discovery/SupportsDiscovery.java | 25 ++++
sandbox/discovery/pom.xml | 53 +++++++
.../plc4x/java/discovery/DiscoveryConnection.java | 166 +++++++++++++++++++++
.../plc4x/java/discovery/DiscoveryDriver.java | 52 +++++++
.../java/discovery/DiscoveryFieldHandler.java | 33 ++++
.../services/org.apache.plc4x.java.api.PlcDriver | 20 +++
sandbox/pom.xml | 1 +
10 files changed, 425 insertions(+)
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/ActiveDiscovery.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/ActiveDiscovery.java
new file mode 100644
index 0000000..7b08163
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/ActiveDiscovery.java
@@ -0,0 +1,25 @@
+/*
+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
+
+ http://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.spi.discovery;
+
+/**
+ * Base interface for all drivers that support detecting a device by actively trying to connect to it.
+ */
+public interface ActiveDiscovery extends SupportsDiscovery {
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/BroadcastDiscovery.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/BroadcastDiscovery.java
new file mode 100644
index 0000000..49bcca0
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/BroadcastDiscovery.java
@@ -0,0 +1,25 @@
+/*
+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
+
+ http://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.spi.discovery;
+
+/**
+ * Base interface for all drivers that support detecting a device by sending broadcast packets.
+ */
+public interface BroadcastDiscovery extends SupportsDiscovery {
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/PassiveDiscovery.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/PassiveDiscovery.java
new file mode 100644
index 0000000..71f1f30
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/PassiveDiscovery.java
@@ -0,0 +1,25 @@
+/*
+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
+
+ http://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.spi.discovery;
+
+/**
+ * Base interface for all drivers that support detecting a device passively listening to network traffic.
+ */
+public interface PassiveDiscovery extends SupportsDiscovery {
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/SupportsDiscovery.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/SupportsDiscovery.java
new file mode 100644
index 0000000..1d8395c
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/discovery/SupportsDiscovery.java
@@ -0,0 +1,25 @@
+/*
+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
+
+ http://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.spi.discovery;
+
+/**
+ * Base interface for all Drivers that generally support some form of discovery mechanism.
+ */
+public interface SupportsDiscovery {
+}
diff --git a/sandbox/discovery/pom.xml b/sandbox/discovery/pom.xml
new file mode 100644
index 0000000..c65a5b4
--- /dev/null
+++ b/sandbox/discovery/pom.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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
+
+ http://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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.plc4x.sandbox</groupId>
+ <artifactId>plc4x-sandbox</artifactId>
+ <version>0.8.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>plc4j-driver-discovery</artifactId>
+
+ <name>Sandbox: PLC4J: Driver: Discovery</name>
+ <description>Implementation of a PLC4X driver which provides discovery services. It's not actually a real diver, but more utilizes the drivers discovery mechanisms in a way that it integrates into the PLC4X API nicely.</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-spi</artifactId>
+ <version>0.8.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryConnection.java b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryConnection.java
new file mode 100644
index 0000000..405ea1f
--- /dev/null
+++ b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryConnection.java
@@ -0,0 +1,166 @@
+/*
+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
+
+ http://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.discovery;
+
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.PlcDriver;
+import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
+import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+import org.apache.plc4x.java.api.model.PlcConsumerRegistration;
+import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
+import org.apache.plc4x.java.spi.discovery.ActiveDiscovery;
+import org.apache.plc4x.java.spi.discovery.BroadcastDiscovery;
+import org.apache.plc4x.java.spi.discovery.SupportsDiscovery;
+import org.apache.plc4x.java.spi.discovery.PassiveDiscovery;
+import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest;
+import org.apache.plc4x.java.spi.messages.DefaultPlcUnsubscriptionRequest;
+import org.apache.plc4x.java.spi.messages.PlcSubscriber;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+public class DiscoveryConnection implements PlcConnection, PlcSubscriber {
+
+ private boolean connected = false;
+ private Map<String, ActiveDiscovery> activeDiscovery;
+ private Map<String, PassiveDiscovery> passiveDiscovery;
+ private Map<String, BroadcastDiscovery> broadcastDiscovery;
+
+ @Override
+ public void connect() {
+ if(connected) {
+ return;
+ }
+
+ activeDiscovery = new HashMap<>();
+ passiveDiscovery = new HashMap<>();
+ broadcastDiscovery = new HashMap<>();
+
+ // Access all PlcDriver implementations in this VM.
+ ServiceLoader<PlcDriver> plcDriverLoader = ServiceLoader.load(
+ PlcDriver.class, Thread.currentThread().getContextClassLoader());
+
+ // Iterate over all of them and check which types of discovery they support.
+ for (PlcDriver driver : plcDriverLoader) {
+ // Check if this driver generally supports discovery at all.
+ if(driver instanceof SupportsDiscovery) {
+ if(driver instanceof ActiveDiscovery) {
+ activeDiscovery.put(driver.getProtocolCode(), (ActiveDiscovery) driver);
+ }
+ if(driver instanceof PassiveDiscovery) {
+ passiveDiscovery.put(driver.getProtocolCode(), (PassiveDiscovery) driver);
+ }
+ if(driver instanceof BroadcastDiscovery) {
+ broadcastDiscovery.put(driver.getProtocolCode(), (BroadcastDiscovery) driver);
+ }
+ }
+ }
+
+ connected = true;
+ }
+
+ @Override
+ public boolean isConnected() {
+ return connected;
+ }
+
+ @Override
+ public void close() {
+ connected = false;
+ activeDiscovery = null;
+ passiveDiscovery = null;
+ broadcastDiscovery = null;
+ }
+
+ @Override
+ public PlcConnectionMetadata getMetadata() {
+ return new PlcConnectionMetadata() {
+ @Override
+ public boolean canRead() {
+ return false;
+ }
+
+ @Override
+ public boolean canWrite() {
+ return false;
+ }
+
+ @Override
+ public boolean canSubscribe() {
+ return true;
+ }
+ };
+ }
+
+ @Override
+ public CompletableFuture<Void> ping() {
+ CompletableFuture<Void> future = new CompletableFuture<>();
+ future.completeExceptionally(new PlcUnsupportedOperationException("The connection does not support pinging"));
+ return future;
+ }
+
+ @Override
+ public PlcReadRequest.Builder readRequestBuilder() {
+ throw new PlcUnsupportedOperationException("The connection does not support reading");
+ }
+
+ @Override
+ public PlcWriteRequest.Builder writeRequestBuilder() {
+ throw new PlcUnsupportedOperationException("The connection does not support writing");
+ }
+
+ @Override
+ public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
+ return new DefaultPlcSubscriptionRequest.Builder(this, new DiscoveryFieldHandler());
+ }
+
+ @Override
+ public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
+ return new DefaultPlcUnsubscriptionRequest.Builder(this);
+ }
+
+ @Override
+ public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
+ // TODO: Implement ...
+ return null;
+ }
+
+ @Override
+ public CompletableFuture<PlcUnsubscriptionResponse> unsubscribe(PlcUnsubscriptionRequest unsubscriptionRequest) {
+ // TODO: Implement ...
+ return null;
+ }
+
+ @Override
+ public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
+ // TODO: Implement ...
+ return null;
+ }
+
+ @Override
+ public void unregister(PlcConsumerRegistration registration) {
+ // TODO: Implement ...
+ }
+
+}
diff --git a/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryDriver.java b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryDriver.java
new file mode 100644
index 0000000..a831a10
--- /dev/null
+++ b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryDriver.java
@@ -0,0 +1,52 @@
+/*
+ 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
+
+ http://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.discovery;
+
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.PlcDriver;
+import org.apache.plc4x.java.api.authentication.PlcAuthentication;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+
+/**
+ * Implementation of a virtual driver which wraps discovery operations in
+ * PLC4X API calls.
+ */
+public class DiscoveryDriver implements PlcDriver {
+
+ @Override
+ public String getProtocolCode() {
+ return "discovery";
+ }
+
+ @Override
+ public String getProtocolName() {
+ return "Apache PLC4X Discovery Driver";
+ }
+
+ @Override
+ public PlcConnection getConnection(String url) {
+ return new DiscoveryConnection();
+ }
+
+ @Override
+ public PlcConnection getConnection(String url, PlcAuthentication authentication) throws PlcConnectionException {
+ throw new PlcConnectionException("Authentication not supported.");
+ }
+
+}
diff --git a/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryFieldHandler.java b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryFieldHandler.java
new file mode 100644
index 0000000..93f6245
--- /dev/null
+++ b/sandbox/discovery/src/main/java/org/apache/plc4x/java/discovery/DiscoveryFieldHandler.java
@@ -0,0 +1,33 @@
+/*
+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
+
+ http://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.discovery;
+
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.spi.connection.DefaultPlcFieldHandler;
+
+public class DiscoveryFieldHandler extends DefaultPlcFieldHandler {
+
+ @Override
+ public PlcField createField(String fieldQuery) throws PlcInvalidFieldException {
+ // TODO: Implement ...
+ return null;
+ }
+
+}
diff --git a/sandbox/discovery/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver b/sandbox/discovery/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver
new file mode 100644
index 0000000..189d3d9
--- /dev/null
+++ b/sandbox/discovery/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver
@@ -0,0 +1,20 @@
+#
+# 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
+#
+# http://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.
+#
+
+org.apache.plc4x.java.discovery.DiscoveryDriver
\ No newline at end of file
diff --git a/sandbox/pom.xml b/sandbox/pom.xml
index 98b9092..93c4a4c 100644
--- a/sandbox/pom.xml
+++ b/sandbox/pom.xml
@@ -37,6 +37,7 @@
<modules>
<module>code-gen</module>
+ <module>discovery</module>
<module>plc-simulator</module>
<module>test-java-amsads-driver</module>