You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/02 12:29:38 UTC
[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated:
added a tcp mock hex dumper
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 5be4ac8 added a tcp mock hex dumper
5be4ac8 is described below
commit 5be4ac88d6ec57c08ebc14af718405f48fcadf30
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 2 13:29:33 2018 +0100
added a tcp mock hex dumper
---
plc4j/protocols/ads/pom.xml | 4 +
.../java/ads/connection/ADSPlcConnection.java | 12 +--
.../apache/plc4x/java/ads/ADSPlcDriverTest.java | 11 ++-
.../java/ads/util/ExtendWithTcpHexDumper.java | 36 +++++++
.../apache/plc4x/java/ads/util/TcpHexDumper.java | 103 +++++++++++++++++++++
plc4j/protocols/ads/src/test/resources/logback.xml | 2 +-
6 files changed, 153 insertions(+), 15 deletions(-)
diff --git a/plc4j/protocols/ads/pom.xml b/plc4j/protocols/ads/pom.xml
index 178a166..c277e83 100644
--- a/plc4j/protocols/ads/pom.xml
+++ b/plc4j/protocols/ads/pom.xml
@@ -65,6 +65,10 @@
</dependency>
<dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
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 92ae31f..70a78a4 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
@@ -37,13 +37,12 @@ import org.apache.plc4x.java.api.model.Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader, PlcWriter {
- private static final int TCP_PORT = 48898;
+ public static final int TCP_PORT = 48898;
private final String hostName;
@@ -95,11 +94,6 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader
workerGroup = new NioEventLoopGroup();
try {
- // As we don't just want to wait till the connection is established,
- // define a future we can use to signal back that the ads session is
- // finished initializing.
- CompletableFuture<Void> sessionSetupCompleteFuture = new CompletableFuture<>();
-
InetAddress serverInetAddress = InetAddress.getByName(hostName);
Bootstrap bootstrap = new Bootstrap();
@@ -120,15 +114,11 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader
f.awaitUninterruptibly();
// Wait till the session is finished initializing.
channel = f.channel();
-
- sessionSetupCompleteFuture.get();
} catch (UnknownHostException e) {
throw new PlcConnectionException("Unknown Host " + hostName, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PlcConnectionException(e);
- } catch (ExecutionException e) {
- throw new PlcConnectionException(e);
}
}
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 49585d4..d15f55a 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
@@ -21,24 +21,29 @@ package org.apache.plc4x.java.ads;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.ads.connection.ADSPlcConnection;
+import org.apache.plc4x.java.ads.util.ExtendWithTcpHexDumper;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.exceptions.PlcException;
import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+@ExtendWithTcpHexDumper(value = ADSPlcConnection.TCP_PORT, shutdownTimeout = 3)
public class ADSPlcDriverTest {
- @Disabled("We first have to find/build some tool to help test these connections.")
+ private static final Logger logger = LoggerFactory.getLogger(ADSPlcDriverTest.class);
+
@Test
@Tag("fast")
- void getConnection() throws PlcException {
+ void getConnection() throws Exception {
ADSPlcConnection adsConnection = (ADSPlcConnection)
new PlcDriverManager().getConnection("ads://localhost/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");
+ adsConnection.close();
}
/**
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java
new file mode 100644
index 0000000..32666cf
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java
@@ -0,0 +1,36 @@
+/*
+ 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.ads.util;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.*;
+
+@Target({TYPE, METHOD, ANNOTATION_TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@ExtendWith(TcpHexDumper.class)
+public @interface ExtendWithTcpHexDumper {
+ int value();
+
+ int shutdownTimeout() default 10;
+}
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java
new file mode 100644
index 0000000..2f1f420
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java
@@ -0,0 +1,103 @@
+/*
+ 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.ads.util;
+
+import org.apache.commons.io.HexDump;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback {
+
+ private static final Logger logger = LoggerFactory.getLogger(TcpHexDumper.class);
+
+ private ExecutorService pool = Executors.newCachedThreadPool();
+
+ private ServerSocket serverSocket;
+
+ int shutdownTimeout = 10;
+
+ public void init(int port) throws IOException, InterruptedException {
+ if (serverSocket != null) {
+ stop();
+ }
+ serverSocket = new ServerSocket(port);
+ logger.info("Starting pool");
+ pool.submit(() -> {
+ Socket accept;
+ try {
+ logger.info("Waiting for an incoming connection");
+ accept = serverSocket.accept();
+ logger.info("Accepted {} and starting listener", accept);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ pool.submit(() -> {
+ InputStream inputStream;
+ try {
+ inputStream = accept.getInputStream();
+ logger.info("Starting to read now");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ byte[] buffer = new byte[4096];
+ try {
+ while (IOUtils.read(inputStream, buffer) > 0) {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ HexDump.dump(buffer, 0, byteArrayOutputStream, 0);
+ logger.info("Dump:\n{}", byteArrayOutputStream);
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ });
+ logger.info("Started pool");
+ }
+
+ public void stop() throws IOException, InterruptedException {
+ serverSocket.close();
+ pool.awaitTermination(shutdownTimeout, TimeUnit.SECONDS);
+ logger.info("Stopped");
+ }
+
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ stop();
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext context) throws Exception {
+ ExtendWithTcpHexDumper annotation = context.getRequiredTestClass().getAnnotation(ExtendWithTcpHexDumper.class);
+ init(annotation.value());
+ shutdownTimeout = annotation.shutdownTimeout();
+ }
+}
diff --git a/plc4j/protocols/ads/src/test/resources/logback.xml b/plc4j/protocols/ads/src/test/resources/logback.xml
index bba8e02..27d40c0 100644
--- a/plc4j/protocols/ads/src/test/resources/logback.xml
+++ b/plc4j/protocols/ads/src/test/resources/logback.xml
@@ -29,7 +29,7 @@
</encoder>
</appender>
- <root level="warn">
+ <root level="info">
<appender-ref ref="STDOUT" />
</root>
--
To stop receiving notification emails like this one, please contact
sruehl@apache.org.