You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/08/03 12:31:31 UTC

[incubator-plc4x] branch feature/ethernet-ip updated: Added the EnipClient class from the enip projects client module

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

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


The following commit(s) were added to refs/heads/feature/ethernet-ip by this push:
     new 428b144  Added the EnipClient class from the enip projects client module
428b144 is described below

commit 428b14427c53c3bf037680370f72b40513af10f5
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Aug 3 14:31:28 2018 +0200

    Added the EnipClient class from the enip projects client module
---
 .../connection/EtherNetIpTcpPlcConnection.java     |  2 +-
 .../plc4x/java/ethernetip/netty/EnipCodec.java     | 58 ++++++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/connection/EtherNetIpTcpPlcConnection.java b/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/connection/EtherNetIpTcpPlcConnection.java
index 2ad3448..e14ac19 100644
--- a/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/connection/EtherNetIpTcpPlcConnection.java
+++ b/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/connection/EtherNetIpTcpPlcConnection.java
@@ -18,12 +18,12 @@ under the License.
 */
 package org.apache.plc4x.java.ethernetip.connection;
 
-import com.digitalpetri.enip.EnipCodec;
 import io.netty.channel.*;
 import org.apache.plc4x.java.base.connection.ChannelFactory;
 import org.apache.plc4x.java.base.connection.TcpSocketChannelFactory;
 import org.apache.plc4x.java.base.events.ConnectEvent;
 import org.apache.plc4x.java.base.events.ConnectedEvent;
+import org.apache.plc4x.java.ethernetip.netty.EnipCodec;
 import org.apache.plc4x.java.ethernetip.netty.Plc4XEtherNetIpProtocol;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
diff --git a/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/netty/EnipCodec.java b/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/netty/EnipCodec.java
new file mode 100644
index 0000000..96dc048
--- /dev/null
+++ b/plc4j/protocols/ethernetip/src/main/java/org/apache/plc4x/java/ethernetip/netty/EnipCodec.java
@@ -0,0 +1,58 @@
+/*
+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.ethernetip.netty;
+
+import com.digitalpetri.enip.EnipPacket;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageCodec;
+
+import java.nio.ByteOrder;
+import java.util.List;
+
+public class EnipCodec extends ByteToMessageCodec<EnipPacket> {
+
+    private static final int HEADER_SIZE = 24;
+    private static final int LENGTH_OFFSET = 2;
+
+    @Override
+    protected void encode(ChannelHandlerContext ctx, EnipPacket packet, ByteBuf out) {
+        EnipPacket.encode(packet, out.order(ByteOrder.LITTLE_ENDIAN));
+    }
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
+        ByteBuf buffer = in.order(ByteOrder.LITTLE_ENDIAN);
+
+        int startIndex = buffer.readerIndex();
+
+        while (buffer.readableBytes() >= HEADER_SIZE &&
+            buffer.readableBytes() >= HEADER_SIZE + getLength(buffer, startIndex)) {
+
+            out.add(EnipPacket.decode(buffer));
+
+            startIndex = buffer.readerIndex();
+        }
+    }
+
+    private int getLength(ByteBuf buffer, int startIndex) {
+        return buffer.getUnsignedShort(startIndex + LENGTH_OFFSET);
+    }
+
+}
\ No newline at end of file