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 01e02b00f6d2b54ed5ee731afe53fcbe57f4cb75 Author: Christofer Dutz <[email protected]> AuthorDate: Fri Aug 30 16:51:07 2019 +0200 - Refactored the AbEthProtocol to use a generic netty driver layer build to support generated drivers --- .../resources/templates/java/pojo-template.ftlh | 5 ++- plc4j/protocols/driver-bases/base/pom.xml | 5 +++ .../base/GeneratedDriverByteToMessageCodec.java | 25 +++++++------- .../java/org/apache/plc4x/java/utils/Message.java | 3 ++ .../org/apache/plc4x/java/utils/SizeAware.java | 26 -------------- .../protocol/test/ProtocolTestsuiteRunner.java | 2 +- .../plc4x/java/abeth/protocol/AbEthProtocol.java | 40 ++-------------------- 7 files changed, 26 insertions(+), 80 deletions(-) diff --git a/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh index b3fbaf8..c8c5fb6 100644 --- a/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh +++ b/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh @@ -40,10 +40,9 @@ package ${helper.packageName(protocolName, languageName, outputFlavor)}; import com.fasterxml.jackson.annotation.*; import org.apache.plc4x.java.utils.Message; -import org.apache.plc4x.java.utils.SizeAware; <#if type.abstract>@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "className")</#if> -public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??> extends ${type.parentType.name}</#if> implements SizeAware, Message { +public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??> extends ${type.parentType.name}</#if> implements Message { <#if helper.isDiscriminatedType(type)> @@ -117,7 +116,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType?? <#if helper.isSimpleType(field.type)> lengthInBits += ${field.type.size} * ${field.name}.length; <#else> - for(SizeAware element : ${field.name}) { + for(Message element : ${field.name}) { lengthInBits += element.getLengthInBytes() * 8; } </#if> diff --git a/plc4j/protocols/driver-bases/base/pom.xml b/plc4j/protocols/driver-bases/base/pom.xml index aa0cbcc..e27b060 100644 --- a/plc4j/protocols/driver-bases/base/pom.xml +++ b/plc4j/protocols/driver-bases/base/pom.xml @@ -37,6 +37,11 @@ <artifactId>plc4j-api</artifactId> <version>0.5.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>org.apache.plc4x</groupId> + <artifactId>plc4j-utils-driver-base-java</artifactId> + <version>0.5.0-SNAPSHOT</version> + </dependency> <dependency> <groupId>org.apache.commons</groupId> diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/GeneratedDriverByteToMessageCodec.java similarity index 67% copy from sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java copy to plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/GeneratedDriverByteToMessageCodec.java index e8ad9e0..4ac7fb6 100644 --- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java +++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/GeneratedDriverByteToMessageCodec.java @@ -16,13 +16,12 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package org.apache.plc4x.java.abeth.protocol; +package org.apache.plc4x.java.base; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import org.apache.plc4x.java.abeth.readwrite.CIPEncapsulationPacket; -import org.apache.plc4x.java.abeth.readwrite.io.CIPEncapsulationPacketIO; -import org.apache.plc4x.java.base.PlcByteToMessageCodec; +import org.apache.plc4x.java.utils.Message; +import org.apache.plc4x.java.utils.MessageIO; import org.apache.plc4x.java.utils.ReadBuffer; import org.apache.plc4x.java.utils.WriteBuffer; import org.slf4j.Logger; @@ -30,20 +29,20 @@ import org.slf4j.LoggerFactory; import java.util.List; -public class AbEthProtocol extends PlcByteToMessageCodec<CIPEncapsulationPacket> { +public abstract class GeneratedDriverByteToMessageCodec<T extends Message> extends PlcByteToMessageCodec<T> { - private static final Logger logger = LoggerFactory.getLogger(AbEthProtocol.class); + private static final Logger logger = LoggerFactory.getLogger(GeneratedDriverByteToMessageCodec.class); - private CIPEncapsulationPacketIO io; + private MessageIO<T, T> io; - public AbEthProtocol() { - io = new CIPEncapsulationPacketIO(); + public GeneratedDriverByteToMessageCodec(MessageIO<T, T> io) { + this.io = io; } @Override - protected void encode(ChannelHandlerContext ctx, CIPEncapsulationPacket cipEncapsulationPacket, ByteBuf byteBuf) throws Exception { - WriteBuffer buffer = new WriteBuffer(cipEncapsulationPacket.getLengthInBytes()); - io.serialize(buffer, cipEncapsulationPacket); + protected void encode(ChannelHandlerContext ctx, T packet, ByteBuf byteBuf) throws Exception { + WriteBuffer buffer = new WriteBuffer(packet.getLengthInBytes()); + io.serialize(buffer, packet); byteBuf.writeBytes(buffer.getData()); } @@ -54,7 +53,7 @@ public class AbEthProtocol extends PlcByteToMessageCodec<CIPEncapsulationPacket> ReadBuffer readBuffer = new ReadBuffer(bytes); while (readBuffer.getPos() < bytes.length) { try { - CIPEncapsulationPacket packet = io.parse(readBuffer); + T packet = io.parse(readBuffer); out.add(packet); } catch (Exception e) { logger.warn("Error decoding package: " + e.getMessage()); diff --git a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/Message.java b/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/Message.java index 7bc60b6..e12a6bb 100644 --- a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/Message.java +++ b/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/Message.java @@ -20,4 +20,7 @@ package org.apache.plc4x.java.utils; public interface Message { + + int getLengthInBytes(); + } diff --git a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/SizeAware.java b/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/SizeAware.java deleted file mode 100644 index c11d379..0000000 --- a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/SizeAware.java +++ /dev/null @@ -1,26 +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.utils; - -public interface SizeAware { - - int getLengthInBytes(); - -} diff --git a/plc4j/utils/protocol-test-utils/src/main/java/org/apache/plc4x/protocol/test/ProtocolTestsuiteRunner.java b/plc4j/utils/protocol-test-utils/src/main/java/org/apache/plc4x/protocol/test/ProtocolTestsuiteRunner.java index 7e1f299..9e8a3f1 100644 --- a/plc4j/utils/protocol-test-utils/src/main/java/org/apache/plc4x/protocol/test/ProtocolTestsuiteRunner.java +++ b/plc4j/utils/protocol-test-utils/src/main/java/org/apache/plc4x/protocol/test/ProtocolTestsuiteRunner.java @@ -112,7 +112,7 @@ public class ProtocolTestsuiteRunner { System.out.println(xmlString); throw new ProtocolTestsuiteException("Differences were found after parsing.\n" + diff.toString()); } - WriteBuffer writeBuffer = new WriteBuffer(((SizeAware) msg).getLengthInBytes(), testSuite.isLittleEndian()); + WriteBuffer writeBuffer = new WriteBuffer(((Message) msg).getLengthInBytes(), testSuite.isLittleEndian()); messageIO.serialize(writeBuffer, msg); byte[] data = writeBuffer.getData(); if(testcase.getRaw().length != data.length) { diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java index e8ad9e0..34c6e64 100644 --- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java +++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocol.java @@ -18,48 +18,14 @@ under the License. */ package org.apache.plc4x.java.abeth.protocol; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; import org.apache.plc4x.java.abeth.readwrite.CIPEncapsulationPacket; import org.apache.plc4x.java.abeth.readwrite.io.CIPEncapsulationPacketIO; -import org.apache.plc4x.java.base.PlcByteToMessageCodec; -import org.apache.plc4x.java.utils.ReadBuffer; -import org.apache.plc4x.java.utils.WriteBuffer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.plc4x.java.base.GeneratedDriverByteToMessageCodec; -import java.util.List; - -public class AbEthProtocol extends PlcByteToMessageCodec<CIPEncapsulationPacket> { - - private static final Logger logger = LoggerFactory.getLogger(AbEthProtocol.class); - - private CIPEncapsulationPacketIO io; +public class AbEthProtocol extends GeneratedDriverByteToMessageCodec<CIPEncapsulationPacket> { public AbEthProtocol() { - io = new CIPEncapsulationPacketIO(); - } - - @Override - protected void encode(ChannelHandlerContext ctx, CIPEncapsulationPacket cipEncapsulationPacket, ByteBuf byteBuf) throws Exception { - WriteBuffer buffer = new WriteBuffer(cipEncapsulationPacket.getLengthInBytes()); - io.serialize(buffer, cipEncapsulationPacket); - byteBuf.writeBytes(buffer.getData()); - } - - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception { - byte[] bytes = new byte[byteBuf.readableBytes()]; - byteBuf.readBytes(bytes); - ReadBuffer readBuffer = new ReadBuffer(bytes); - while (readBuffer.getPos() < bytes.length) { - try { - CIPEncapsulationPacket packet = io.parse(readBuffer); - out.add(packet); - } catch (Exception e) { - logger.warn("Error decoding package: " + e.getMessage()); - } - } + super(new CIPEncapsulationPacketIO()); } }
