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

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


The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol 
by this push:
     new b1824a5  Added possibility to supply optional port. Can be used for 
Unit-Tests and tunneling.
b1824a5 is described below

commit b1824a53fd0eb52f4678c53188ef07e59fa4701e
Author: Sebastian Rühl <sru...@apache.org>
AuthorDate: Fri Feb 2 14:16:32 2018 +0100

    Added possibility to supply optional port.
    Can be used for Unit-Tests and tunneling.
---
 .../main/java/org/apache/plc4x/java/ads/ADSPlcDriver.java | 11 +++++++----
 .../plc4x/java/ads/connection/ADSPlcConnection.java       | 15 ++++++++++++++-
 .../java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java  |  4 ++--
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/ADSPlcDriver.java 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/ADSPlcDriver.java
index 2b9b3b6..0210f99 100644
--- 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/ADSPlcDriver.java
+++ 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/ADSPlcDriver.java
@@ -18,6 +18,7 @@ under the License.
 */
 package org.apache.plc4x.java.ads;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.plc4x.java.ads.api.generic.types.AMSNetId;
 import org.apache.plc4x.java.ads.api.generic.types.AMSPort;
 import org.apache.plc4x.java.ads.connection.ADSPlcConnection;
@@ -41,7 +42,7 @@ public class ADSPlcDriver implements PlcDriver {
             + "(/"
             + "(?<sourceAmsNetId>" + AMSNetId.AMS_NET_ID_PATTERN + 
"):(?<sourceAmsPort>" + AMSPort.AMS_PORT_PATTERN + ")"
             + ")?");
-    private static final Pattern ADS_URI_PATTERN = 
Pattern.compile("^ads://(?<host>\\w+)/" + ADS_ADDRESS_PATTERN);
+    private static final Pattern ADS_URI_PATTERN = 
Pattern.compile("^ads://(?<host>\\w+)(:(?<port>\\d*))?/" + ADS_ADDRESS_PATTERN);
 
     @Override
     public String getProtocolCode() {
@@ -61,13 +62,15 @@ public class ADSPlcDriver implements PlcDriver {
                 "Connection url " + url + " doesn't match 
'ads://{host|ip}/{targetAmsNetId}:{targetAmsPort}/{sourceAmsNetId}:{sourceAmsPort}'
 RAW:" + ADS_URI_PATTERN);
         }
         String host = matcher.group("host");
+        String portString = matcher.group("port");
+        Integer port = StringUtils.isNotBlank(portString) ? 
Integer.parseInt(portString) : null;
         AMSNetId targetAmsNetId = AMSNetId.of(matcher.group("targetAmsNetId"));
         AMSPort targetAmsPort = AMSPort.of(matcher.group("targetAmsPort"));
         String sourceAmsNetIdString = matcher.group("sourceAmsNetId");
-        AMSNetId sourceAmsNetId = sourceAmsNetIdString != null ? 
AMSNetId.of(sourceAmsNetIdString) : null;
+        AMSNetId sourceAmsNetId = StringUtils.isNotBlank(sourceAmsNetIdString) 
? AMSNetId.of(sourceAmsNetIdString) : null;
         String sourceAmsPortString = matcher.group("sourceAmsPort");
-        AMSPort sourceAmsPort = sourceAmsPortString != null ? 
AMSPort.of(sourceAmsPortString) : null;
-        return new ADSPlcConnection(host, targetAmsNetId, targetAmsPort, 
sourceAmsNetId, sourceAmsPort);
+        AMSPort sourceAmsPort = StringUtils.isNotBlank(sourceAmsPortString) ? 
AMSPort.of(sourceAmsPortString) : null;
+        return new ADSPlcConnection(host, port, targetAmsNetId, targetAmsPort, 
sourceAmsNetId, sourceAmsPort);
     }
 
     @Override
diff --git 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java
 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java
index 70a78a4..82be0de 100644
--- 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java
+++ 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java
@@ -36,6 +36,7 @@ import org.apache.plc4x.java.api.model.Address;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -46,6 +47,8 @@ public class ADSPlcConnection extends AbstractPlcConnection 
implements PlcReader
 
     private final String hostName;
 
+    private final Optional<Integer> suppliedPort;
+
     private final AMSNetId targetAmsNetId;
 
     private final AMSPort targetAmsPort;
@@ -61,8 +64,18 @@ public class ADSPlcConnection extends AbstractPlcConnection 
implements PlcReader
         this(hostName, targetAmsNetId, targetAmsPort, null, null);
     }
 
+    public ADSPlcConnection(String hostName, Integer port, AMSNetId 
targetAmsNetId, AMSPort targetAmsPort) {
+        this(hostName, port, targetAmsNetId, targetAmsPort, null, null);
+    }
+
+
     public ADSPlcConnection(String hostName, AMSNetId targetAmsNetId, AMSPort 
targetAmsPort, AMSNetId sourceAmsNetId, AMSPort sourceAmsPort) {
+        this(hostName, null, targetAmsNetId, targetAmsPort, sourceAmsNetId, 
sourceAmsPort);
+    }
+
+    public ADSPlcConnection(String hostName, Integer port, AMSNetId 
targetAmsNetId, AMSPort targetAmsPort, AMSNetId sourceAmsNetId, AMSPort 
sourceAmsPort) {
         this.hostName = hostName;
+        this.suppliedPort = Optional.ofNullable(port);
         this.targetAmsNetId = targetAmsNetId;
         this.targetAmsPort = targetAmsPort;
         this.sourceAmsNetId = sourceAmsNetId;
@@ -110,7 +123,7 @@ public class ADSPlcConnection extends AbstractPlcConnection 
implements PlcReader
                 }
             });
             // Start the client.
-            ChannelFuture f = bootstrap.connect(serverInetAddress, 
TCP_PORT).sync();
+            ChannelFuture f = bootstrap.connect(serverInetAddress, 
suppliedPort.orElse(TCP_PORT)).sync();
             f.awaitUninterruptibly();
             // Wait till the session is finished initializing.
             channel = f.channel();
diff --git 
a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
 
b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
index 021de10..ae19173 100644
--- 
a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
+++ 
b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
@@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ExtendWithTcpHexDumper(port = ADSPlcConnection.TCP_PORT, shutdownTimeout = 3)
+@ExtendWithTcpHexDumper(shutdownTimeout = 3)
 public class ADSPlcDriverTest {
 
     private static final Logger logger = 
LoggerFactory.getLogger(ADSPlcDriverTest.class);
@@ -45,7 +45,7 @@ public class ADSPlcDriverTest {
     @Tag("fast")
     void getConnection() throws Exception {
         ADSPlcConnection adsConnection = (ADSPlcConnection)
-            new 
PlcDriverManager().getConnection("ads://localhost/0.0.0.0.0.0:13");
+            new PlcDriverManager().getConnection("ads://localhost:" + usedPort 
+ "/0.0.0.0.0.0:13");
         Assertions.assertEquals(adsConnection.getHostName(), "localhost");
         Assertions.assertEquals(adsConnection.getTargetAmsNetId().toString(), 
"0.0.0.0.0.0");
         Assertions.assertEquals(adsConnection.getTargetAmsPort().toString(), 
"13");

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

Reply via email to