Skip to content

Commit

Permalink
Added possibility to supply optional port.
Browse files Browse the repository at this point in the history
Can be used for Unit-Tests and tunneling.
  • Loading branch information
sruehl committed Feb 2, 2018
1 parent 4ff6028 commit b1824a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
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;
Expand All @@ -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() {
Expand All @@ -61,13 +62,15 @@ public PlcConnection connect(String url) throws PlcConnectionException {
"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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one

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;
Expand All @@ -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;
Expand All @@ -61,8 +64,18 @@ public ADSPlcConnection(String hostName, AMSNetId targetAmsNetId, AMSPort target
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;
Expand Down Expand Up @@ -110,7 +123,7 @@ protected void initChannel(Channel channel) throws Exception {
}
});
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one
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);
Expand All @@ -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");
Expand Down

0 comments on commit b1824a5

Please sign in to comment.