You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/12/25 12:12:16 UTC
[plc4x] branch next-gen-core updated: Refactoring towards Generic
Connections Part 4: - S7Driver is now subclassing the new Generic Driver
which contains only getters for important Properties - Introduced new Stack
Configurer to automatically generate the "xxxMessage" Classes (using
Reflection) - Allowed Transport Layer to get Access to Connection String
Properties - Removed the "PlcByteToMessageCodec" Interface and implemented
ByteToMessageCodec directly on all classes to avoid confusion with
interfaces (they will all die ove [...]
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch next-gen-core
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/next-gen-core by this push:
new 74ef223 Refactoring towards Generic Connections Part 4: - S7Driver is now subclassing the new Generic Driver which contains only getters for important Properties - Introduced new Stack Configurer to automatically generate the "xxxMessage" Classes (using Reflection) - Allowed Transport Layer to get Access to Connection String Properties - Removed the "PlcByteToMessageCodec" Interface and implemented ByteToMessageCodec directly on all classes to avoid confusion with interfaces (th [...]
74ef223 is described below
commit 74ef223624496e7950a96000feb221b37d8470d6
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Wed Dec 25 13:12:06 2019 +0100
Refactoring towards Generic Connections Part 4:
- S7Driver is now subclassing the new Generic Driver which contains only getters for important Properties
- Introduced new Stack Configurer to automatically generate the "xxxMessage" Classes (using Reflection)
- Allowed Transport Layer to get Access to Connection String Properties
- Removed the "PlcByteToMessageCodec" Interface and implemented ByteToMessageCodec directly on all classes to avoid confusion with interfaces (they will all die overall as the codec layer will be generated, see GeneratedDriverByteToMessageCodec)
- Migrated KNX Driver (non-passive) to implement new GeneratedDriverBase
---
.../plc4x/java/abeth/protocol/AbEthProtocol.java | 2 +-
plc4j/protocols/iso-on-tcp/pom.xml | 4 ++
.../java/isoontcp/protocol/IsoOnTcpProtocol.java | 4 +-
.../spi/GeneratedDriverByteToMessageCodec.java | 6 +-
.../plc4x/java/spi/PlcByteToMessageCodec.java | 59 ----------------
.../spi/connection/DefaultNettyPlcConnection.java | 32 ++++-----
.../java/spi/connection/GeneratedDriverBase.java | 70 +++++++++++++++++++
.../connection/GeneratedProtocolMessageCodec.java | 80 ++++++++++++++++++++++
.../java/spi/connection/NettyChannelFactory.java | 26 +++++++
.../apache/plc4x/java/spi/connection/Parser.java | 29 ++++++++
.../spi/connection/ProtocolStackConfigurer.java | 30 ++++++++
.../java/spi/connection/ReflectionBasedIo.java | 63 +++++++++++++++++
.../plc4x/java/spi/connection/Serializer.java | 29 ++++++++
.../spi/connection/SingleProtocolStackBuilder.java | 64 +++++++++++++++++
.../connection/SingleProtocolStackConfigurer.java | 69 +++++++++++++++++++
.../plc4x/java/spi/parser/ConnectionParser.java | 13 ++++
.../tcp/connection/TcpSocketChannelFactory.java | 19 +++--
.../server/s7/protocol/S7Step7Protocol.java | 2 +-
.../java/bacnetip/protocol/BacNetIpProtocol.java | 2 +-
.../plc4x/java/df1/protocol/Df1Protocol.java | 4 +-
.../knxnetip/protocol/KnxNetIpProtocolMessage.java | 4 +-
.../apache/plc4x/java/knxnetip/KnxNetIpDriver.java | 61 ++++++++++-------
.../knxnetip/protocol/KnxNetIpProtocolMessage.java | 59 ----------------
.../javapassive/s7/protocol/PassiveS7Protocol.java | 4 +-
.../apache/plc4x/java/s7/readwrite/S7Driver.java | 73 +++++++++++++-------
.../s7/readwrite/protocol/S7ProtocolLogic.java | 11 +--
.../s7/readwrite/protocol/S7ProtocolMessage.java | 67 ------------------
27 files changed, 613 insertions(+), 273 deletions(-)
diff --git a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java
index 55cc499..badfc68 100644
--- a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java
+++ b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java
@@ -44,7 +44,7 @@ public class AbEthProtocol extends GeneratedDriverByteToMessageCodec<CIPEncapsul
public void serialize(WriteBuffer io, CIPEncapsulationPacket value) throws ParseException {
CIPEncapsulationPacketIO.serialize(io, value);
}
- });
+ }, CIPEncapsulationPacket.class);
logger.trace("Created new AB-ETH protocol");
}
diff --git a/plc4j/protocols/iso-on-tcp/pom.xml b/plc4j/protocols/iso-on-tcp/pom.xml
index ef3e738..d1f9917 100644
--- a/plc4j/protocols/iso-on-tcp/pom.xml
+++ b/plc4j/protocols/iso-on-tcp/pom.xml
@@ -49,6 +49,10 @@
</dependency>
<dependency>
<groupId>io.netty</groupId>
+ <artifactId>netty-codec</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</dependency>
diff --git a/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java b/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java
index d05cd98..3615584 100644
--- a/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java
+++ b/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java
@@ -21,15 +21,15 @@ package org.apache.plc4x.java.isoontcp.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageCodec;
import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
-import org.apache.plc4x.java.spi.PlcByteToMessageCodec;
import org.apache.plc4x.java.isoontcp.protocol.model.IsoOnTcpMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
-public class IsoOnTcpProtocol extends PlcByteToMessageCodec<IsoOnTcpMessage> {
+public class IsoOnTcpProtocol extends ByteToMessageCodec<IsoOnTcpMessage> {
static final byte ISO_ON_TCP_MAGIC_NUMBER = 0x03;
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
index ebce592..9b37e11 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
@@ -20,6 +20,7 @@ package org.apache.plc4x.java.spi;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageCodec;
import org.apache.commons.codec.binary.Hex;
import org.apache.plc4x.java.spi.generation.Message;
import org.apache.plc4x.java.spi.generation.MessageIO;
@@ -30,13 +31,14 @@ import org.slf4j.LoggerFactory;
import java.util.List;
-public abstract class GeneratedDriverByteToMessageCodec<T extends Message> extends PlcByteToMessageCodec<T> {
+public abstract class GeneratedDriverByteToMessageCodec<T extends Message> extends ByteToMessageCodec<T> {
private static final Logger logger = LoggerFactory.getLogger(GeneratedDriverByteToMessageCodec.class);
private MessageIO<T, T> io;
- public GeneratedDriverByteToMessageCodec(MessageIO<T, T> io) {
+ public GeneratedDriverByteToMessageCodec(MessageIO<T, T> io, Class<T> clazz) {
+ super(clazz);
this.io = io;
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/PlcByteToMessageCodec.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/PlcByteToMessageCodec.java
deleted file mode 100644
index 7c905d7..0000000
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/PlcByteToMessageCodec.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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;
-
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageCodec;
-import org.apache.commons.lang3.reflect.FieldUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.lang.reflect.Field;
-
-public abstract class PlcByteToMessageCodec<OUTBOUND_IN>
- extends ByteToMessageCodec<OUTBOUND_IN> {
-
- private static final Logger logger = LoggerFactory.getLogger(PlcByteToMessageCodec.class);
-
- private volatile ChannelHandler prevChannelHandler = null;
-
- public PlcByteToMessageCodec() {
- }
-
- public PlcByteToMessageCodec(Class<? extends OUTBOUND_IN> outboundMessageType) {
- super(outboundMessageType);
- }
-
- protected ChannelHandler getPrevChannelHandler(ChannelHandlerContext ctx) {
- if(prevChannelHandler == null) {
- try {
- Field prevField = FieldUtils.getField(ctx.getClass(), "prev", true);
- if(prevField != null) {
- ChannelHandlerContext prevContext = (ChannelHandlerContext) prevField.get(ctx);
- prevChannelHandler = prevContext.handler();
- }
- } catch(Exception e) {
- logger.error("Error accessing field 'prev'", e);
- }
- }
- return prevChannelHandler;
- }
-
-}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java
index 6fbbff1..2e478c3 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java
@@ -38,6 +38,7 @@ import org.apache.plc4x.java.spi.generation.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.net.SocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@@ -54,9 +55,8 @@ public class DefaultNettyPlcConnection<BASE_PROTOCOL_CLASS extends Message> exte
protected final boolean awaitSessionSetupComplete;
protected Channel channel;
protected boolean connected;
- private Class<BASE_PROTOCOL_CLASS> baseProtocolClass;
- private GeneratedDriverByteToMessageCodec<BASE_PROTOCOL_CLASS> messageCodec;
- private Plc4xProtocolBase<BASE_PROTOCOL_CLASS> protocolLogic;
+ private SocketAddress address;
+ private ProtocolStackConfigurer stackConfigurer;
protected DefaultNettyPlcConnection(ChannelFactory channelFactory) {
this(channelFactory, false);
@@ -75,14 +75,11 @@ public class DefaultNettyPlcConnection<BASE_PROTOCOL_CLASS extends Message> exte
this.connected = false;
}
- public DefaultNettyPlcConnection(ChannelFactory channelFactory, boolean awaitSessionSetupComplete, PlcFieldHandler handler,
- Class<BASE_PROTOCOL_CLASS> baseProtocolClass,
- GeneratedDriverByteToMessageCodec<BASE_PROTOCOL_CLASS> messageCodec,
- Plc4xProtocolBase<BASE_PROTOCOL_CLASS> protocolLogic) {
+ public DefaultNettyPlcConnection(SocketAddress address, ChannelFactory channelFactory, boolean awaitSessionSetupComplete, PlcFieldHandler handler,
+ ProtocolStackConfigurer stackConfigurer) {
this(channelFactory, awaitSessionSetupComplete, handler);
- this.baseProtocolClass = baseProtocolClass;
- this.messageCodec = messageCodec;
- this.protocolLogic = protocolLogic;
+ this.address = address;
+ this.stackConfigurer = stackConfigurer;
}
@@ -95,7 +92,10 @@ public class DefaultNettyPlcConnection<BASE_PROTOCOL_CLASS extends Message> exte
CompletableFuture<Void> sessionSetupCompleteFuture = new CompletableFuture<>();
// Have the channel factory create a new channel instance.
- channel = channelFactory.createChannel(getChannelHandler(sessionSetupCompleteFuture));
+ if (address == null) {
+ throw new IllegalStateException("No Address is known, please check driver implementation!");
+ }
+ channel = channelFactory.createChannel(address, getChannelHandler(sessionSetupCompleteFuture));
channel.closeFuture().addListener(future -> {
if (!sessionSetupCompleteFuture.isDone()) {
sessionSetupCompleteFuture.completeExceptionally(
@@ -155,8 +155,8 @@ public class DefaultNettyPlcConnection<BASE_PROTOCOL_CLASS extends Message> exte
}
public ChannelHandler getChannelHandler(CompletableFuture<Void> sessionSetupCompleteFuture) {
- if (messageCodec == null || protocolLogic == null || baseProtocolClass == null) {
- throw new IllegalStateException("Codec Classes have to be set!");
+ if (stackConfigurer == null) {
+ throw new IllegalStateException("No Protocol Stack Configurer is given!");
}
return new ChannelInitializer<Channel>() {
@Override
@@ -173,11 +173,7 @@ public class DefaultNettyPlcConnection<BASE_PROTOCOL_CLASS extends Message> exte
}
}
});
- pipeline.addLast(messageCodec);
- Plc4xProtocolBase<BASE_PROTOCOL_CLASS> plc4xS7Protocol = protocolLogic;
- setProtocol(plc4xS7Protocol);
- Plc4xNettyWrapper<BASE_PROTOCOL_CLASS> context = new Plc4xNettyWrapper<>(pipeline, plc4xS7Protocol, baseProtocolClass);
- pipeline.addLast(context);
+ setProtocol(stackConfigurer.apply(pipeline));
}
};
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java
new file mode 100644
index 0000000..8d61b8d
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java
@@ -0,0 +1,70 @@
+/*
+ * 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.connection;
+
+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;
+import org.apache.plc4x.java.spi.generation.Message;
+import org.apache.plc4x.java.spi.parser.ConnectionParser;
+
+public abstract class GeneratedDriverBase<CONFIGURATION, BASE_PACKET extends Message> implements PlcDriver {
+
+ protected abstract int getDefaultPortIPv4();
+
+ protected abstract Class<CONFIGURATION> getConfigurationClass();
+
+ protected abstract PlcFieldHandler getFieldHandler();
+
+ protected abstract Class<? extends NettyChannelFactory> getTransportChannelFactory();
+
+ protected abstract ProtocolStackConfigurer<BASE_PACKET> getStackConfigurer(CONFIGURATION configuration);
+
+ @Override
+ public PlcConnection connect(String url) throws PlcConnectionException {
+ ConnectionParser parser = new ConnectionParser(getProtocolCode(), url);
+ CONFIGURATION configuration = parser.createConfiguration(getConfigurationClass());
+
+ // Create Instance of Transport
+ NettyChannelFactory transport;
+ try {
+ transport = getTransportChannelFactory().newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new IllegalStateException("Cannot Instantiate Transport '"
+ + getTransportChannelFactory().getSimpleName()
+ + "'. Cannot access Default no Args Constructor.", e);
+ }
+ // Set all Properties
+ transport.setProperties(parser.getProperties());
+
+ return new DefaultNettyPlcConnection<>(
+ parser.getSocketAddress(getDefaultPortIPv4()),
+ transport,
+ true,
+ getFieldHandler(),
+ getStackConfigurer(configuration));
+ }
+
+ @Override
+ public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
+ throw new PlcConnectionException("Basic S7 connections don't support authentication (NG).");
+ }
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java
new file mode 100644
index 0000000..ea0bd87
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java
@@ -0,0 +1,80 @@
+/*
+ * 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.connection;
+
+import io.netty.buffer.ByteBuf;
+import org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec;
+import org.apache.plc4x.java.spi.generation.Message;
+import org.apache.plc4x.java.spi.generation.MessageIO;
+import org.apache.plc4x.java.spi.generation.ParseException;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+import org.apache.plc4x.java.spi.generation.WriteBuffer;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+class GeneratedProtocolMessageCodec<BASE_PAKET_CLASS extends Message> extends GeneratedDriverByteToMessageCodec<BASE_PAKET_CLASS> {
+
+ private final Function<ByteBuf, Integer> packetSizeEstimator;
+ private final Consumer<ByteBuf> corruptPackageRemover;
+
+ public GeneratedProtocolMessageCodec(Class<BASE_PAKET_CLASS> basePaketClass,
+ Parser<BASE_PAKET_CLASS> parser,
+ Serializer<BASE_PAKET_CLASS> serializer) {
+ this(basePaketClass, parser, serializer, null, null);
+ }
+
+ public GeneratedProtocolMessageCodec(
+ Class<BASE_PAKET_CLASS> basePaketClass,
+ Parser<BASE_PAKET_CLASS> parser,
+ Serializer<BASE_PAKET_CLASS> serializer,
+ Function<ByteBuf, Integer> packetSizeEstimator,
+ Consumer<ByteBuf> corruptPackageRemover) {
+ super(new MessageIO<BASE_PAKET_CLASS, BASE_PAKET_CLASS>() {
+ @Override
+ public BASE_PAKET_CLASS parse(ReadBuffer io) throws ParseException {
+ // return TPKTPacketIO.parse(io);
+ return parser.parse(io);
+ }
+
+ @Override
+ public void serialize(WriteBuffer io, BASE_PAKET_CLASS value) throws ParseException {
+ serializer.serialize(io, value);
+ }
+
+ }, basePaketClass);
+ this.packetSizeEstimator = packetSizeEstimator;
+ this.corruptPackageRemover = corruptPackageRemover;
+ }
+
+ @Override protected int getPacketSize(ByteBuf byteBuf) {
+ if (this.packetSizeEstimator == null) {
+ return -1;
+ }
+ return packetSizeEstimator.apply(byteBuf);
+ }
+
+ @Override protected void removeRestOfCorruptPackage(ByteBuf byteBuf) {
+ if (this.corruptPackageRemover == null) {
+ throw new IllegalStateException("This Implementation does not support Corrupt Package Removal!");
+ }
+ this.corruptPackageRemover.accept(byteBuf);
+ }
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/NettyChannelFactory.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/NettyChannelFactory.java
index 9a9416f..4fee7d7 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/NettyChannelFactory.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/NettyChannelFactory.java
@@ -40,6 +40,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
+import java.util.Properties;
/**
* Adapter with sensible defaults for a Netty Based Channel Factory.
@@ -56,6 +57,7 @@ public abstract class NettyChannelFactory implements ChannelFactory {
* TODO should be removed together with the Construcotr.
*/
private SocketAddress address;
+ private Properties properties;
/**
* @Deprecated Only there for Retrofit
@@ -167,4 +169,28 @@ public abstract class NettyChannelFactory implements ChannelFactory {
throw new PlcConnectionException("Unable to ping remote host");
}
}
+
+ public Properties getProperties() {
+ // Null Safety for older implementations
+ if (properties == null) {
+ return new Properties();
+ }
+ return properties;
+ }
+
+ public void setProperties(Properties properties) {
+ this.properties = properties;
+ }
+
+ protected String getProperty(String key) {
+ return ((String) getProperties().get(key));
+ }
+
+ protected boolean hasProperty(String key) {
+ return getProperties().contains(key);
+ }
+
+ protected String getPropertyOrDefault(String key, String defaultValue) {
+ return getProperties().getProperty(key, defaultValue);
+ }
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Parser.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Parser.java
new file mode 100644
index 0000000..bd77710
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Parser.java
@@ -0,0 +1,29 @@
+/*
+ * 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.connection;
+
+import org.apache.plc4x.java.spi.generation.Message;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+
+public interface Parser<RESULT extends Message> {
+
+ RESULT parse(ReadBuffer io);
+
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ProtocolStackConfigurer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ProtocolStackConfigurer.java
new file mode 100644
index 0000000..6e335f8
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ProtocolStackConfigurer.java
@@ -0,0 +1,30 @@
+/*
+ * 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.connection;
+
+import io.netty.channel.ChannelPipeline;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
+import org.apache.plc4x.java.spi.generation.Message;
+
+public interface ProtocolStackConfigurer<T extends Message> {
+
+ /** Applies the given Stack to the Pipeline and returns the wired instance */
+ Plc4xProtocolBase<T> apply(ChannelPipeline pipeline);
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ReflectionBasedIo.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ReflectionBasedIo.java
new file mode 100644
index 0000000..e95a9b1
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/ReflectionBasedIo.java
@@ -0,0 +1,63 @@
+/*
+ * 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.connection;
+
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.apache.plc4x.java.spi.generation.Message;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+import org.apache.plc4x.java.spi.generation.WriteBuffer;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class ReflectionBasedIo<BASE extends Message> implements Parser<BASE>, Serializer<BASE> {
+
+ private final Method parseMethod;
+ private final Method serializeMethod;
+
+ public ReflectionBasedIo(Class<BASE> clazz) {
+ String className = clazz.getSimpleName() + "IO";
+ String fqcn = clazz.getPackage().getName() + ".io." + className;
+ try {
+ Class<?> ioClass = Class.forName(fqcn);
+ parseMethod = MethodUtils.getMatchingMethod(ioClass, "parse", ReadBuffer.class);
+ serializeMethod = MethodUtils.getMatchingMethod(ioClass, "serialize", WriteBuffer.class, clazz);
+ } catch (ClassNotFoundException e) {
+ throw new IllegalStateException(
+ String.format("Unnable to get suitable IO Class for given Message. Expected IO Class '%s' for Class '%s'", clazz.getName(), fqcn));
+ }
+ }
+
+ @Override public BASE parse(ReadBuffer io) {
+ try {
+ return (BASE) parseMethod.invoke(null, io);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalStateException("Unable to use the parse Method!", e);
+ }
+ }
+
+ @Override public void serialize(WriteBuffer io, BASE message) {
+ try {
+ serializeMethod.invoke(null, io, message);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ throw new IllegalStateException("Unable to use the serialize Method!", e);
+ }
+ }
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Serializer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Serializer.java
new file mode 100644
index 0000000..7f741e0
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/Serializer.java
@@ -0,0 +1,29 @@
+/*
+ * 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.connection;
+
+import org.apache.plc4x.java.spi.generation.Message;
+import org.apache.plc4x.java.spi.generation.WriteBuffer;
+
+public interface Serializer<INPUT extends Message> {
+
+ void serialize(WriteBuffer io, INPUT message);
+
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackBuilder.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackBuilder.java
new file mode 100644
index 0000000..5b4e253
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackBuilder.java
@@ -0,0 +1,64 @@
+/*
+ * 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.connection;
+
+import io.netty.buffer.ByteBuf;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
+import org.apache.plc4x.java.spi.generation.Message;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+/**
+ * Used to Build Instances of {@link SingleProtocolStackConfigurer}.
+ *
+ * @param <BASE_PAKET_CLASS> Type of Created Message that is Exchanged.
+ */
+public final class SingleProtocolStackBuilder<BASE_PAKET_CLASS extends Message> {
+
+ private Class<BASE_PAKET_CLASS> basePaketClass;
+ private Plc4xProtocolBase<BASE_PAKET_CLASS> protocol;
+ private Function<ByteBuf, Integer> packetSizeEstimator;
+ private Consumer<ByteBuf> corruptPacketRemover;
+
+ SingleProtocolStackBuilder(Class<BASE_PAKET_CLASS> basePaketClass) {
+ this.basePaketClass = basePaketClass;
+ }
+
+ public SingleProtocolStackBuilder<BASE_PAKET_CLASS> withProtocol(Plc4xProtocolBase<BASE_PAKET_CLASS> protocol) {
+ this.protocol = protocol;
+ return this;
+ }
+
+ public SingleProtocolStackBuilder<BASE_PAKET_CLASS> withPacketSizeEstimator(Function<ByteBuf, Integer> packetSizeEstimator) {
+ this.packetSizeEstimator = packetSizeEstimator;
+ return this;
+ }
+
+ public SingleProtocolStackBuilder<BASE_PAKET_CLASS> withCorruptPacketRemover(Consumer<ByteBuf> corruptPacketRemover) {
+ this.corruptPacketRemover = corruptPacketRemover;
+ return this;
+ }
+
+ public SingleProtocolStackConfigurer<BASE_PAKET_CLASS> build() {
+ assert this.protocol != null;
+ return new SingleProtocolStackConfigurer<>(basePaketClass, protocol, packetSizeEstimator, corruptPacketRemover);
+ }
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java
new file mode 100644
index 0000000..edcd302
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java
@@ -0,0 +1,69 @@
+/*
+ * 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.connection;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelPipeline;
+import org.apache.plc4x.java.spi.Plc4xNettyWrapper;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
+import org.apache.plc4x.java.spi.generation.Message;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+/**
+ * Builds a Protocol Stack.
+ */
+public class SingleProtocolStackConfigurer<BASE_PAKET_CLASS extends Message> implements ProtocolStackConfigurer<BASE_PAKET_CLASS> {
+
+ private final Class<BASE_PAKET_CLASS> basePaketClass;
+ private final Plc4xProtocolBase<BASE_PAKET_CLASS> protocol;
+ private final Function<ByteBuf, Integer> packetSizeEstimator;
+ private final Consumer<ByteBuf> corruptPacketRemover;
+
+ /** Only accessible via Builder */
+ SingleProtocolStackConfigurer(Class<BASE_PAKET_CLASS> basePaketClass, Plc4xProtocolBase<BASE_PAKET_CLASS> protocol,
+ Function<ByteBuf, Integer> packetSizeEstimator,
+ Consumer<ByteBuf> corruptPacketRemover) {
+ this.basePaketClass = basePaketClass;
+ this.protocol = protocol;
+ this.packetSizeEstimator = packetSizeEstimator;
+ this.corruptPacketRemover = corruptPacketRemover;
+ }
+
+ public static <BPC extends Message> SingleProtocolStackBuilder<BPC> builder(Class<BPC> basePaketClass) {
+ return new SingleProtocolStackBuilder<>(basePaketClass);
+ }
+
+ private ChannelHandler getMessageCodec() {
+ ReflectionBasedIo<BASE_PAKET_CLASS> io = new ReflectionBasedIo<>(basePaketClass);
+ return new GeneratedProtocolMessageCodec<>(basePaketClass, io, io, packetSizeEstimator, corruptPacketRemover);
+ }
+
+ /** Applies the given Stack to the Pipeline */
+ @Override public Plc4xProtocolBase<BASE_PAKET_CLASS> apply(ChannelPipeline pipeline) {
+ pipeline.addLast(getMessageCodec());
+ Plc4xNettyWrapper<BASE_PAKET_CLASS> context = new Plc4xNettyWrapper<>(pipeline, protocol, basePaketClass);
+ pipeline.addLast(context);
+ return protocol;
+ }
+
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/parser/ConnectionParser.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/parser/ConnectionParser.java
index f02e61a..9ad8f89 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/parser/ConnectionParser.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/parser/ConnectionParser.java
@@ -38,6 +38,7 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -96,6 +97,18 @@ public class ConnectionParser {
}
}
+ /**
+ * Returns all Properties as Properties Object
+ * All Keys are translated to Upper Case
+ */
+ public Properties getProperties() {
+ Properties properties = new Properties();
+ splitQuery(uri).entrySet()
+ .forEach(entry -> properties.setProperty(entry.getKey().toUpperCase(), entry.getValue().get(0)));
+
+ return properties;
+ }
+
// TODO Respect Path Params
public <T> T createConfiguration(Class<T> pClazz) {
Map<String, Field> fieldMap = Arrays.stream(FieldUtils.getAllFields(pClazz))
diff --git a/plc4j/transports/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java b/plc4j/transports/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
index af83813..7653db0 100644
--- a/plc4j/transports/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
+++ b/plc4j/transports/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
@@ -46,6 +46,14 @@ public class TcpSocketChannelFactory extends NettyChannelFactory {
private static final Logger logger = LoggerFactory.getLogger(TcpSocketChannelFactory.class);
+ public static final String SO_KEEPALIVE = "SO_KEEPALIVE";
+ public static final String CONNECT_TIMEOUT_MILLIS = "CONNECT_TIMEOUT_MILLIS";
+ private static final String TCP_NODELAY = "TCP_NODELAY";
+
+ public TcpSocketChannelFactory() {
+ // Default Constructor
+ }
+
/**
* Only there for retrofit
*/
@@ -68,10 +76,13 @@ public class TcpSocketChannelFactory extends NettyChannelFactory {
}
@Override public void configureBootstrap(Bootstrap bootstrap) {
- bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
- bootstrap.option(ChannelOption.TCP_NODELAY, true);
- // TODO we should use an explicit (configurable?) timeout here
- // bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
+ boolean keepalive = Boolean.parseBoolean(getPropertyOrDefault(SO_KEEPALIVE, "true"));
+ boolean nodelay = Boolean.parseBoolean(getPropertyOrDefault(TCP_NODELAY, "true"));
+ int connectTimeout = Integer.parseInt(getPropertyOrDefault(CONNECT_TIMEOUT_MILLIS, "1000"));
+ logger.info("Configuring Bootstrap with properties\n\t{} {}\n\t{} {}\n\t{} {}", SO_KEEPALIVE, keepalive, TCP_NODELAY, nodelay, CONNECT_TIMEOUT_MILLIS, connectTimeout);
+ bootstrap.option(ChannelOption.SO_KEEPALIVE, keepalive);
+ bootstrap.option(ChannelOption.TCP_NODELAY, nodelay);
+ bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
}
}
diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
index 5b58dfa..5fb548d 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
@@ -50,7 +50,7 @@ public class S7Step7Protocol extends GeneratedDriverByteToMessageCodec<TPKTPacke
throw new ParseException("Error serializing message", e);
}
}
- });
+ }, TPKTPacket.class);
}
@Override
diff --git a/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocol.java b/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocol.java
index 4c93d68..973803c 100644
--- a/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocol.java
+++ b/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocol.java
@@ -40,7 +40,7 @@ public class BacNetIpProtocol extends GeneratedDriverByteToMessageCodec<BVLC> {
public void serialize(WriteBuffer io, BVLC value) throws ParseException {
BVLCIO.serialize(io, value);
}
- });
+ }, BVLC.class);
}
@Override
diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java
index 273d875..7d4d2a0 100644
--- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java
+++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java
@@ -21,8 +21,8 @@ package org.apache.plc4x.java.df1.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageCodec;
import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
-import org.apache.plc4x.java.spi.PlcByteToMessageCodec;
import org.apache.plc4x.java.df1.readwrite.DF1Command;
import org.apache.plc4x.java.df1.readwrite.DF1Symbol;
import org.apache.plc4x.java.df1.readwrite.DF1SymbolMessageFrame;
@@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory;
import java.util.List;
-public class Df1Protocol extends PlcByteToMessageCodec<DF1Command> {
+public class Df1Protocol extends ByteToMessageCodec<DF1Command> {
private static final Logger logger = LoggerFactory.getLogger(Df1Protocol.class);
diff --git a/sandbox/test-java-knxnetip-driver-passive/src/main/java/org/apache/plc4x/java/passive/knxnetip/protocol/KnxNetIpProtocolMessage.java b/sandbox/test-java-knxnetip-driver-passive/src/main/java/org/apache/plc4x/java/passive/knxnetip/protocol/KnxNetIpProtocolMessage.java
index fec502f..b0ae72b 100644
--- a/sandbox/test-java-knxnetip-driver-passive/src/main/java/org/apache/plc4x/java/passive/knxnetip/protocol/KnxNetIpProtocolMessage.java
+++ b/sandbox/test-java-knxnetip-driver-passive/src/main/java/org/apache/plc4x/java/passive/knxnetip/protocol/KnxNetIpProtocolMessage.java
@@ -19,9 +19,9 @@ under the License.
package org.apache.plc4x.java.passive.knxnetip.protocol;
import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec;
import org.apache.plc4x.java.knxnetip.passive.KNXNetIPMessage;
import org.apache.plc4x.java.knxnetip.passive.io.KNXNetIPMessageIO;
+import org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec;
import org.apache.plc4x.java.spi.generation.MessageIO;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
@@ -40,7 +40,7 @@ public class KnxNetIpProtocolMessage extends GeneratedDriverByteToMessageCodec<K
public void serialize(WriteBuffer io, KNXNetIPMessage value) throws ParseException {
// Ignore.
}
- });
+ }, KNXNetIPMessage.class);
}
@Override
diff --git a/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java b/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java
index 9ef6e2b..f39147e 100644
--- a/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java
+++ b/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java
@@ -18,22 +18,21 @@ under the License.
*/
package org.apache.plc4x.java.knxnetip;
-import org.apache.plc4x.java.api.PlcConnection;
-import org.apache.plc4x.java.api.authentication.PlcAuthentication;
-import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.base.connection.UdpSocketChannelFactory;
import org.apache.plc4x.java.knxnetip.connection.KnxNetIpConfiguration;
-import org.apache.plc4x.java.api.PlcDriver;
import org.apache.plc4x.java.knxnetip.connection.KnxNetIpFieldHandler;
import org.apache.plc4x.java.knxnetip.protocol.KnxNetIpProtocolLogic;
-import org.apache.plc4x.java.knxnetip.protocol.KnxNetIpProtocolMessage;
import org.apache.plc4x.java.knxnetip.readwrite.KNXNetIPMessage;
-import org.apache.plc4x.java.spi.connection.DefaultNettyPlcConnection;
-import org.apache.plc4x.java.spi.parser.ConnectionParser;
+import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
+import org.apache.plc4x.java.spi.connection.NettyChannelFactory;
+import org.apache.plc4x.java.spi.connection.PlcFieldHandler;
+import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer;
+import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer;
-import java.net.*;
+import java.util.function.Function;
-public class KnxNetIpDriver implements PlcDriver {
+public class KnxNetIpDriver extends GeneratedDriverBase<KnxNetIpConfiguration, KNXNetIPMessage> {
public static final int KNXNET_IP_PORT = 3671;
@@ -47,23 +46,37 @@ public class KnxNetIpDriver implements PlcDriver {
return "KNXNet/IP";
}
- @Override
- public PlcConnection connect(String connectionString) throws PlcConnectionException {
- ConnectionParser parser = new ConnectionParser(getProtocolCode(), connectionString);
- KnxNetIpConfiguration configuration = parser.createConfiguration(KnxNetIpConfiguration.class);
- SocketAddress address = parser.getSocketAddress(KNXNET_IP_PORT);
- return new DefaultNettyPlcConnection<>(new UdpSocketChannelFactory(address), true, new KnxNetIpFieldHandler(),
- KNXNetIPMessage.class,
- new KnxNetIpProtocolMessage(),
- new KnxNetIpProtocolLogic(
- configuration
- )
- );
+ @Override protected int getDefaultPortIPv4() {
+ return KNXNET_IP_PORT;
}
- @Override
- public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
- throw new PlcConnectionException("KNXNet/IP connections don't support authentication.");
+ @Override protected Class<KnxNetIpConfiguration> getConfigurationClass() {
+ return KnxNetIpConfiguration.class;
+ }
+
+ @Override protected PlcFieldHandler getFieldHandler() {
+ return new KnxNetIpFieldHandler();
+ }
+
+ @Override protected Class<? extends NettyChannelFactory> getTransportChannelFactory() {
+ return UdpSocketChannelFactory.class;
}
+ @Override protected ProtocolStackConfigurer<KNXNetIPMessage> getStackConfigurer(KnxNetIpConfiguration knxNetIpConfiguration) {
+ return SingleProtocolStackConfigurer.builder(KNXNetIPMessage.class)
+ .withProtocol(new KnxNetIpProtocolLogic(knxNetIpConfiguration))
+ .withPacketSizeEstimator((new PacketSizeEstimator()))
+ .build();
+ }
+
+ private static class PacketSizeEstimator implements Function<ByteBuf, Integer> {
+
+ @Override public Integer apply(ByteBuf byteBuf) {
+ if (byteBuf.readableBytes() >= 6) {
+ return byteBuf.getUnsignedShort(byteBuf.readerIndex() + 4);
+ }
+ return -1;
+ }
+
+ }
}
diff --git a/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolMessage.java b/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolMessage.java
deleted file mode 100644
index 1c93ac0..0000000
--- a/sandbox/test-java-knxnetip-driver/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolMessage.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
-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.knxnetip.protocol;
-
-import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec;
-import org.apache.plc4x.java.knxnetip.readwrite.KNXNetIPMessage;
-import org.apache.plc4x.java.knxnetip.readwrite.io.KNXNetIPMessageIO;
-import org.apache.plc4x.java.spi.generation.MessageIO;
-import org.apache.plc4x.java.spi.generation.ParseException;
-import org.apache.plc4x.java.spi.generation.ReadBuffer;
-import org.apache.plc4x.java.spi.generation.WriteBuffer;
-
-public class KnxNetIpProtocolMessage extends GeneratedDriverByteToMessageCodec<KNXNetIPMessage> {
-
- public KnxNetIpProtocolMessage() {
- super(new MessageIO<KNXNetIPMessage, KNXNetIPMessage>() {
- @Override
- public KNXNetIPMessage parse(ReadBuffer io) throws ParseException {
- return KNXNetIPMessageIO.parse(io);
- }
-
- @Override
- public void serialize(WriteBuffer io, KNXNetIPMessage value) throws ParseException {
- KNXNetIPMessageIO.serialize(io, value);
- }
- });
- }
-
- @Override
- protected int getPacketSize(ByteBuf byteBuf) {
- if(byteBuf.readableBytes() >= 6) {
- return byteBuf.getUnsignedShort(byteBuf.readerIndex() + 4);
- }
- return -1;
- }
-
- @Override
- protected void removeRestOfCorruptPackage(ByteBuf byteBuf) {
- // Not really any marker byte to look for.
- }
-
-}
diff --git a/sandbox/test-java-s7-driver-passive/src/main/java/org/apache/plc4x/javapassive/s7/protocol/PassiveS7Protocol.java b/sandbox/test-java-s7-driver-passive/src/main/java/org/apache/plc4x/javapassive/s7/protocol/PassiveS7Protocol.java
index 00f3260..1c35c04 100644
--- a/sandbox/test-java-s7-driver-passive/src/main/java/org/apache/plc4x/javapassive/s7/protocol/PassiveS7Protocol.java
+++ b/sandbox/test-java-s7-driver-passive/src/main/java/org/apache/plc4x/javapassive/s7/protocol/PassiveS7Protocol.java
@@ -20,7 +20,7 @@ package org.apache.plc4x.javapassive.s7.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
-import org.apache.plc4x.java.spi.PlcByteToMessageCodec;
+import io.netty.handler.codec.ByteToMessageCodec;
import org.apache.plc4x.java.s7.passive.TPKTPacket;
import org.apache.plc4x.java.s7.passive.io.TPKTPacketIO;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
@@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory;
import java.util.List;
-public class PassiveS7Protocol extends PlcByteToMessageCodec<TPKTPacket> {
+public class PassiveS7Protocol extends ByteToMessageCodec<TPKTPacket> {
private static final Logger logger = LoggerFactory.getLogger(PassiveS7Protocol.class);
diff --git a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java b/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java
index 7cd4bfa..5e1765d 100644
--- a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java
+++ b/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java
@@ -18,24 +18,23 @@ under the License.
*/
package org.apache.plc4x.java.s7.readwrite;
-import org.apache.plc4x.java.api.PlcConnection;
+import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.api.PlcDriver;
-import org.apache.plc4x.java.api.authentication.PlcAuthentication;
-import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.s7.readwrite.connection.S7Configuration;
import org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic;
-import org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolMessage;
import org.apache.plc4x.java.s7.readwrite.utils.S7PlcFieldHandler;
-import org.apache.plc4x.java.spi.connection.DefaultNettyPlcConnection;
-import org.apache.plc4x.java.spi.parser.ConnectionParser;
+import org.apache.plc4x.java.spi.connection.NettyChannelFactory;
+import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer;
+import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
+import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer;
import org.apache.plc4x.java.tcp.connection.TcpSocketChannelFactory;
import org.osgi.service.component.annotations.Component;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
+import java.util.function.Consumer;
+import java.util.function.Function;
@Component(service = PlcDriver.class, immediate = true)
-public class S7Driver implements PlcDriver {
+public class S7Driver extends GeneratedDriverBase<S7Configuration, TPKTPacket> {
private static final int ISO_ON_TCP_PORT = 102;
@@ -49,23 +48,49 @@ public class S7Driver implements PlcDriver {
return "Siemens S7 (Basic)";
}
- @Override
- public PlcConnection connect(String url) throws PlcConnectionException {
- ConnectionParser parser = new ConnectionParser(getProtocolCode(), url);
- S7Configuration configuration = parser.createConfiguration(S7Configuration.class);
- SocketAddress address = parser.getSocketAddress(ISO_ON_TCP_PORT);
- return new DefaultNettyPlcConnection<>(new TcpSocketChannelFactory(address), true, new S7PlcFieldHandler(),
- TPKTPacket.class,
- new S7ProtocolMessage(),
- new S7ProtocolLogic(
- configuration
- )
- );
+ @Override protected int getDefaultPortIPv4() {
+ return ISO_ON_TCP_PORT;
}
- @Override
- public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
- throw new PlcConnectionException("Basic S7 connections don't support authentication (NG).");
+ @Override protected Class<S7Configuration> getConfigurationClass() {
+ return S7Configuration.class;
+ }
+
+ @Override protected S7PlcFieldHandler getFieldHandler() {
+ return new S7PlcFieldHandler();
}
+ @Override protected Class<? extends NettyChannelFactory> getTransportChannelFactory() {
+ return TcpSocketChannelFactory.class;
+ }
+
+ @Override protected ProtocolStackConfigurer<TPKTPacket> getStackConfigurer(S7Configuration configuration) {
+ return SingleProtocolStackConfigurer.builder(TPKTPacket.class)
+ .withProtocol(new S7ProtocolLogic(configuration))
+ .withPacketSizeEstimator(new ByteLengthEstimator())
+ .withCorruptPacketRemover(new CorruptPackageCleaner())
+ .build();
+ }
+
+ /** Estimate the Length of a Packet */
+ public static class ByteLengthEstimator implements Function<ByteBuf, Integer> {
+
+ @Override public Integer apply(ByteBuf byteBuf) {
+ if (byteBuf.readableBytes() >= 4) {
+ return byteBuf.getUnsignedShort(byteBuf.readerIndex() + 2);
+ }
+ return -1;
+ }
+ }
+
+ /** Consumes all Bytes till another Magic Byte is found */
+ public static class CorruptPackageCleaner implements Consumer<ByteBuf> {
+
+ @Override public void accept(ByteBuf byteBuf) {
+ while (byteBuf.getUnsignedByte(0) != TPKTPacket.PROTOCOLID) {
+ // Just consume the bytes till the next possible start position.
+ byteBuf.readByte();
+ }
+ }
+ }
}
diff --git a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java b/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
index 247fbad..c927eb2 100644
--- a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
+++ b/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
@@ -63,6 +63,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
private static final Logger logger = LoggerFactory.getLogger(S7ProtocolLogic.class);
+ public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);
private int callingTsapId;
private int calledTsapId;
@@ -94,12 +95,12 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
TPKTPacket packet = new TPKTPacket(createCOTPConnectionRequest(calledTsapId, callingTsapId, cotpTpduSize));
context.sendRequest(packet)
- .expectResponse(TPKTPacket.class, Duration.ofMillis(1000))
+ .expectResponse(TPKTPacket.class, REQUEST_TIMEOUT)
.check(p -> p.getPayload() instanceof COTPPacketConnectionResponse)
.unwrap(p -> (COTPPacketConnectionResponse) p.getPayload())
.handle(cotpPacketConnectionResponse -> {
context.sendRequest(createS7ConnectionRequest(cotpPacketConnectionResponse))
- .expectResponse(TPKTPacket.class, Duration.ofMillis(1000))
+ .expectResponse(TPKTPacket.class, REQUEST_TIMEOUT)
.unwrap(TPKTPacket::getPayload)
.only(COTPPacketData.class)
.unwrap(COTPPacket::getPayload)
@@ -121,7 +122,7 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
// Prepare a message to request the remote to identify itself.
TPKTPacket tpktPacket = createIdentifyRemoteMessage();
context.sendRequest(tpktPacket)
- .expectResponse(TPKTPacket.class, Duration.ofMillis(1000))
+ .expectResponse(TPKTPacket.class, REQUEST_TIMEOUT)
.check(p -> p.getPayload() instanceof COTPPacketData)
.unwrap(p -> ((COTPPacketData) p.getPayload()))
.check(p -> p.getPayload() instanceof S7MessageUserData)
@@ -151,7 +152,7 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
true, (short) tpduId));
context.sendRequest(tpktPacket)
- .expectResponse(TPKTPacket.class, Duration.ofMillis(1000))
+ .expectResponse(TPKTPacket.class, REQUEST_TIMEOUT)
.onTimeout(future::completeExceptionally)
.onError((p, e) -> future.completeExceptionally(e))
.check(p -> p.getPayload() instanceof COTPPacketData)
@@ -190,7 +191,7 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
true, (short) tpduId));
context.sendRequest(tpktPacket)
- .expectResponse(TPKTPacket.class, Duration.ofMillis(1000))
+ .expectResponse(TPKTPacket.class, REQUEST_TIMEOUT)
.onTimeout(future::completeExceptionally)
.onError((p, e) -> future.completeExceptionally(e))
.check(p -> p.getPayload() instanceof COTPPacketData)
diff --git a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolMessage.java b/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolMessage.java
deleted file mode 100644
index 437229b..0000000
--- a/sandbox/test-java-s7-driver/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolMessage.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
-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.s7.readwrite.protocol;
-
-import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.s7.readwrite.TPKTPacket;
-import org.apache.plc4x.java.s7.readwrite.io.TPKTPacketIO;
-import org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec;
-import org.apache.plc4x.java.spi.generation.MessageIO;
-import org.apache.plc4x.java.spi.generation.ParseException;
-import org.apache.plc4x.java.spi.generation.ReadBuffer;
-import org.apache.plc4x.java.spi.generation.WriteBuffer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-public class S7ProtocolMessage extends GeneratedDriverByteToMessageCodec<TPKTPacket> {
-
- private static final Logger logger = LoggerFactory.getLogger(S7ProtocolMessage.class);
-
- public S7ProtocolMessage() {
- super(new MessageIO<TPKTPacket, TPKTPacket>() {
- @Override
- public TPKTPacket parse(ReadBuffer io) throws ParseException {
- return TPKTPacketIO.parse(io);
- }
-
- @Override
- public void serialize(WriteBuffer io, TPKTPacket value) throws ParseException {
- TPKTPacketIO.serialize(io, value);
- }
- });
- }
-
- @Override
- protected int getPacketSize(ByteBuf byteBuf) {
- if(byteBuf.readableBytes() >= 4) {
- return byteBuf.getUnsignedShort(byteBuf.readerIndex() + 2);
- }
- return -1;
- }
-
- @Override
- protected void removeRestOfCorruptPackage(ByteBuf byteBuf) {
- while (byteBuf.getUnsignedByte(0) != TPKTPacket.PROTOCOLID) {
- // Just consume the bytes till the next possible start position.
- byteBuf.readByte();
- }
- }
-
-}